Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a METHOD in JAVA that allows the user to enter the last names of five cand

ID: 3879227 • Letter: W

Question

Write a METHOD in JAVA that allows the user to enter the last names of five candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, the votes received by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output would be: Candidate Votes Received % of Total Votes Johnson 5000 25.91 Miller 4000 20.73 Wilson 6000 31.09 Robinson 2500 12.95 Abbot 1800 9.33 Total 19300 The winner of the election is Wilson. DO NOT USE ARRAYS PLEASE.

Explanation / Answer

/*The code is well commented so please copy whole code in your ide and execute

* To solve this Program , I will create a class Candidate that that will store

* candidate name, number of votes and vote share

*

* so we can store these five candidate in array, as you mentioned do not use array

* so i will use five different object to store the candidate

* And Scanner class to read input from console

* Sample Input:

Johnson 5000

Miller 4000

Wilson 6000

Robinson 2500

Abbot 1800

  

*Sample output:

Johnson 5000 25.906735751295333

Miller 4000 20.72538860103627

Wilson 6000 31.088082901554404

Robinson 2500 12.953367875647666

Abbot 1800 9.32642487046632

Total: 19300

The winner of the election is Wilson

*This solution is too long because I didn't use array

*If you need any modification please let me know in comment*/

import java.util.*;

import java.io.*;

//Class for candidate

class Candidate{

String lname;

int numberOfVotes;

double voteShare;

//constructor to initialize member variables

public Candidate(String lname,int numberOfvotes)

{

this.lname=lname;

this.numberOfVotes=numberOfvotes;

this.voteShare=0;

}

}

public class ElectionResult {

//Method to receive input and displaying result

public void printResult()

{

//Creating object of Scanner class

Scanner scan=new Scanner(System.in);

String lname="";

int numOfVotes=0;

//to store total votes

int totalVotes=0;

//Reading data for first candidate

lname=scan.next();

numOfVotes=scan.nextInt();

//creating object for candidate

Candidate candidate1=new Candidate(lname,numOfVotes);

//Similarly for others

//Second

lname=scan.next();

numOfVotes=scan.nextInt();

Candidate candidate2=new Candidate(lname,numOfVotes);

//Third

lname=scan.next();

numOfVotes=scan.nextInt();

Candidate candidate3=new Candidate(lname,numOfVotes);

//Fourth

lname=scan.next();

numOfVotes=scan.nextInt();

Candidate candidate4=new Candidate(lname,numOfVotes);

//Fifth

lname=scan.next();

numOfVotes=scan.nextInt();

Candidate candidate5=new Candidate(lname,numOfVotes);

//Now calculating total votes

totalVotes =candidate1.numberOfVotes+candidate2.numberOfVotes+candidate3.numberOfVotes+candidate4.numberOfVotes+candidate5.numberOfVotes;

//Now calculating vote share

candidate1.voteShare=((double)candidate1.numberOfVotes/totalVotes)*100;

candidate2.voteShare=((double)candidate2.numberOfVotes/totalVotes)*100;

candidate3.voteShare=((double)candidate3.numberOfVotes/totalVotes)*100;

candidate4.voteShare=((double)candidate4.numberOfVotes/totalVotes)*100;

candidate5.voteShare=((double)candidate5.numberOfVotes/totalVotes)*100;

//Now to find the winner name , we need to find the candidate with maximum votes

int maxVotes=0;

String winnerName="";

if(candidate1.numberOfVotes>maxVotes)

{

maxVotes=candidate1.numberOfVotes;

winnerName=candidate1.lname;

}

if(candidate2.numberOfVotes>maxVotes)

{

maxVotes=candidate2.numberOfVotes;

winnerName=candidate2.lname;

}

if(candidate3.numberOfVotes>maxVotes)

{

maxVotes=candidate3.numberOfVotes;

winnerName=candidate3.lname;

}

if(candidate4.numberOfVotes>maxVotes)

{

maxVotes=candidate4.numberOfVotes;

winnerName=candidate4.lname;

}

if(candidate5.numberOfVotes>maxVotes)

{

maxVotes=candidate5.numberOfVotes;

winnerName=candidate5.lname;

}

//Printing final result

System.out.println(candidate1.lname+" "+candidate1.numberOfVotes+" "+candidate1.voteShare);

System.out.println(candidate2.lname+" "+candidate2.numberOfVotes+" "+candidate2.voteShare);

System.out.println(candidate3.lname+" "+candidate3.numberOfVotes+" "+candidate3.voteShare);

System.out.println(candidate4.lname+" "+candidate4.numberOfVotes+" "+candidate4.voteShare);

System.out.println(candidate5.lname+" "+candidate5.numberOfVotes+" "+candidate5.voteShare);

System.out.println("Total: "+totalVotes);

System.out.println("The winner of the election is "+winnerName);

scan.close();

}

public static void main(String[] args) {

ElectionResult electionResult=new ElectionResult();

//Calling method

electionResult.printResult();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote