CS21 Spring 2005
Homework 3
Due Monday, Feb 6 before 11:30pm


Please complete the following 4 programs and turn them in using cs21handin. You should work on this assignment by yourself (no partners).

  1. Using methods in the String class, and perhaps loops, write a program (ScrabbleScore.java). That reads in a word entered by the user and computes the words Scrabble score. Scrabble is a word game where players build up linked words (like a crossword puzzle) from letter tiles. Players get a word score for each word they add to the board. A word's score is the sum of the points of each individual letter in the word. The letter values are the following:
    Point Value                Letters
    -----------------------------------------------------------
       1            A, E, I, O, U, L, N, R, S, T 
    -----------------------------------------------------------
       2            D, G
    -----------------------------------------------------------
       3            B, C, M, P
    -----------------------------------------------------------
       4            F, H, V, W, Y
    -----------------------------------------------------------
       5            K
    -----------------------------------------------------------
       8            J, X
    -----------------------------------------------------------
      10            Q, Z
    -----------------------------------------------------------
    
    Your program should prompt the user to enter a word, then it should use the nextLine method of the Scanner class that returns a String reference to the line of input entered by the user. Next, your program should check to see if the line entered is valid (a single word). If the input is valid, output the word and its Scrabble score, if not output the word and an error message. Here are some runs of my program (note the error handling):
    % java ScrabbleScore 
    
    This program computes a word's Scrabble score.
    Enter a single word: zoo
    
    The Scrabble score for 'zoo' is: 12
    
    
    % java ScrabbleScore 
    
    This program computes a word's Scrabble score.
    Enter a single word: HeLLo
    
    The Scrabble score for 'HeLLo' is: 8
    
    
    % java ScrabbleScore 
    
    This program computes a word's Scrabble score.
    Enter a single word: hello there
    
    Error: 'hello there' is not a valid word
    
    
    % java ScrabbleScore 
    
    This program computes a word's Scrabble score.
    Enter a single word: 234566
    
    Error: '234566' is not a valid word
    

  2. Write a program (RadioActiveDecay.java) that simulates the radio active delay of 10,000 atoms, where each atom has a 50 percent chance of decaying each year. Your program should use methods of the Random class to simulate the radioactive decay of 10,000 atoms until none are left. For each year, print out the year and the number of atoms left that year, until all atoms have decayed. For example, here are two runs of my program:
    % java RadioActiveDecay
    
    Year      Atoms Left
    -----    ----------
       0      10000
       1       5029
       2       2546
       3       1256
       4        625
       5        311
       6        171
       7         84
       8         47
       9         32
      10         11
      11          5
      12          0
    
    % java RadioActiveDecay
    
    Year      Atoms Left
    -----    ----------
       0      10000
       1       5053
       2       2535
       3       1288
       4        623
       5        319
       6        155
       7         85
       8         44
       9         19
      10         11
      11          5
      12          2
      13          1
      14          0
    

  3. Write a program (MonteCarloPi.java) that approximates PI using the Monte Carlo Method. The idea of this method is based on throwing some number of darts at random location on a circular dart board which is inside a square frame. Here is picture of what the dart board looks like, where the circle has a radius of 1:


    Using the ratio of the cirle's area to the square's, you can get the value of one fouth pi, which is approximately equal to the number of darts that hit inside the circle to the number thrown:
    area of circle with radius 1     PI	    number of darts inside circle
    ----------------------------  =  --    ~=   -----------------------------
    area of square with side  2      4          number of darts thrown 
    
    Each dart's position is given by an (x, y) coordinate, so you need to randomly generate two values for each dart throw, which will be somewhere inside the square. You can use the Pythagorean Theorm to determine if the dart is within the circle:


    Note: you can equivalently simulate this by just considering the upper right quadrant of the dart board (positive x, y values and a square of side 1).

    Your program shouls prompt the user to enter the number of dart throws, and then compute the approximation of pi. Each run should produce a slightly different approximation due to the random number generator. Here are two runs of my program:

    % java MonteCarloPi
    
    This program approximates the value of PIusing the Montecarlo Method.
      Enter the number of darts to throw: 1000
    
      The value of pi is: 3.104
    
    % java MonteCarloPi
    
    This program approximates the value of PIusing the Montecarlo Method.
      Enter the number of darts to throw: 1000
    
    The value of pi is: 3.092
    

  4. Implement a program (IntegralPi.java) that approximates pi using the integral approximation of the area of 1/4 of a circle. The idea is to break the 1/4 circle up into some number of rectangles and to sum the area of the rectangles to get an approximation of the area of the 1/4 circle:


    For each rectangle, the width is constant (it depends on the radius and the number of rectangles). You can compute the height of each rectangle using the following formula (where r is the radius and x is the point on the x axis of the midpoint of the rectangle):


    Your program should ask the user for the number of rectangles, and then print out the approximation of pi using this method.

    In the comment at the top of your solution to problem (4) answer this question: Which pi approximation method is better? Why?

    a note about debugging: if your program doesn't seem to be computing the correct value, add debugging output statements to your program to print out intermediate results (like the height and the width and the area values as you compute them for each rectangle). And, think about what type you want to use for each of these.


Please use the cs21handin command to turn in your homework.