Problem A.1 Midterm elections are here! Help your local election commission by c
ID: 3570141 • Letter: P
Question
Problem A.1
Midterm elections are here! Help your local election commission by counting votes and telling them the winner. If more than one candidate ties with the most votes, then print out all of their names in alphabetical order.
Input
Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. Each test case will begin with an integer n (1 <= n <= 1000), indicating the number of votes. The next n lines will hold the votes. The candidates
Explanation / Answer
Program code:
//Candidate.java that contains only name and count variables
public class Candidate
{
String elected_name;
int count_of_votes;
Candidate(String name, int count)
{
elected_name=name;
count_of_votes=count;
}
public String getName()
{
return elected_name;
}
public int getCount()
{
return count_of_votes;
}
}
//CountVotings.java
import java.util.*;
public class CountVotings
{
static ArrayList<Candidate> elections;
public static void main(String args[])
{
//Scanner object
Scanner input=new Scanner(System.in);
//string of nomanies names
String s[]={"Ronald Rex", "Jackel Joe","Andria Pearson","Leo Cliper","Xing Cha"};
String choice;
int num_of_votes;
//loop to test till the user enters other than y
do{
//prompt the user to enter the total votes
System.out.println("Enter total number of votes: ");
num_of_votes=input.nextInt();
//create an array list of Candidate type
elections=new ArrayList<Candidate>();
//check whether the input is within 1 to 1000
if(num_of_votes>=1 && num_of_votes <=1000)
{
//if within the limits computes the votes
for(int i=0;i<s.length;i++)
{
addVotes(s[i],num_of_votes );
}
}
//if the condition is false display an error message
else
{
System.out.println("Number of votes exceeded. Try again.");
}
//sort the array according to voitings
Collections.sort(elections, new Comparator<Candidate>()
{
public int compare(Candidate cand1, Candidate cand2)
{
return (int) (cand1.getCount() - cand2.getCount());
}
});
//display the names
displayCandidates();
System.out.println("Would like to continue? Press y or Y");
choice=input.next();
}while(choice.equalsIgnoreCase("Y"));
}
//addVotes method
public static void addVotes(String s, int num_of_votes)
{
int candidates=(int)(Math.random()*num_of_votes);
Candidate c=new Candidate(s, candidates);
elections.add(c);
}
//displayCandidates method
public static void displayCandidates()
{
for(int i=elections.size()-1;i>=0;i--)
{
System.out.println(elections.get(i).getName().toUpperCase()+" "+elections.get(i).getCount());
}
}
}
---------------------------------------------------------------------------------------
Sample output:
Enter total number of votes:
1000
JACKEL JOE 986
ANDRIA PEARSON 649
RONALD REX 580
XING CHA 441
LEO CLIPER 193
Would like to continue? Press y or Y
y
Enter total number of votes:
825
LEO CLIPER 748
RONALD REX 496
ANDRIA PEARSON 423
JACKEL JOE 304
XING CHA 41
Would like to continue? Press y or Y
y
Enter total number of votes:
1500
Number of votes exceeded. Try again.
Would like to continue? Press y or Y
n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.