Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The goal of this lab is to write a program, Election, that tallies votes in an election and finds the winner. The votes are write-in

The goal of this lab is to write a program, Election, that tallies votes in an election and finds the winner.

The votes are write-in so there is no predetermined set of candidates. Whoever appears the most in the votes is the winner, where a tie can be broken arbitrarily. The user enters the votes, one vote per line, and ends with either typing an empty line or CTRL-D. The program tallies the votes using two arrays, a String[] variable named names and an int[] variable namedcounts. During the course of receiving votes from the user, the two arrays will have the same lengths. Their lengths are equal to the number of unique names that have appeared in the votes so far. Thus, the initial length is 0 for both arrays.

To add a vote, we use three methods:

 public static int find( String[] names, String name ) 
 public static String[] addName( String[] names, String name ) 
 public static int[] addNewCount( int[] counts ) 

The first one, find, scans the array, names, using a for-loop that iterates over all the valid index values 0 .. names.length - 1. If the array has an entry equal to the second parameter, name, the method returns the index at which the value equal to name has been found. If the for-loop completes without terminating, the method returns -1 as the indication of not seeing name.

The second method, addName, creates a new String array from names by appending the value of name at the end and then returns the new array.

The third method, addNewCount, creates a new int array from counts by appending a new element of 1 at the end.

After necessary initialization (the two arrays and a Scanner object to receive an input from the keyboard), the program prints a prompt to inform the user how to enter the votes.

########################################### # Enter the votes, one vote per line. # # End with either CTRL-D or an empty line.# ########################################### 

After this, the program enters a while-loop to receive an indefinite number of votes from the user. The loop can be terminated in two possible ways. The condition for the while-loop should be set to terminate with CTRL-D; that is, the condition is:

1

 while ( keyboard.hasNext() ) 

where keyboard refers to the Scanner object for receiving input from the keyboard (that is,System.in).

In the body of this while-loop, the program receives input from the user usingkeyboard.nextLine() and stores it in a String variable, name. If name is equal to "", the loop terminates using break. If this does not occur, the method calls find to identify the position at which name occurs in names. If the returned value of find is nonnegative, the name already exists on the array, so the program increases the value of counts at the returned position by 1. Otherwise, the program executes addName( names, name ) and addNewCount( count ) to record that the name has appeared just once.

After terminating the loop, the program calls a method

 public static void findWinner( names, counts ) 

that finds the winner based on the tally. The method findWinner first reports how many votes each person received, in the order of appearance in the array names, and then computes the winner. To compute the winner, it uses two int variables, maxCount and theWinner, whose initial values are 0. The method scans the contents of the array using a for-loop with an iteration variable,i. During the scan, if counts[ i ] is greater than maxCount, names[ i ] is the winner for the moment, so the program updates maxCount with counts[ i ] and theWinner with i.

Here is a execution example of the code.

% java Election ########################################### # Enter the votes, one vote per line. # # End with either CTRL-D or an empty line.# ########################################### Frodo Sam Pippin Frodo Frodo Pippin Pippin Pippin Sam Sam Pippin 
Frodo received 3 votes. Sam received 3 votes. Pippin received 5 votes. -------- 
The winner is Pippin! % 

2

You can enter a much larger set of votes. The accompanying file, votes.txt, has 100K votes ending with an empty line. You can feed the contents by using the concept of redirection represented with the symbol <. Using redirection, we open a file and feed its contents as the input character stream in place of the keyboard.

If you type of the command

java Election < votes.txt \begin{verbatim} the contents of \code{votes.txt} are fed to the program \code{Election}. 
\begin{verbatim} % java Election < votes.txt ########################################### # Enter the votes, one vote per line. # # End with either CTRL-D or an empty line.# ########################################### Draco received 20125 votes. Dudley received 19883 votes. Harry received 20135 votes. Ron received 19799 votes. Hermione received 20058 votes. -------- The winner is Harry! % 

3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions