Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions

Question

2. Why has the conflict escalated?

Answered: 1 week ago

Question

1. What might have led to the misinformation?

Answered: 1 week ago