Question: 1. Using your favorite IDE (e.g., Eclipse, NetBeans, etc.), create a new Java application (project), and add: a main class (if one is not automatically

1. Using your favorite IDE (e.g., Eclipse, NetBeans, etc.), create a new Java application (project), and add:

  • a main class (if one is not automatically created),
  • a VotingMachine class that models a voting machine. Make sure your new class is in the same package as the main class.

Then, add to the VotingMachine class:

  • private integer data fields for the number of votes for each party (Democrat or Republican); initially, no votes have been cast for either party (data fields are initialized to 0).
  • public member methods:
    • void clear(): clear the machine state (i.e., set number of votes for either party to 0),
    • void voteDem(): vote for a Democrat (i.e., add 1 to the number of votes for the Democratic party),
    • void voteRep(): vote for a Republican (i.e., add 1 to the number of votes for the Republican party),
    • int getDemVotes(): get the tally (number of votes) for the Democratic party,
    • int getRepVotes(): get the tally (number of votes) for the Republican party,
    • String getWinner(): return (as a String) the winner of the election based on the votes (i.e., either the string "Democrat" or "Republican") If there is a tie, it is up to you as to what should be output.

2. Switch back to the main class and copy/paste the following highlighted code to the main method:

public static void main(String[] args) { VotingMachine machine = new VotingMachine(); // randomly add 1,000 votes for either party for (int i=1; i<=1000; i++) { if (Math.random() < 0.5) { machine.voteDem(); } else { machine.voteRep(); } } // end for loop // show results of voting System.out.printf("The Democratic candidate won %d votes, ", machine.getDemVotes()); System.out.printf("while the Republican candidate won %d votes, so ... ", machine.getRepVotes()); System.out.printf("The %s candidate won the election! ", machine.getWinner()); } // end main

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!