So I'm having trouble with my printResults
method in my TestCandidate
class. Everything seems to run as expected, except for the column displaying the percent of total votes each candidate received. For some reason, that operation is only printing 0's. (For the record, when i use the word "operation," I'm mainly referring to the list[i].getVotes / getTotal(list)
operation. Just to clarify).
I've tried various combinations of parenthesizing the list[i].getVotes
method and the getTotal(list)
method but to no avail. I've also tried performing the operation outside of the print statement and just printing a variable that the operation would have been assigned to.
The strange thing is, when I take either the list[i].getVotes
or the getTotal(list)
out and just leave one, they both print fine. Just not together and I just can't figure out why.
So, in summary, why won't list[i].getVotes / getTotal(list)
divide properly? (Please do not try to do anything but what the question asks for, I'd like to learn and fix my own mistakes and best optimize my code as much as possible but I simply cannot get past this one for whatever reason). Thank you!
Here's the Candidate
class:
public class Candidate
{
private int votes;
private String candidateName;
public Candidate(String name, int numVotes)
{
votes = numVotes;
candidateName = name;
}
public String getName()
{
return candidateName;
}
public void setName(String name)
{
candidateName = name;
}
public int getVotes()
{
return votes;
}
public void setVotes(int numVotes)
{
votes = numVotes;
}
public String toString()
{
return candidateName + " received:\t" + votes + " votes.";
}
}
Here's the TestCandidate
class:
public class TestCandidate
{
public static void main(String[] args)
{
Candidate[] election = new Candidate[5];
election[0] = new Candidate("John Smith", 5000);
election[1] = new Candidate("Mary Miller", 4000);
election[2] = new Candidate("Michael Duffy", 6000);
election[3] = new Candidate("Tim Robinson", 2500);
election[4] = new Candidate("Joe Ashtony", 1800);
printVotes(election);
System.out.println();
printResults(election);
System.out.println();
System.out.println("The total amount of votes cast was: " + getTotal(election) + ".");
}
public static <E> void printVotes(E[] array) //prints the candidates and the amount of votes they
{ //each received
for(E element : array)
{
System.out.printf("%s ", element);
System.out.println();
}
}
public static int getTotal(Candidate[] list) //adds up the votes from each candidate and prints the
{ //total amount
int totalVotes = 0;
for(int i = 0; i < list.length; i++)
{
totalVotes += list[i].getVotes();
}
return totalVotes;
}
public static void printResults(Candidate[] list) //prints a table containing candidate's name, amount of votes, and the
{ //percentage of total votes they earned
System.out.println("Candidate: \t Votes Recieved: \t Percent of Total Votes:");
for(int x = 0; x < list.length; x++)
{
System.out.println(list[x].getName() + " \t " + list[x].getVotes() + " \t \t \t " + list[x].getVotes()/getTotal(list) + "%");
}
}
}
And here's my current output:
John Smith received: 5000 votes.
Mary Miller received: 4000 votes.
Michael Duffy received: 6000 votes.
Tim Robinson received: 2500 votes.
Joe Ashtony received: 1800 votes.
Candidate: Votes Recieved: Percent of Total Votes:
John Smith 5000 0%
Mary Miller 4000 0%
Michael Duffy 6000 0%
Tim Robinson 2500 0%
Joe Ashtony 1800 0%
The total amount of votes cast was: 19300.