COS 126

Conditionals and Loops
Programming Assignment

Due: 11:59pm

The goal of this assignment is to write five short Java programs so that you get used to writing code and debugging it. Do five of the following seven exercises. All questions have equal weight.

  1. Write a program Distinct.java that takes three integer command line parameters a, b, and c and print out the number of distinct values (1, 2, or 3) among a, b, and c.

  2. Write a program Zodiac.java that takes two command line integers M and D and prints the Zodiac sign corresponding to month M (1 = January, 12 = December) and day D. Use the following table

    SIGN FROM TO
    Capricon December 22 January 19
    Aquarius January 20 February 17
    Pisces February 18 March 19
    Aries March 20 April 19
    Taurus April 20 May 20
    Gemini May 21 June 20
    Cancer June 21 July 22
    Leo July 23 August 22
    Virgo August 23 September 22
    Libra September 23 October 22
    Scorpio October 23 November 21
    Sagittarius November 22 December 21

  3. Write a program Statistics.java that takes an integer N as a command line argument and uses Math.random to print N real numbers between 0 and 1, their average, and their standard deviation. The average and standard deviation of the values x1, x2, ..., xn, is given by the following formulae:
    sumx   = x1 + x2 + ... + xn
    sumx2  = (x1)2 + (x2)2 + ... + (xn)2
    avg    = sumx / n
    stddev = sqrt ( (sumx2) / n  -  avg * avg )
    

  4. Write a program NHellos.java that modifies TenHellos.java so that the user specifies the number of lines to print N as a command line argument. Hint: consider using i % 10 and i % 100 to determine whether to use "st", "nd", "rd", or "th" for printing the ith Hello.

  5. Write a program PrimeCounter.java that takes a command line argument N and prints out the number of primes less than N.

  6. Write a program Checkerboard.java that takes one command line parameter N and prints out a two dimensional N-by-N checkerboard pattern with alternating spaces and asterisks, like the following 4-by-4 pattern.
    * * * *
     * * * *
    * * * *
     * * * *
    

  7. Write a program BohrRadius.java that finds the radii where the probability of finding the electron in the 4s excited state of hydrogen is zero. The probability is given by: (1 - 3r/4 + r2/8 - r3/192)2 e-r/2, where r is the radius in units of the Bohr radius (0.529173E-8 cm). Use Newton's method. By starting Newton's method at different values of r, you can discover all three roots.