Question
My code has no syntax error however I am not printing the correct output seen on the tester class. I don't know what the issue
My code has no syntax error however I am not printing the correct output seen on the tester class. I don't know what the issue is.
public class VotingMachine { /** * Create 4 instance variables. Two are of type String and * each stores the name of a political party * Two are of type int and they store the number of votes for * each political party */ private String Republican; private String Democrat; private int RepVotes; private int DemVotes; /** Create a constructor method that has 2 String arguments which are used to initialize the name of each political party Set the number of votes for each party to 0 */ public VotingMachine(String Republican, String Democrat) { this.Republican = Republican; this.Democrat = Democrat; this.RepVotes = 0; this.DemVotes = 0; } /** Create a public method voteParty1() which adds one vote for political party 1 */ public void voteParty1() {
this.DemVotes++;
} /** Create a public method voteParty2() which adds one vote for political party 2 */ public void voteParty2() {
this.RepVotes++;
} /** * Create a public method newElection() that sets the current number of votes for * each political party to 0 */ public void newElection() { this.RepVotes = 0; this.DemVotes = 0; } /** * Create a public method getWinner() that returns a String. * If party 1 has more votes than party 2 then return the string * containing: the name of party 1 followed by " win the election with " * followed by the number of votes for party 1 followed by " Votes" * * If party 2 has more votes than party 1 then return the string * containing: the name of party 2 followed by " win the election with " * followed by the number of votes for party 2 followed by " Votes" * * If party 2 has the same votes as party 1 then return the string * containing: the name of party 1 followed by " and " followed by * the name of party 2 followed by " tied with " followed by the * number of votes for party 1 */ public String getWinner() { if(this.DemVotes > this.RepVotes) { return this.Democrat + " win the election with " + this.DemVotes; } else if(this.RepVotes > this.DemVotes) {
return this.Republican + " win the election with " + this.RepVotes; } else if(this.DemVotes == this.RepVotes) { return this.Democrat + " and " + this.Republican + " tied with " + this.DemVotes; } return Democrat; } }
import java.util.Random;
public class VotingMachineTester { public static void main(String[] args) { VotingMachine vm = new VotingMachine("Democrats","Republicans"); int votes1 = 7736; for (int i = 0; i < votes1; i++) vm.voteParty1(); int votes2 = 7624; for (int i = 0; i < votes2; i++) vm.voteParty2(); // Expected Output: // Party1 votes 7736 // Party2 votes 7624 // Democrats win the election with 7736 Votes System.out.println("Party1 votes " + votes1); System.out.println("Party2 votes " + votes2); System.out.println(vm.getWinner()); // Expected Output: // Democrats and Republicans tied with 1 vm.newElection(); vm.voteParty1(); vm.voteParty2(); System.out.println(vm.getWinner()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started