现在的位置: 首页 > 综合 > 正文

The first step of Java[2]

2013年10月10日 ⁄ 综合 ⁄ 共 3891字 ⁄ 字号 评论关闭

/*
   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();
 }
}

抱歉!评论已关闭.