Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > The first step of Java[2]
【标  题】:The first step of Java[2]
【关键字】:The,first,step,of,Java
【来  源】:http://blog.csdn.net/DL88250/archive/2007/01/21/1488952.aspx

The first step of Java[2]

Your Ad Here

/*
   Write a Java program called Card.java that prints out your information in the following format:
   Name: <Your name>
   Address: <Your address>
   Email: <Your email>
   Phone: <Your contact phone number>
*/

public class Card
{
 public static void main(String[] args)
 {
  System.out.println("Name: " + "DL88250");
  System.out.println("Address: " + "SEI");
  System.out.println("Email: " + "Dl88250@gmail.com");
  System.out.println("Phone: " + "5038553");
 }

/*
   Assume a bank account begins with a balance of $100 and earns interest at an annual rate of 5%. 
   The interest is computed at the end of each year using the following formula:
   newBalance = previousBalance * (1 + interestRate)
   Write a Java program named ComputeInterest.java to compute and display this account balance at
   the end of each year for a five-year period. Do not use loops.
*/

import java.lang.Math;

public class ComputeInterest
{
 private static double interestRate = 0.5;
 
 public static void main(String[] args)
 {
  double balance = 100.0;
  System.out.println("The interest rate is: " + interestRate);
  System.out.println("The balance is: " + balance);
  System.out.println("After 5 yeas, balance is: " + balance * Math.pow(1.0 + interestRate, 5.0)); 
 }
}

/*
   The following pseudocode will build an array, a, containing the first n prime numbers:
   Set the first element of a to the lowest prime number (2).
   For each odd number, test, starting at 3:
       For each element, p, in a:
           Compute the remainder of test/p.
       If the remainder was non-zero for every p:
           Append test to a.
    If the length of a equals n:
     break

  Create a Java program, PrimeNumbers.java, to implement this algorithm.
  The program should accept one command-line argument,
  the number of primes to generate (n).
  It should respond by printing the first n prime numbers,
  followed by a message stating the n-th prime number.
  If more than 10 prime numbers are requested,
  the program should print only the first five and the last five,
  separated by a line displaying an ellipsis ("...").
  For example:
  $ java PrimeNumbers 13
    2
    3
    5
    7
    11
    ...
    23
    29
    31
    37
    41
    The 13-th prime number is 41.
  $
  Other requirements:
  ·Your program should generate the entire array of prime numbers before printing any of them,
    rather than printing them "on the fly".
  ·For n equal to 1, 2, or 3, the output should read "first", "second", or "third" rather than "1-th",
    "2-th", or "3-th". (Feel free to generalize this to other numbers.)
  ·If no command-line argument is supplied, the program should print a helpful message to the Java error stream,
    System.err, and exit.
  ·Use int (4-byte) variables throughout the program.
  ·Your program should make no assumption about the maximum number of primes which can be requested.
    For example, you cannot simply define a as an 8000-element array.
  ·If the command-line argument is less than one, assume that it is equal to one.
*/

import java.util.Vector;

public class PrimeNumbers
{
 private static Vector primeNumbers = new Vector(); // Store the prime numbers
 
 // Display these prime numbers
 public static void Display()
 {
  int i = 0;
  for (i = 0; i < primeNumbers.size() && i < 5; i++)
  {
   System.out.println(primeNumbers.get(i).toString());
  }
  if (primeNumbers.size() > 10)
  {
   System.out.println("....");
   for (i = primeNumbers.size() - 5; i < primeNumbers.size(); i++)
   {
    System.out.println(primeNumbers.get(i).toString());
   }
  }
  else
  {
   for (; i < primeNumbers.size(); i++)
   {
    System.out.println(primeNumbers.get(i).toString());
   }
  }
  
  System.out.println("The " + primeNumbers.size() + "-th prime number is " + primeNumbers.get(i-1).toString() + '.');
 }
 
 public static void main(String[] args)
 {
  if (0 == args.length)
  {// no command-line argument is supplied
   System.err.println("Please type in argument, and try it again!");
   return;
  }
  int index = Integer.parseInt(args[0]);
  int remainder = 0;
  boolean includeFactor = false;
  int odd = 3;
  
  primeNumbers.add((Object)(2));
  for (int i = 0; primeNumbers.size() != index; i++)
  { 
   for (int j = 0; j < primeNumbers.size(); j++)
   {
    remainder = odd % (int)(((Integer)(primeNumbers.get(j))).intValue());
    if (0 == remainder)
    {// the odd includes a factor at least
     includeFactor = true;
     break;
    }
   }
   if (!includeFactor)
   {
    primeNumbers.add((Object)(odd));
   }
   odd += 2;
   includeFactor = false;
  }
  
  Display();
 }
}

The first step of Java[3]:【上一篇】
CowNewSQL的扩展与编译:【下一篇】
【相关文章】
  • The first step of Java[3]
  • c++ ,java 和c# 谁是英雄 ?
  • VxWorks操作系统复位实战(三)[by Progsoft]
  • Java (接口)
  • Enum:Java枚举是类类型
  • java 起步
  • Java中控制台输入数值
  • Java中CardLayout卡片布局管理器使用的小例子
  • Java的自动转换
  • java 类型提升的约定
  • 【随机文章】
  • 指针的理解
  • NAT技术精华
  • Adobe Atmosphere 初级教程三
  • WML重点
  • 企业如何实施知识管理
  • Tomcat5.0.x使用经验两则
  • C/C++ FAQ(一)
  • MyEclipse中创建Hibernate对象关系映射文件出错解决办法
  • ASP、JSP与妓女
  • 金额大写转换
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.