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
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