-1

For a school assignment I made a votingmachine that counts votes and prints them out. Here is the VotingMachine Class with all its methods.

public class VotingMachine {
//declaring instance variables
private int democratVotes;
private int republicanVotes;
private int independentVotes;
//default constructor
public VotingMachine(){
}

//parameter constructor for changing values
public VotingMachine(int demVotes, int repubVotes, int indeVotes){
this.democratVotes = demVotes;
this.republicanVotes = repubVotes;
this.independentVotes = indeVotes;
}
//getter methods
public int getDemocratVotes(){
return democratVotes;
}
public int getRepublicanVotes(){
return republicanVotes;
}
public int getIndependentVotes(){
return independentVotes;
}
//setter methods
public void setDemocratVotes(int votes){
this.democratVotes = votes;
}
public void setRepublicanVotes(int votes){
this.republicanVotes = votes;
}
public void setIndependentVotes(int votes){
this.independentVotes = votes;
}
//increment votes by 1 methods
public void voteDemocrat(){
democratVotes += 1;
}
public void voteRepublican(){
republicanVotes += 1;
}
public void Independent(){
independentVotes += 1;
}
//overloaded methods (methods with same names as above)
public void voteDemocrat(int votes){
democratVotes += votes;
}
public void voteRepublican(int votes){
republicanVotes += votes;
}
public void voteIndependent(int votes){
independentVotes += votes;
}
//method to clear all votes
public void clearVote(){
democratVotes = 0;
republicanVotes = 0;
independentVotes = 0;
}
//method to calculate total votes
public int totalVotes(){
int totalVotes = democratVotes + republicanVotes + independentVotes;
return totalVotes;
}
//methods to calculate percentages while calling totalVotes()
public double percentDemocrat(){
double percentDemocrat = democratVotes / totalVotes();
return percentDemocrat;
}
public double percentRepublican(){
double percentRepublican = republicanVotes / totalVotes();
return percentRepublican;
}
public double percentIndependent(){
double percentIndependent = independentVotes / totalVotes();
return percentIndependent;
}

//method to print statistics
public void percentStats(){
System.out.println("Democrats: " + democratVotes);
System.out.println("Republicans: " + republicanVotes);
System.out.println("Independent: " + independentVotes);
System.out.println("Total Votes: " + totalVotes());
System.out.println("Percent Democrat Votes: " + percentDemocrat());
System.out.println("Percent Republican Votes: " + percentRepublican());
System.out.println("Percent Independent Votes: " + percentRepublican());
}
}

This is the driver that I am using for the project:

public class MachineDriver {

    public static void main(String[] args) {
        VotingMachine test = new VotingMachine();
        test.voteDemocrat();
        test.voteDemocrat();
        test.voteRepublican();
        test.voteIndependent();
        test.percentStats();
    }
}

However, when I run the program the percentDemocrat, percentRepublican, and percentIndependent all show up as 0.0. What did I do wrong? Here is what shows up in the console:

Democrats: 2 Republicans: 1 Independent: 1 Total Votes: 4 Percent Democrat Votes: 0.0 Percent Republican Votes: 0.0 Percent Independent Votes: 0.0

blakcy88
  • 39
  • 5
  • In `percentDemocrat()` and the other methods, the numbers you are using for the math are `int`s. You need to cast them to `float`s (or `double`s) to get correct floating-point answers. For example: `double percentDemocrat = (double)democratVotes / (double)totalVotes();` – Frecklefoot Sep 08 '17 at 16:13

1 Answers1

1

You are dividing integers that's why you getting result equal to zero.

You can cast integer to double

public double percentDemocrat(){
    double percentDemocrat = ((double)democratVotes) / totalVotes();
    return percentDemocrat;
}
Abhilekh Singh
  • 2,845
  • 2
  • 18
  • 24