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.

  1. 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:
    1. 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!)
    2. 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.  
      
    3. 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.

    4. 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.
      1. 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
        
      2. 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.

    5. 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 a sum, 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!

    6. 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.

      1. Using the comments as a guide, complete the program so that it plays the game as described above.
      2. 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.
      3. 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.
      4. 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.