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 upstart NetBeans Java collision (project) named CPS151_Lab3, subjoin a VotingMachine arrange that models a voting medium (view the demo on How to specify a Java arrange in NetBeans). Make purposeuring your upstart arrange is in the corresponding load as the ocean arrange.

    Then, subjoin to this arrange:

    date fields coercion the compute of suffrages coercion each face (Democrat or Republican); initially, no suffrages entertain been mould coercion either face.

    member systems:

    unoccupied bright(): bright the medium avow (i.e., regular compute of suffrages coercion either face to 0),

    unoccupied suffrageDem(): suffrage coercion a Democrat,

    unoccupied suffrageRep(): suffrage coercion a Republican,

    int acquireDemVotes(): acquire the coincide (compute of suffrages) coercion the Democratic face,

    int acquireRepVotes(): acquire the coincide (compute of suffrages) coercion the Republican face,

    String acquireWinner(): requite (as a String) the winner of the sselection inveterate on the suffrages (i.e., either the string “Democrat” or “Republican”).

    a delinquency (no-arg) doer that brights the medium avow (i.e., regulars the compute of suffrages coercion either face to 0)

    Lab 3.2

    Switch purpose to the CPS151_Lab3 arrange and copy/paste the aftercited highlighted code to the main method:

    public static unoccupied ocean(String[] args)
    {
    VotingMedium medium = upstart VotingMachine();

    // chancely subjoin 1,000 suffrages coercion either face
    coercion (int i=1; i<=1000; i++)
    {
    if (Math.random() < 0.5) { medium.voteDem(); }
    else { medium.voteRep(); }
    } // purpose coercion loop

    // likeness results of voting
    System.out.printf(“The Democratic solicitor acquired %d suffrages, “, medium.getDemVotes());
    System.out.printf(“while the Republican solicitor acquired %d suffrages, so …n”, medium.getRepVotes());
    System.out.printf(“The %s solicitor acquired the selection!n”, medium.getWinner());
    } // purpose ocean

    Expert Reply

     

    VotingMachine.java

    public arrange VotingMedium {
    // Declaring exemplification variables
    private int noOfVotesForRep;
    private int noOfVotesForDem;

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

    // This system achieve reregular the suffrages coercion each face
    unoccupied bright() {
    noOfVotesForDem = 0;
    noOfVotesForRep = 0;
    }

    // This system achieve extension the suffrages coercion Democrat Face
    unoccupied suffrageDem() {
    noOfVotesForDem++;
    }

    // This system achieve extension the suffrages coercion Republic Face
    unoccupied suffrageRep() {
    noOfVotesForRep++;
    }

    // This system achieve requite how no of suffrages mould coercion Democrat Face
    int acquireDemVotes() {
    requite noOfVotesForDem;
    }

    // This system achieve requite how no of suffrages mould coercion Republic Face
    int acquireRepVotes() {
    requite noOfVotesForRep;
    }

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

    requite res;
    }

    }

    _______________

    Driver.java

    public arrange Driver {
    public static unoccupied ocean(String[] args) {
    VotingMedium medium = upstart VotingMachine();

    // chancely subjoin 1,000 suffrages coercion either face
    coercion (int i = 1; i <= 1000; i++) {
    if (Math.random() < 0.5) {
    machine.voteDem();
    } else {
    machine.voteRep();
    }
    } // purpose coercion loop

    // likeness results of voting
    System.out.printf(“The Democratic solicitor acquired %d suffrages, “, medium.getDemVotes());
    System.out.printf(“while the Republican solicitor acquired %d suffrages, so …n”, medium.getRepVotes());
    System.out.printf(“The %s solicitor acquired the selection!n”, medium.getWinner());
    } // purpose ocean
    }

    _______________

    Output:

    The Democratic solicitor acquired 506 suffrages, while the Republican solicitor acquired 494 suffrages, so …
    The Democrat solicitor acquired the selection!