Question: Lab 3.1 Starting with a new NetBeans Java application (project) named CPS151_Lab3 , add a VotingMachine class that models a voting machine (see the demo

Lab 3.1

Starting with a new NetBeans Java application (project) named CPS151_Lab3, add a VotingMachine class that models a voting machine (see the demo on How to define a Java class in NetBeans). Make sure your new class is in the same package as the main class.

Then, add to this class:

date fields for the number of votes for each party (Democrat or Republican); initially, no votes have been cast for either party.

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,

void voteRep(): vote for a Republican,

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").

a default (no-arg) constructor that clears the machine state (i.e., sets the number of votes for either party to 0)

Lab 3.2

Switch back to the CPS151_Lab3 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!