Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

IN JAVA

image text in transcribedimage text in transcribed

The goal of this lab is to write the 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. The user enters the votes, one vote per line, and ends with either typing an empty line or control-D. The program tallies the votes using two arrays, a stringt variable names and an inti variable counts. During the course of receiving votes from the user, the two arrays have the same lengths. The length is equal to the number of names that have appeared in the votes so far. Thus, the initial length of the two arrays is 0. 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 inti1 counts The first one scanns 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 name given as the second parameter, it returns the index, thereby terminating the for-loop. If the for-loop completes without terminating, it means that the name does not appear in the array, and so the method returns-1 The second method creates a new string array from names by appending nane at the end returns the new array The third method creates a new int array from counts by appending a new entry 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 ho to enter the votes. Enter the votes, one vote per line. # End with either CTRL-D or an empty line . # Then the program enters a while-loop to receive an indefinite number of votes from the user. The loop can be termined in two possible ways. The condition for the while-loop handles only CTRL-D that is, the condition is: while keyboard.hasNext ) where keyboard is the Scanner object to receive input from the keyboard In the loop-body, the progr receives input from the user using keyboard .nextLine() and stores it in a string variable, name. If name is equal to ", the while-loop should terminate, so the progran executes break. Otherwise, using the find method, it finds the position at which name occurs in the array names If the returned value is nonnegative, the name has already appeared, so the program increases the value of counts at the position that has been found Otherwise, the progran executes addName( names, name ) and addNewCount( count ) to record that the name has appeared just once. happens to be method and stores in . If this

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

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions

Question

LO4 Identify a system for controlling absenteeism.

Answered: 1 week ago