CS Labs: Lab 6

CS Labs: Lab 6

To accompany Chapter 6 of An Introduction to Object-Oriented Programming with Java by C. Thomas Wu.

Repeat, repeat, repeat ...

There are 9 checkpoints . If you need help with any exercise, raise your hand.

First, copy the lab materials to your account.

             cp -r /home/Classes/Cs1/Labs/Lab06 .
     
Everything you need for the lab exercises today is contained in this new directory.

Change directory to Lab06.

Keep paper and pens or pencils handy.

Loop Conditions

Go into subdirectory Sum and edit the class Demo. This program reads in an integer n and cumulatively adds 1/n until it sums to 1. Examine the implementation, then run it twice, first entering 2 for n and then entering 5.

Run the program once more, entering 7. Notice what is happening. For this input, the program loops infinitely. Why?

We can make sure the program terminates by stopping the loop when the sum equals or exceeds one. Make this change to the program.

1 Show us your corrected program, and explain the problem with the original.

Another Problem Program

Go into subdirectory MarchingOn and edit the Demo class. This program asks the user for the numbers of rows and columns. It is intended to print asterisks in the form of a rectangle, using the number of rows and columns that were provided by the user. For example, if the user specifies 3 rows and 5 columns, the following should be printed:
*****
*****
*****
Run this program and see what happens. Obviously, you need to correct it. (Hint: there are two things you will need to correct.) To help see what is wrong, hand trace the code, using small sample values for the numbers of rows and columns, such as 1 and 2.

2 Show us your corrected program. Be sure that you can explain everything that was wrong with the original.

Loops in Graphics

Go into directory Dazzle. Study the code for the paint() method of the Dazzle class and run the program. Notice that one way to construct a Color object is to construct it with three integer values r, g, and b, which stand for the amounts of red, green, and blue (respectively) to use in creating the color. The value that can be assigned to r, g and b can range from 0 to 255. In the current program, each time a color is created, the amounts of red, green, and blue are equal to each other. Balancing them like this will always produce shades of grey. When all three are zero, the color will be black, and when all three are 255, the color will be white.

Modify the values assigned to r, g, and b so that the circle you draw will have non-gray colors. We want you to use certain constraints while assigning the values. On each iteration of the loop:

The idea is to have some fun thinking of interesting ways to select the colors. Don't spend too much time on it; there's still a lot of this lab to complete.

3 Show us your revised code, and how it runs.

while and do-while

Go into subdirectory AnswerMe and edit the class Demo. This program is intended to repeatedly ask the user for a number between 1 and 10 inclusive until the user enters a value in this range, printing an error message if the user's input is incorrect. However, when you run it, it terminates when you enter any integer.

Fix this program so that it works correctly.

4Show us your corrected program, and explain the problem with the original.

Change this code to use a do-while loop having the following pseudocode structure:

do
  prompt user for a number n
  if n is not in range 1 to 10, print error message
while n is not in range 1 to 10
In the original version, there were two input statements but only one test. In this version, there is only one input statement, but there are two tests.

Make sure your do-while version behaves the same as the original version.

5 Show us your modified code.

Finally, convert this program into a loop-and-a-half version that has only one input statement and one test but that behaves the same as the original version.

6 Show us your modified code.

A Challenge Program

Go into the subdirectory OneTwoThree and edit the Demo class. When you run this program, you get the following output:
     *
     *
     *
     
Modify the program so that the output is
     *
     **********
     *
     
There are 10 asterisks in the middle row.

Your modifications must obey the following constraints:

  1. Print only one asterisk at a time. The only statements you may use for printing are:
  2. Add only one if statement to the body of the original for loop. Hint: Use a for statement inside the if statement. Your if condition should test for a particular value of i.
  3. Make no changes to the outer for loop.

7 Show us your completed program.

Estimating PI

Consider the unit circle which, in cartesian coordinates, is defined by the equation x*x + y*y = 1. The area of the part of the unit circle that lies in the first quadrant is pi/4, where pi is a well-known constant.

Imagine that you throw darts at random at the unit square (in the first quadrant) so that the darts land randomly on (x,y) coordinates satisfying 0 <= x <= 1 and 0 <= y <= 1. Some of these darts will land inside the unit circle. Since the first quadrant part of the unit circle has area pi/4 and the unit square has area 1, the proportion of darts you throw that land in the unit circle will be approximately pi/4.

Go into the subdirectory EstimatePi and edit the Demo class. Examine this program and observe that it asks the user to enter the number of darts to throw. Since Math.random() produces a double value in the range of 0 to 1, we can use this to generate random (x,y) values representing points in the unit square.

The body of the for loop generates these points and accumulates the number of them that land in the unit circle in the variable inside. When the for loop terminates, the value of inside will be the total number of darts that landed inside the unit circle. The quotient obtained by dividing this value by the total number of darts thrown should approximate the value of pi/4.

The program then outputs this quotient, times 4 (to make it an approximation to pi). Of course, if you throw just a few darts, the approximation will be rather poor. The more darts you throw, the better an approximation you will get.

The program is missing only the test in the body of the for loop that determines whether the dart with coordinates (x, y) lies inside the unit circle.

You are to complete the program and test it with several choices for numbers of darts. At least one of your choices should be 1000000.

For your information, this sort of method for approximating a value is called Monte Carlo, in honor of the famous casino in Monaco, where random numbers are generated for entertainment.

8 Show us your completed program and the results of your tests.

After the Lab


9 Show us that you have logged out, cleaned up, and pushed in your chairs for this last checkpoint.

End of Lab


Susan Haller and Timothy Fossum, University of Wisconsin-Parkside