
Could somebody help me with this question.
Implement a program to process votes for 5 candidates in a talent contest.
The program should use a String array to hold the names of the 5 candidates and an integer array to record the number of votes for each contestant.

import java.io.*;
import java.util.*;
class Candidate{
public String name;
public int vote ;
public Candidate(){}
public Candidate(String name,int vote) {
super();
this.name = name;
this.vote=vote;
}
public String getName(){
return name;
}
public int getVote() {
return vote;
}
}
class VoteComparator implements Comparator{
public int compare(Object can1, Object can2){
int vote1 = ((Candidate)can1).getVote();
int vote2 = ((Candidate)can2).getVote();
if(vote1 > vote2)
return 1;
else if(vote1 < vote2)
return -1;
else
return 0;
}
}
public class ProcessVotes{
public static void main(String[] args) throws Exception{
List<Candidate> list = new ArrayList<Candidate>();
list.add(new Candidate("A",10000));
list.add(new Candidate("B",20000));
list.add(new Candidate("C",15000));
list.add(new Candidate("D",12000));
list.add(new Candidate("E",17000));
System.out.println(" ");
int v=0;
String n="";
Collections.sort(list,new VoteComparator());
for(Candidate data: list){
v=data.getVote();
n=data.getName();
System.out.println(data.getName()+" \t "+data.getVote());
}
System.out.println(n+" receives highest vote i.e. "+v);
}
}

import java.io.*;
import java.util.*;
class Candidate{
public String name;
public int vote ;
public Candidate(){}
public Candidate(String name,int vote) {
super();
this.name = name;
this.vote=vote;
}
public String getName(){
return name;
}
public int getVote() {
return vote;
}
}
class VoteComparator implements Comparator{
public int compare(Object can1, Object can2){
int vote1 = ((Candidate)can1).getVote();
int vote2 = ((Candidate)can2).getVote();
if(vote1 > vote2)
return 1;
else if(vote1 < vote2)
return -1;
else
return 0;
}
}
public class ProcessVotes{
public static void main(String[] args) throws Exception{
List<Candidate> list = new ArrayList<Candidate>();
list.add(new Candidate("A",10000));
list.add(new Candidate("B",20000));
list.add(new Candidate("C",15000));
list.add(new Candidate("D",12000));
list.add(new Candidate("E",17000));
System.out.println(" ");
int v=0;
String n="";
Collections.sort(list,new VoteComparator());
for(Candidate data: list){
v=data.getVote();
n=data.getName();
System.out.println(data.getName()+" \t "+data.getVote());
}
System.out.println(n+" receives highest vote i.e. "+v);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.