Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 3.1 Starting with a new NetBeans Java application (project) named CPS151_Lab

ID: 3882752 • Letter: L

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

Explanation / Answer

VotingMachine.java

public class VotingMachine {
// Declaring instance variables
private int noOfVotesForRep;
private int noOfVotesForDem;

// Zero argumented constructor
public VotingMachine() {
this.noOfVotesForDem = 0;
this.noOfVotesForRep = 0;
}

// This method will reset the votes for each party
void clear() {
noOfVotesForDem = 0;
noOfVotesForRep = 0;
}

// This method will increase the votes for Democrat Party
void voteDem() {
noOfVotesForDem++;
}

// This method will increase the votes for Republic Party
void voteRep() {
noOfVotesForRep++;
}

// This method will return how no of votes cast for Democrat Party
int getDemVotes() {
return noOfVotesForDem;
}

// This method will return how no of votes cast for Republic Party
int getRepVotes() {
return noOfVotesForRep;
}

// This method will return who is the winner
String getWinner() {
String res = null;
if (noOfVotesForDem > noOfVotesForRep) {
res = "Democrat";
} else if (noOfVotesForDem < noOfVotesForRep) {
res = "Republic";
}

return res;
}

}

_______________

Driver.java

public class Driver {
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
}

_______________

Output:

The Democratic candidate won 506 votes, while the Republican candidate won 494 votes, so ...
The Democrat candidate won the election!

_____________Could you rate me well.Plz .Thank You