CS1004 Homework #4
Due on Tuesday, April 5, 2005 at 11:00am

There are two parts to this homework: a written component worth 8 points, and programming component worth 17 points.  Submission instructions are available here.

Written questions

As described in the homework submission instructions, you may submit this as a hardcopy, or as a file along with your programming problems in one of four formats (Word, PDF, HTML, or plaintext).  Make sure to put at least your name and the section number on top of the homework whether it's submitted in written or electronic form, and if it's submitted electronically, make sure you name your file correctly.  It makes it very difficult for the TAs if you don't follow the exact submission instructions, especially on the electronic version.  Homeworks that omit these may not be graded.

Note that problems assigned from Schneider/Gersting or Lewis/Loftus are the exercise problems at the end of each chapter, not the practice problems or self-review questions.  (The practice/self-review problems are optional, and solutions for them are provided in the book.  For obvious reasons, the solutions for the exercises are not. ;-))

  1. (2 points) We can use loops to simulate many different mathematical functions; for example, we implemented multiply in assembly.  We'll look at a couple more examples in class, but one that we won't is divisionWithout using the division operator, sketch out the psuedocode necessary, using a for, while, or do loop, to divide two numbers and obtain the quotient and remainder.  (Psuedocode means "approximate code"; in other words, don't worry about the class definition, imports, etc.; focus on the main loop structure and the operations to be done inside it.  You can see a description of psuedocode in section 2.2.1 of Schneider/Gersting.)
  2. (2 points) Janak needs a random number generator, so he decides to use his newfound knowledge of Java to write the following code.  His goal is to write a program that prompts the user if they want a random number, and if yes, it prints out one and asks the user again (if no, it simply quits).

    import java.util.*;

    public class RandomGenerator {
      public static void main(String[] args) {
        Random rand = new Random();
        Scanner scan = new Scanner(System.in);
        String getMoreNumbers;
        System.out.print("Do you want a random number (type yes or no)? ");
        getMoreNumbers = scan.nextLine();
        while(getMoreNumbers.equals("yes")) {
          System.out.println("The next random number is " + rand.nextInt());
        }
      }
    }


    While the program compiles, much to his dismay, the program gets stuck in an infinite loop printing out random numbers forever!  He has to use CTRL-C to exit the program.  Why does this happen?  Help Janak out and fix this program:
    1. (1 point) State in 2-3 sentences what the fundamental problem is;
    2. (1 point) Write a corrected version.  Since you won't be compiling it, the syntax doesn't have to be perfect, but make sure to get the loops correct.  (If you really want to, feel free to type it in and compile it, but the goal of this exercise is to spot the bug in the loop without having to compile it.)
  3. (4 points) Up until this point in the course, we've "designed" the programming assignments for you, but this won't always be the case -- coming up with your own program design is very important, especially after the end of the semester, when the instructors of the course aren't around.  One major aspect to designing any complex software is to not start by writing Java code -- instead, we'll think and write out what the program should do without even worrying about the code initially.  In this exercise, we'll explore this process for this homework's programming problem #3, which involves the implementation of the classic Rock-Paper-Scissors game (see Lewis/Loftus programming exercise 5.16, on page 281), where you are to design a program that pits the user against a computer player.  Before you implement it, let's think about the design of the program and see if we can sketch out what the program's control flow is; in Software Engineering parlance, this is one piece of a design document.  There are several different ways to write this out:
    1. Lists of steps, like we did in the first homework:
       
      1. Computer make choice.
      2. Prompt user for choice.
      3. Was choice valid?
      4. etc.

    2. A flowchart. You can see a brief explanation of how a flowchart is diagrammed here.  You don't have to worry about the small mechanics of a flow chart -- something approximate, like the following, is appropriate.

       
    Pick one of the two methods above, and suggest how Rock-Paper-Scissors would work in a way that satisfies the problem description (2 points).  Finally, write out 4-6 lines (or, if you prefer, a UML diagram) explaining the class you would need to write -- including the key variables and utility Java classes (i.e., not those in java.lang.*) needed -- that you would need to use in order to implement this system (2 points).

Programming problems

As described below, you will submit this part of the assignment as five files: three .java files, corresponding to the source for each problem, and a README file (no typescript is necessary).  Make sure to put comments in your code - you may lose points if you don't comment your code.  Look at previous solutions, or textbook examples, for an idea as to how to comment your code.

  1. (4 points; 2 points for each pattern) Lewis/Loftus programming exercise 5.13 (p. 281).  Only implement 2 patterns (i.e., amongst a, b, c or d, choose a and b, or b and c, etc.). Feel free to use the book's Stars.java program as a starting point. Name each of your programs Stars_X.java where X identifies the pattern (a, b, c, or d) implemented by the program. Note: you must use looping constructs a la Stars.java to receive any credit. Do not simply print each structure as a series of sequential System.out.println() statements.
  2. (6 points) Using the included PairOfDice and Die classes (discussed in lecture and in the book), write a program named SimpleDiceStats that rolls the pair of dice N times and records the following summary from the N rolls:
    1. (2 points) The number of boxcars (6, 6) encountered;
    2. (2 points) The number of snake eyes (1, 1) encountered; and
    3. (2 points) The number of rolls totaling seven.
    4. (1 point extra credit) Also record and report the number of times consecutive boxcars, consecutive snake eyes, and consecutive 7s were rolled.

    For each of these statistics, print out, on the screen, the frequency (count) and a percentage of the total number of rolls.

    We'll supply N in an unusual fashion: it will be input on the command-line when your program is executed (i.e., the user will type "java SimpleDiceStats 1000"), so you will receive the value from your main method's 'args' parameter.  In class, we'll give a code example of how to process command-line arguments.
  3. (7 points) Lewis/Loftus programming exercise 5.16 (p. 281): Actually implement the Rock-Paper-Scissors program that you designed in written part #3.  Make sure the computer plays fairly (i.e., it picks rock, paper, or scissors purely at random).  You must allow the user to play multiple games (i.e., use a loop).  Here's an example execution of the program.  User input is in bold.

    $ java RockPaperScissors

    Choose rock, paper, or scissors: rock
    I chose paper.  I win!
    Play again? y
    Choose rock, paper, or scissors: paper
    I chose paper.  Tie!
    Play again? n

    You won 0 times.
    You lost 1 time.
    We tied 1 time.

    $


    Point breakdown: