AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-basics: WAP to find the election winner

The results from the presidential election for ABC Party have been reported by each district and stored in 4 one-dimensional arrays as follows:

District Candidate
(Aaron)
1 192
2 147
3 186
4 114
5 267

District Candidate
(Baker)
1 48
2 90
3 12
4 21
5 13
District Candidate
(Caden)
1 206
2 312
3 121
4 408
5 382

District Candidate
(Daena)
1 37
2 21
3 38
4 39
5 29

Write a program to do the following:
a. Display the tables with appropriate labels for rows and columns.
b. Compute and display the total numbers of votes received by each candidate and the percentage of the total votes cast.
c. If any one candidate received over 50% of the votes, the program should display a message declaring that the candidate is the winner.
d. If no candidate received 50% of the votes, the program should display a message declaring that a runoff between the two candidates receiving the highest number of votes; the two candidates should be identified by their names.
e. Run the program once with the data shown and once with candidate Caden receiving only 108 votes in district 4.

corejava x 353
basics x 171
Posted On : 2018-11-25 22:20:40.0
profile mirachan - anyforum.in mirachan
400
up-rate
4
down-rate

Answers


First let´s create a bean class as Candidate.java to hold the data:

Candidate.java:
-----------------------------------
public class Candidate {

private String name;
private int[] votes;
private String record;
private double percentage;
private int totalVotes;

/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the votes
*/
public int[] getVotes() {
return votes;
}
/**
* @param votes the votes to set
*/
public void setVotes(int[] votes) {
this.votes = votes;
}
/**
* @return the percentage
*/
public double getPercentage() {
return percentage;
}
/**
* @param percentage the percentage to set
*/
public void setPercentage(double percentage) {
this.percentage = percentage;
}
/**
* @return the totalVotes
*/
public int getTotalVotes() {
return totalVotes;
}
/**
* @param totalVotes the totalVotes to set
*/
public void setTotalVotes(int totalVotes) {
this.totalVotes = totalVotes;
}
/**
* @return the record
*/
public String getRecord() {
return record;
}
/**
* @param record the record to set
*/
public void setRecord(String record) {
this.record = record;
}

}


VoteCounter.java:
--------------------------------------
import java.util.Scanner;

public class VoteCounter {
private static int noOfCandidates=4;
public static Candidate[] candidates=new Candidate[noOfCandidates];
public static void main(String[] args) throws Exception{
Candidate candidate = null;
int[] AaronVotes=new int[]{192,147,186,114,267};
int[] BakerVotes=new int[]{48,90,12,21,13};
int[] CadenVotes=new int[]{206,312,121,408,382};
int[] DaenaVotes=new int[]{37,21,38,39,21};
double maxPercentage=0.00;
Candidate winner=null;
int noOfAreas=AaronVotes.length;
StringBuilder record=null;
int total=0;
int grandTotal=0;
StringBuilder header=new StringBuilder();

header.append("Name");
for(int i=1;i<=noOfAreas;i++){
header.append("\tArea"+i);
}
header.append("\ttotal");
header.append("\t%");
System.out.println(header);

for(int i=1; i<=noOfCandidates; i++){
total=0;
candidate= new Candidate();
switch (i) {
case 1:
candidate.setName("Aaron");
candidate.setVotes(AaronVotes);
break;
case 2:
candidate.setName("Baker");
candidate.setVotes(BakerVotes);
break;
case 3:
candidate.setName("Caden");
candidate.setVotes(CadenVotes);
break;
case 4:
candidate.setName("Daena");
candidate.setVotes(DaenaVotes);
break;
}
record = new StringBuilder(candidate.getName());
for(int votes:candidate.getVotes()){
total=total+votes;
record.append("\t"+votes);
}
record.append("\t"+total);
grandTotal=grandTotal+total;
candidate.setTotalVotes(total);
candidate.setRecord(record.toString());
candidates[i-1]=candidate;
}
double votePercentage=0.00;
for(Candidate cand: candidates){
votePercentage=cand.getTotalVotes()*100/grandTotal;
if(votePercentage>maxPercentage){
maxPercentage=votePercentage;
winner=cand;
}
cand.setPercentage(votePercentage);
cand.setRecord(cand.getRecord() + "\t"+votePercentage);
System.out.println(cand.getRecord());
}
System.out.println("Do you want to compute the final results: Y/N");
Scanner input= new Scanner(System.in);
try{
if(input.nextLine().equalsIgnoreCase("Y")){
if(null!=winner){
if(winner.getPercentage()>50.00)
System.out.println(winner.getName()+" is the winner");
else
System.out.println("a runoff between the two candidates receiving the highest number of votes; the two candidates should be identified by their names.");
}
}else{
throw new Exception("Invalid choice!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
input.close();
}
}
}

Posted On : 2018-11-30 17:58:28
Satisfied : 0 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188222642
Reply This Thread
up-rate
0
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in