CS21 Spring 2006
Homework 2
Due Monday, January 30 before 11:30pm


Answer the following Review Questions from the book on your own, but do not turn them in.

Please complete the following 4 programs and turn them in using cs21handin. You should work on this assignment by yourself (no partners). There is an additional extra credit problem, but don't try it until you have solved the first 4. After Tuesday's lecture, you should be able to complete the first two problems, and after Thursday's lecture the remaining ones.

    note: There are several problems at the back of the book numbered 5.1 (and 5.2, and 5.3, etc.). To ensure that you are doing the correct 5.X book problem, check that the one you are doing is on the same page number as the one listed with each problem below.

  1. Problem 5.14 on page 277 as a full program (write a program named ReadPositive.java, that contains a while loop to read in a positive int value from the user). A run of your program may look like:
    % java ReadPositive
    This program reads in a positive int
      Enter a positive int: -3
        Hey, -3 isn't positive.  Try again...
      Enter a positive int: 0
        Hey, 0 isn't positive.  Try again...
      Enter a positive int: -1000
       Hey, -1000 isn't positive.  Try again...
      Enter a positive int: 9
      You entered the positive value 9
    

  2. Problem 5.3 on page 279 (sum of even values). Name your class SumOfEvens.java. You can use part of your solution to the previous problem to ensure that the user enters a positive int value.
    % java SumOfEvens
    This program computes the sum of the even values from 0 to an upper bound you enter.
      Enter a positive upper bound value: -9
        Hey, -9 isn't positive.  Try again...
      Enter a positive upper bound value: 0
        Hey, 0 isn't positive.  Try again...
      Enter a positive upper bound value: 10
      The sum of the even values from 0 to 10 is 30
    
    % java SumOfEvens
    This program computes the sum of the even values from 0 to an upper bound you enter. 
      Enter a positive upper bound value: 8
      The sum of the even values from 0 to 8 is 20
    

  3. Write a program (LowerTri.java) to print out lower trinagular 10x10 matrix. Your program should be very easy to change so that it could print the a lower triangular 3x3 matrix, or 8x8, or 20x20, etc.

    Try using the printf method of the String class to format your output so that it looks something like this:

       1   0   0   0   0   0   0   0   0   0
       1   2   0   0   0   0   0   0   0   0
       1   2   3   0   0   0   0   0   0   0
       1   2   3   4   0   0   0   0   0   0
       1   2   3   4   5   0   0   0   0   0
       1   2   3   4   5   6   0   0   0   0
       1   2   3   4   5   6   7   0   0   0
       1   2   3   4   5   6   7   8   0   0
       1   2   3   4   5   6   7   8   9   0
       1   2   3   4   5   6   7   8   9  10
    

  4. Write a program (Stars.java) that asks the user to enter a value between 1 and 20, and then prints the following pattern of stars of the entered number of rows (note the error handing in my solution):
    % java Stars
    
    This program prints a pattern of stars.
    Enter the number of rows you want up to 20: -3
      Hey, -3 isn't in range, try again...
    Enter the number of rows you want up to 20: 33
      Hey, 33 isn't in range, try again...
    Enter the number of rows you want up to 20: 8
    
    * * * * * * * * * * * * * * * 
      * * * * * * * * * * * * * 
        * * * * * * * * * * * 
          * * * * * * * * * 
            * * * * * * * 
              * * * * * 
                * * * 
                  * 
    % java Stars
    
    This program prints a pattern of stars.
    Enter the number of rows you want up to 20: 13
    
    * * * * * * * * * * * * * * * * * * * * * * * * * 
      * * * * * * * * * * * * * * * * * * * * * * * 
        * * * * * * * * * * * * * * * * * * * * * 
          * * * * * * * * * * * * * * * * * * * 
            * * * * * * * * * * * * * * * * * 
              * * * * * * * * * * * * * * * 
                * * * * * * * * * * * * * 
                  * * * * * * * * * * * 
                    * * * * * * * * * 
                      * * * * * * * 
                        * * * * * 
                          * * * 
                            * 
    % java Stars
    
    This program prints a pattern of stars.
    Enter the number of rows you want up to 20: 4
    * * * * * * * 
      * * * * * 
        * * * 
          * 
    
    If you have problems getting this one, then try to do something easier first, like print the following patterns of stars, then see if you can use these solutions to help you solve the above problem:
    // try this:
    
    * * * * *
    * * * *
    * * *
    * *
    *
    
    // then try this:
    * * * * *
      * * * *
        * * *
          * *
            *
    
    // then try to put it all together
    
  5. Extra Credit. Do not try this before completing the first 4. Extra credit is graded on an all or nothing basis. You are welcome to submit partial solutions for me to look at, but you will not receive any credit for them; only completely correct solutions will receive extra credit. Try writing an applet (StarsApplet.java) that draws the following when run:
    % appletviewer stars.html
    

    See the code in section 2.9 (on page 103) for an example of how to write a Java applet (copy this code directly, try it out, and then modify it to solve the above problem.

    To run your applet, first create a file named stars.html with the following contents:

    <html>
    <body>
    
    <h3>Pattern of Stars Applet</h3>
    
    <applet code="StarsApplet.class" width=300 height=225>
    </applet>
    
    </body>
    </html>
    
    Then run appletviewer on this file:
    % appletviewer stars.html
    

Please use the cs21handin command to turn in your homework.