Homework Solution: Starting with a new NetBeans Java application (project) named CPS151_Lab3, add a VotingMachine class th…

    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 ...n", machine.getRepVotes()); System.out.printf("The %s candidate won the election!n", machine.getWinner()); } // end main

    Expert Answer

     
    VotingMachine.java public class VotingMachine {

    Lab 3.1

    Starting with a novel NetBeans Java contact (project) named CPS151_Lab3, subjoin a VotingMachine collocate that models a voting record (attobject the demo on How to controlmaltle a Java collocate in NetBeans). Make indisputable your novel collocate is in the selfselfsame load as the deep collocate.

    Then, subjoin to this collocate:

    date fields control the sum of expressions control each cause (Democrat or Republican); initially, no expressions keep been kind control either cause.

    member systems:

    wanting obvious(): obvious the record declare (i.e., controlmal sum of expressions control either cause to 0),

    wanting expressionDem(): expression control a Democrat,

    wanting expressionRep(): expression control a Republican,

    int earnDemVotes(): earn the harmonize (sum of expressions) control the Democratic cause,

    int earnRepVotes(): earn the harmonize (sum of expressions) control the Republican cause,

    String earnWinner(): produce (as a String) the winner of the choice fixed on the expressions (i.e., either the string “Democrat” or “Republican”).

    a failure (no-arg) committer that obviouss the record declare (i.e., controlmals the sum of expressions control either cause to 0)

    Lab 3.2

    Switch object to the CPS151_Lab3 collocate and copy/paste the restraintthcoming highlighted edict to the main method:

    public static wanting deep(String[] args)
    {
    VotingRecord record = novel VotingMachine();

    // strayly subjoin 1,000 expressions control either cause
    control (int i=1; i<=1000; i++)
    {
    if (Math.random() < 0.5) { record.voteDem(); }
    else { record.voteRep(); }
    } // object control loop

    // appearance results of voting
    System.out.printf(“The Democratic applicant acquired %d expressions, “, record.getDemVotes());
    System.out.printf(“period the Republican applicant acquired %d expressions, so …n”, record.getRepVotes());
    System.out.printf(“The %s applicant acquired the choice!n”, record.getWinner());
    } // object deep

    Expert Defense

     

    VotingMachine.java

    public collocate VotingRecord {
    // Declaring illustration variables
    private int noOfVotesForRep;
    private int noOfVotesForDem;

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

    // This system conquer reformal the expressions control each cause
    wanting obvious() {
    noOfVotesForDem = 0;
    noOfVotesForRep = 0;
    }

    // This system conquer acception the expressions control Democrat Cause
    wanting expressionDem() {
    noOfVotesForDem++;
    }

    // This system conquer acception the expressions control Republic Cause
    wanting expressionRep() {
    noOfVotesForRep++;
    }

    // This system conquer produce how no of expressions kind control Democrat Cause
    int earnDemVotes() {
    produce noOfVotesForDem;
    }

    // This system conquer produce how no of expressions kind control Republic Cause
    int earnRepVotes() {
    produce noOfVotesForRep;
    }

    // This system conquer produce who is the winner
    String earnWinner() {
    String res = null;
    if (noOfVotesForDem > noOfVotesForRep) {
    res = “Democrat”;
    } else if (noOfVotesForDem < noOfVotesForRep) {
    res = “Republic”;
    }

    produce res;
    }

    }

    _______________

    Driver.java

    public collocate Driver {
    public static wanting deep(String[] args) {
    VotingRecord record = novel VotingMachine();

    // strayly subjoin 1,000 expressions control either cause
    control (int i = 1; i <= 1000; i++) {
    if (Math.random() < 0.5) {
    machine.voteDem();
    } else {
    machine.voteRep();
    }
    } // object control loop

    // appearance results of voting
    System.out.printf(“The Democratic applicant acquired %d expressions, “, record.getDemVotes());
    System.out.printf(“period the Republican applicant acquired %d expressions, so …n”, record.getRepVotes());
    System.out.printf(“The %s applicant acquired the choice!n”, record.getWinner());
    } // object deep
    }

    _______________

    Output:

    The Democratic applicant acquired 506 expressions, period the Republican applicant acquired 494 expressions, so …
    The Democrat applicant acquired the choice!