Lab 7 In-Class: While Loops
This lab is designed to give you practice writing while loops.
As usual, create a subdirectory for this lab, open up the Web version of
this handout in Netscape, and open emacs.
- The program in LoveCS.java prints
"I love Computer Science!!" 10 times. Copy it to your directory and
compile and run it to see how it works. Then modify it as follows:
- Instead of using constant LIMIT, ask the user how many times the
message should be printed. You will need to declare a variable to store
the user's response and use that variable to control the loop. (Remember
that all caps is used only for constants!)
- Number each line in the output, and add a message at the
end of the loop that
says how many times the message was printed.
So if the user enters 3, your program should
print this:
1 I love Computer Science!!
2 I love Computer Science!!
3 I love Computer Science!!
Printed this message 3 times.
- If the message is printed N times, compute and print
the sum of the numbers from 1 to N. So for the example above, the last line
would now read:
Printed this message 3 times. The sum of the numbers from 1 to 3 is 6.
Note that you will need to add a variable to hold the sum.
Print this program to turn in.
- File PowersOf2.java contains a skeleton
of a program to read in an integer from the user and print out that
many powers of 2, starting with 20.
- Using the comments as a guide, complete the
program so that it prints out the number of powers of 2
that the user requests. Do not use Math.pow to compute the powers of 2!
Instead, compute each power from the previous one (how do you get 2n
from 2n-1?).
For example, if the user enters 4, your program
should print this:
Here are the first 4 powers of 2:
1
2
4
8
- Modify the program so that instead of just printing the powers, you
print which power each is, e.g.:
Here are the first 4 powers of 2:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
Print this program to turn in.
- The factorial of n (written n!) is the product of the integers
between 1 and n. Thus 4! = 1*2*3*4 = 24. By definition, 0! = 1. Factorial
is not defined for negative numbers.
-
Write
a program that asks the user for a non-negative integer and
computes and prints the
factorial of that integer. You'll need a while loop to do most of the work --
this is a lot like computing the sum of the numbers from 1 to some limit,
but it's a product instead. And you'll
need to think about what should happen if the user enters 0.
-
Now modify your program so that it checks to see if the user entered a
negative number. If so, the program should print a message saying that
a nonnegative number is required and ask the user the enter another number.
The program should keep doing this until the user enters a nonnegative
number, after which it should compute the factorial of that number. Hint:
you will need another while loop before the loop that computes the
factorial. You should not need to change any of the code that computes
the factorial
- As we discussed in class, when a value is too big (in the positive
or negative direction) to fit in the number of bits available, overflow
occurs. Factorials get big very fast, so they are a good place to see
overflow. Do the following:
- The loop that computes N! is actually computing the factorial of each
number from 1 to N-1 along the way. Add statements to your loop to print
the current value of N and its factorial in each loop iteration. So if the
user entered 4, your program should now print
1!=1
2!=2
3!=6
4!=24
Print your revised program and write the answers to the remaining questions
on it.
- A Java
int
is represented with 32 bits using two's
complement. What is the largest value that can be stored in an int
? (Do the calculation to find the actual value.)
- Run your program with an input of 20 and look at the values that
are printed. At some point the factorial becomes negative, which is
clearly wrong, but overflow actually occurs before this -- what is the
highest integer for which your program computes factorial correctly,
and what is its factorial?
Does this make sense given
your answer to (b)? Explain.
- Because an
int
uses 32 bits, its value can be big
and difficult to think about. To get a better feel for what is
going on, you will modify your program to use type byte
(an 8-bit integer type, still using two's complement)
instead of int
.
First save your factorial program under another name, e.g.,
FactorialByte.java. Remember to modify the name of the class as well.
Then modify your new program so that it uses type byte
instead of int
everywhere. You will have to cast
the result of the call to readInt() to a byte, as well as the result
of each calculation (* and + return ints even with bytes for arguments).
Now answer the following questions (on the same printout as your other answers):
- What is the largest value that can stored in type
byte
?
- Run your program with different inputs to
figure out the highest integer for which it computes
factorial correctly. (Hint: It's not very big!) What is the
integer, and what is its factorial? Be sure your answer makes sense
with respect to your answer to (i).
- Now figure out the actual factorial of the next biggest number
(the first one the program can't compute). Convert this value
to base 2.
Consider the rightmost 8 bits of this number, which is what
a
byte
can hold.
Interpreting this as an 8-bit
two's complement number, what is its base 10 value? SHOW
YOUR WORK!!
- This should be the same as the answer your program gives for
the first value it can't compute the factorial for. If it isn't,
go back and figure out what went wrong.
- File Guess.java contains a skeleton for a
program to play a guessing game with the user. The program should randomly
generate an integer between 1 and 10, then ask the user to try to
guess the number. As long as the
user guesses incorrectly, the program should ask him or her
to try again; when the guess is correct,
the program should print a
congratulatory message.
- Using the comments as a guide, complete the program so that it plays
the game as described above.
- Modify the program so that if the guess is wrong, the program
says whether it
is too high or too low. You will need an if statement (inside
your loop) to do this.
- Now add code to count how many guesses it takes the user to get
the number, and print this number at the end with the congratulatory message.
- Finally, count how many of the guesses are too high, and how many are too low;
print these values, along with the total number of guesses, when the user
finally gets it.
Print this program to turn in.
What to Hand In
- Hardcopy of LoveCS.java, PowersOf2.java, your factorial program, and Guess.java.
- Tar up your lab7 directory and e-mail it to your instructor with
cpsc120 lab7 in the subject line.