CS256: Lab 3


Due: See Moodle for the due date and time

Goals:

1)    Practice with nested if statements

2)    Practice with nested loops.

Requirements:

1) Create a directory named lab3 and put the following work in this directory.

 

2) Write a class named EasyLoop that continually asks the user for a number and echoes (prints) each number the user enters that is above (greater than) 100.  If the user enters a negative number, the program should stop.

Hints:

·         Declare a single variable to hold the numbers read from the user.

·         Initialize this variable to some non-negative value.

·         Put your read statement inside the loop

 

3) Write a class named ArithmeticStatistics to calculate and output the minimum, maximim, and average (i.e., the arithmetic mean – http://en.wikipedia.org/wiki/Arithmetic_mean) of a list of non-negative test scores.  Assume the scores will be entered one per line and the list will end when a negative value is input.  Note: The negative number that terminates the input stream must not be included in the calculation of the min, max, and average.  Format your output exactly as follows:
min: <minValue>; max: <maxValue>; mean: <arithmetic mean>
For example:

For the numbers 2.0, 3.0, 1.0, output:
min: 1.0; max: 3.0; mean: 2.0

If no non-negative scores are entered, you should print:

min: 0.0; max: 0.0; mean: 0.0

 

Hints: you need a counter variable, an accumulating variable, a variable to store the min, and one to store the max.  If the counter variable is 1.0, assign the value read to both the min and max variables.  Here is an example that prints a string containing the values of both min and max:

System.out.println(“min: “ + min + “; max: “ + max);

Caution: The double quotes shown above are not java double quotes.

 

4) Write a class named MakeAPattern that reads a number of lines from the user then makes a pattern of stars in the Run I/O window as follows:  If the user enters 7, the output should look exactly like:

*******

******

*****

****

***

**

*

The ‘algorithm’ is:  the top line has the same number of stars as the user entered.  The next line has one fewer stars, and so on.  The last line has only one star.

 

Hints: You need a nested loop to do this.  For the outside loop, use the variable holding the value you read from the user as your counter, and decrement the counter inside the loop instead of incrementing it as we normally do.  Use a different counter for the inner loop and initialize it to the value currently held in the outer loop counter.

Finally, use:

 System.out.print(‘*’) // caution: the delimiters shown here are not java single quotes

to print a single star and System.out.println() to print an end-of-line.

 

5) In a comment at the top of EasyLoop.java, please write short answers to the following questions:

 

1.     How are the control-flow characteristics of if-statements and while-statements similar and how are they different?

2.     How is the number of times a loop’s Boolean condition is evaluated related to the number of times the loop body statements are executed?

3.     Show the output the following code would produce:

double MIN = 10;

double MAX = 20;
double num = 15;

while (num > MIN)

{

System.out.println (num);

num = num - 1;

}

 

2% extra credit:

Write a class named Accumulate to input and sum a list of numbers from the user until any one of the following conditions is true: (a) a negative value is input, (b) the sum exceeds 100.0, or (c) 5 numbers have been summed.  If the user enters a negative number, this number must not be included in the count or the sum.  After the loop has terminated, output the count and the sum.  Output your messages to JGrasp’s Run I/O window by using System.out.println()

Hint: use a loop with logical operators in the condition or a boolean variable ‘done’.  You will also need a variable to store the sum.

 

6) Test your code until you feel you have worked out all the bugs.

 

7)  Write your name in a comment at the top of each file.  Make a zip file of your entire lab3 directory and submit this file on moodle.

Approach:

1) For the ArithmeticStatistics class: if the number you read is less than min, set min to be the number you read.  If the number you read is greater than max, set max to be the number you read.

Write English comments (pseudo-code) inside your new class stating what you intend to do.  See the hint above and ../demos/W02N03.java.

 

 2) For Accumulate:

Write English comments (pseudo-code) inside your new class stating what you intend to do.  See the example in ../demos/W03N01.java

 

3) After completing the pseudo-code, go back and write java code underneath your comment lines.  (The above pseudo-code is not complete.)

 

4) Test your class by trying all three terminating conditions in separate tests, i.e., the sum exceeds 100.0, the user inputs a negative value, and the user enters more than 6 numbers such that the first 5 numbers together total less than 100.