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 I love Computer Science!! 2 I love Computer Science!! 3 I love Computer Science!! Printed this message 3 times.
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.
Here are the first 4 powers of 2: 1 2 4 8
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.
1!=1 2!=2 3!=6 4!=24Print your revised program and write the answer to the following question on it.
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.
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:
John Kerry (Democrat) - 3000 votes; 5 precincts
A skeleton of the Candidate class is in the file Candidate.java. 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 Election.java 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: