1. [ 6 Points]Using Arrays: Study the following Votes.java and ProcessVotes.java
ID: 3625718 • Letter: 1
Question
1. [ 6 Points]Using Arrays: Study the following Votes.java and ProcessVotes.java programs given and then answer the following questions.a) Copy and paste Votes.java and ProcessVotes.java programs and run the Votes.java program.
Enter following integers when you are asked to enter votes
1 2 3 0 2 1 2 2 3 4 4 0 0 1 0 9
What is the output you are getting?
b) Base on your answer to part (a) above, briefly explain the functionality of the Votes class. You need to clearly explain what while loop does and what switch – case statement does.
c) Observe that the ProcessVotes class’s declareWinner method is a static method. Therefore, in order to access this method, you have to use the class name and then the dot. If the declareWinner method is implemented as a non static method, how to you invoke that method in the Votes class.
*******************************************************************************************************
Here are the programs
1) public class ProcessVotes
{
public static void declareWinner(int[] Choice, String[] Names)
{
int maxVoteIndex = 0;
for(int i = 0 ; i < Names.length ; i++)
{
if(Choice[maxVoteIndex] < Choice[i])
maxVoteIndex = i;
}
// Display the winner
System.out.println("Winner " + Names[maxVoteIndex] + " has obtained"+ Choice[maxVoteIndex] + " votes");
}
}
2)
import java.util.Scanner;
public class Votes
{
public static void main(String [] args){
String candidates[] = {"John", "William", "Jane" , "Bradly", "Brad Hogg"};
int votes[] = {0, 0, 0, 0, 0, 0};
int vote;
Scanner scan = new Scanner(System.in);
// Get the first vote
vote = scan.nextInt();
while (vote >= 0 && vote <= 4)
{
switch(vote)
{
case 0:
votes[0] = votes[0] + 1; // alternatively we can say votes[0]++
break;
case 1:
votes[1] = votes[1] + 1; // alternatively we can say votes[1]++
break;
case 2:
votes[2] = votes[2] + 1; // alternatively we can say votes[1]++
break;
case 3:
votes[3] = votes[3] + 1; // alternatively we can say votes[1]++
break;
}
// read the next vote
vote = scan.nextInt();
}
ProcessVotes.declareWinner(votes,candidates);
}
}
Explanation / Answer
a) Winner John has obtained4 votes
b)
The Votes class reads in a series of integers from the console. Each integer is interpreted as a vote for a particular candidate. The candidates are given in the candidates array. An entry of 0 means a vote for candidate[0], an entry of 1 means a vote for candidate[1], etc. The counts for each candidate are stored in the votes array. votes[0] stores the total votes for candidate[0], votes[1] stores the number of votes for candidate[1], etc. The while loop repeatedly reads in votes until a number is entered which is higher than the number of any candidate. The switch statement adds a vote to the total of the appropriate candidate -- for instance if "1" is entered then votes[1] is increased by one reflecting that one more vote has been cast for candidate[1]. When all the votes have been cast, the declareWinner method of ProcessVotes is invoked to print out the winning candidate.
c) You would need to instantiate a ProcessVotes object and call the method on that object, like this:
ProcessVotes pv = new ProcessVotes();
pv.declareWinner(votes,candidates);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.