Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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_2

Step: 3

blur-text-image_3

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

Bioinformatics Databases And Systems

Authors: Stanley I. Letovsky

1st Edition

1475784058, 978-1475784053

More Books

Students also viewed these Databases questions

Question

Explain what a keyword rsum is.

Answered: 1 week ago

Question

Evaluate three pros and three cons of e-prescribing

Answered: 1 week ago

Question

6. Discuss the steps involved in conducting a task analysis.

Answered: 1 week ago