Lab 8 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 Mozilla, and open emacs.

  1. The program in prints "I love Computer Science!!" as many times as the user wishes. Copy it to your directory and compile and run it to see how it works. Then modify it as follows:
    1. 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.  
    2. 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 to sum the numbers you need to do the following: add a variable to hold the sum and set it to 0 before the loop; update it inside the loop to do the addition.

    Print this program to turn in.

  2. File 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:
    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.

  3. 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.
    1. 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.

    2. 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 to 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.

    3. 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:
      1. 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
        Print your revised program and write the answer to the following question on it.

      2. A Java int is represented with 32 bits using two's complement. The largest value that can be stored in an int is 231 - 1 which is 2,147,483,647. 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 the largest possible int? Explain.

  4. Election Day Today is election day and, as you know, people are concerned about the accurate counting of votes. In this exercise you will write a simple program to help tally election results. The program will assume that there are only two candidates in the election. It will take as input the number of votes each candidate receives in each voting precinct and find the total number of votes for each candidate and the number of precincts each candidate carries (wins). Clearly a loop is needed. Each iteration of the loop is responsible for reading in the votes from a single precinct and updating the tallies.

    Candidates for the election will be represented by a Candidate class. A candidate will be described by the following instance data:

    The class contains the following methods:

    A skeleton of the Candidate class is in the file Open the file and complete the class as indicated in the comments. Compile the class to make sure you have no syntax errors.

    The file contains a skeleton of the program to tally the election results. Open the file and do the following as indicated by the starred comments in the program:

    1. Note that code to read in each candidate's name and party is already there. Add code to declare and instantiate the two Candidate objects.
    2. Set the precinct counter numPrecincts to 0 before the loop (this is part of the loop setup).

    3. Add code to control the loop. The loop should be controlled by asking the user whether or not there are more precincts to report (that is, more precincts whose votes need to be added in). The user should respond by typing in the letter y or n (upper or lowercase). To control the loop by asking the user you need to do three things:
      1. The variable response (type String - already declared) should be initialized before the loop. In this case just set it to "Y" so the loop control condition will be true.
      2. The loop control condition must be put in the while statement. The loop should execute as long as the response is y or Y - you can avoid using an or by using the equalsIgnoreCase method in the String class (see page 119).
      3. Just before the end of the body of the loop (see the comment) add a prompt to ask the user if there is another precinct. Read in the response. NOTE: You will need to use a scan.nextLine() statement twice - the first one reads in the NEW LINE character that remains in the input stream after reading the last integer the second one actually reads in the user's response.

    4. Complete the code inside the loop as directed by the comments. In most cases you will need to invoke methods in the Candidate class. Be sure that when you check to see who won the precinct you account for ties.

    5. Print out the results after the loop. This should include the name, party, total votes and the number of precincts won for each candidate. Also compute and print out the number of ties.

What to Hand In