L02

Due by the end of Tuesday, 19 January 2021


SUBMIT   /   CHECK

This Week's Activities

Programming Activity 1
Download the file Rectangles.java. Run the program and examine the code. Currently it prints one rectangle.

Revise the program so that it prints multiple rectangles -- prompting for more heights and widths until a negative height is entered. Use good sentinel-loop design. In particular, that means no if control of any sort.

It's OK to start if the user has to enter a width after the negative height, but if you think about it, you should be able to figure out how to write it so that the user can just press enter after the negative height. And you can do it without using an if control. All you have to do is place the while control in the right place!

Programming Activity 2
Continuing with review, this activity looks at conditionals and Strings.

Create a simple program named Answers.java that implements the following pseudo-code:

  1. Ask for and read a question from the user.
  2. If the "question" does not end with a question mark ("?") report that that's not a question.
  3. Otherwise if the question starts with Who answer William Lyon Mackenzie King.
  4. Otherwise if the question starts with What answer The Higgs boson.
  5. Otherwise if the question starts with When answer Right now!
  6. Otherwise if the question starts with Where answer Here.
  7. Otherwise if the question starts with Why answer Why not?
  8. Otherwise answer That is one of the great mysteries of the universe.

Use good design. In particular, use if-else-if controls as appropriate.

Remember that there are String methods for checking what a String startsWith or endsWith.

Programming Activity 3
Write a simple program named CountRolls. It uses the following code to simulate rolling a die:
roll = 1 + (int)(6 * Math.random());
It rolls the die 600 times, counting how many of each number were rolled. (Use an appropriate kind of loop.)
Remember to name the numbers 6 and 600 in this program.

Use an array to hold the six counters you need.

Hint:
  • Let counter[roll-1] keep track of how many times roll was rolled.
  • Think about the way we add one to a variable. So, how do you add one to counter[roll-1]?
  • Does the way we add one to counter[roll-1] change depending on what value roll has?

After the rolls have all been counted, print out the counts in a little table. For example:

Roll Count 1 100 2 102 3 98 4 108 5 103 6 89

Submit this/these files:

You will be graded on the following:

  1. Rectangles.java asks for heights and widths repeatedly
  2. . . . stops after receiving a negative height (OK if read corresponding width), without trying to draw a rectangle of that height.
  3. . . . . . . and does so without requiring the user to enter a width
  4. . . . does not use any conditional controls
  5. Answers.java reads a line of user input.
  6. . . . uses at least one if-else if structure
  7. . . . exactly one response message is printed regardless of the (valid) input
  8. . . . no redundant conditions in the if controls
  9. CountRolls.java creates an array of 6 (or 7) integer variables.
  10. . . . the die-roll simulation code is part of a for loop that iterates exactly 600 times.
  11. . . . that loop updates the counters correctly, without using any conditional control.
  12. . . . correct results printed, using a for loop

SUBMIT   /   CHECK