15-110 Introduction to Programming [Reid-Miller]
LAB eXtra [while and do loops]
Objective
- To translate a flow chart of a while loop into Java code.
- To read a while loop and determine what it is computing.
- To parameterize a method that generates random numbers.
- To write several while or do loops with different conditions using
this method.
Exercises
Using Eclipse, write a simple Java program in a project named
LabX that contains a class named LoopTester with a
main method.
For each of the following exercises, do not move on to the next
exercise until you have the current exercise working correctly.
- In the main method write the java code corresponding to
the flowchart below. Before the loop assign n to 20. After
the loop print the values of sum1, sum2, and
sum3. In a comment, explain what the loop is computing and
the relationship amoung sum1, sum2, and
sum2.
In part 2 you will write a method that returns a random integer
between x and y. In parts 3-5 your main method will call this method
supplying different values for x and y.
- Next, write a static method to compute and
return a random integer between x and y, inclusive, assuming x ≤ y.
Your method should not assign values to x or y nor print the random
number. Instead it will return the random number. Your static method
should have the following header:
public static int getRandomNumber(int x, int y)
- THINK: Suppose x = 0 and y = 10, what numbers do you want to
generate, and how would you do so using Math.random()? Suppose x = 4
and y = 5, what numbers do you want to generate, and how would you do
so using random? Consider x = 2 and y = 7. What operations
did you do in each case? Do you see a pattern? Write the operations
in terms of x and y.
- After the first loop in the main
method print out 20 random integers between 5 and 10 (inclusive) on
the same line with one space in between each number:
- Use a while loop that repeats 20 times, printing one number
each time it repeats.
- To check that your getRandomNumber is working correctly,
make sure that your output includes both 5 and 10 and no numbers
outside that range. Run it several times. Also, be sure your output
has exactly 20 numbers.
-
After your second loop, write another loop that prints out random
integers between 10 and 100 (inclusive) on the same line, each
separated with a single space, until it prints out the first random
number greater than 90. Use your static method again.
- REMEMBER: Each time you call the method
getRandomNumber, you will get a new random number. If you
need to test that same random number later, you should store the
number in a variable so you can use it later.
- THINK: What loop condition do you need now to stop this loop?
<--
- Later we will see how a do loop would be better choice here.-->
- THINK: Which would be better, a while loop or do
statement?
Advanced Exercise (If you have time...)
-
After your third loop, write another while loop that prints out random
integers between 100 and 999 (inclusive).
until it has printed out five even numbers.
The five even numbers do not have to be one after another. We're only
looking for a total of five even numbers.
- Create a variable to act as a counter, initially set to 0, before you
start this loop. Each time you repeat the loop, get a random number using
the static method you wrote in problem 1 and print it out. If it is even,
add 1 to your counter.
- THINK: What loop condition do you need now to stop this loop?
- THINK: Which would be better, a while loop or do statement?
- Add a fifth while loop that prints out random integers between 1000 and
9999 (inclusive), one per line, until at least one of the following occurs:
- three numbers are output in a row that are greater than 7000
- ten numbers are output.