CPSC 120A Mini-Assignment - Election Day
Due Monday, November 12 at 4:00 p.m.

Create a subdirectory of your Assignments directory named mini for this assignment.

Election day is this week and, as you know, people in the last few years have been 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 Candidate.java. Open the file and complete the class as indicated in the comments. 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:

  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). (There is a similar example in pre-lab.) 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. For example, the actual adding of the votes from the current precinct to the total for a candidate is done by a method 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