Question
public class VotingMachine { /* * party1Name and party2Name each store the name of a political party * numVotes1 and numVotes2 are used to record
public class VotingMachine { /* * party1Name and party2Name each store the name of a political party * numVotes1 and numVotes2 are used to record number of votes for each political party */ String party1Name; String party2Name; int numVotes1, numVotes2;
/** 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 */ //-----------Start below here. To do: approximate lines of code = 4 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
//-----------Start below here. To do: approximate lines of code = 8 // Create accessor methods for each instance variable (i.e. "getters") //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
/** Create a public method voteParty1() which adds one vote for political party 1 */ //-----------Start below here. To do: approximate lines of code = 2 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
/** Create a public method voteParty2() which adds one vote for political party 2 */ //-----------Start below here. To do: approximate lines of code = 2 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
/** * Create a public method newElection() that sets the current number of votes for * each political party to 0 */ //-----------Start below here. To do: approximate lines of code = 2 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
/** * 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 followed by " votes" */ //-----------Start below here. To do: approximate lines of code = 7 // //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions. }
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(); System.out.println("Party1 votes " + vm.getNumVotes1()); System.out.println("Party2 votes " + vm.getNumVotes2()); System.out.println(vm.getWinner()); System.out.println("Expected: Party1 votes 7736 Party2 votes 7624 Democrats win the election with 7736 votes"); vm.newElection(); vm.voteParty1();vm.voteParty1(); vm.voteParty2();vm.voteParty2(); System.out.println("Party1 votes " + vm.getNumVotes1()); System.out.println("Party2 votes " + vm.getNumVotes2()); System.out.println(vm.getWinner()); System.out.println("Expected: Party1 votes 2 Party2 votes 2 Democrats and Republicans tied with 2 votes"); } }
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