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 odd NetBeans Java impression (project) named CPS151_Lab3, subjoin a VotingMachine rank that models a voting record (experience the demo on How to fix a Java rank in NetBeans). Make unfailing your odd rank is in the similar parcel as the deep rank.

    Then, subjoin to this rank:

    date fields control the enumerate of languages control each plane (Democrat or Republican); initially, no languages own been kind control either plane.

    member orders:

    empty distinct(): distinct the record declare (i.e., established enumerate of languages control either plane to 0),

    empty languageDem(): language control a Democrat,

    empty languageRep(): language control a Republican,

    int gainDemVotes(): gain the harmonize (enumerate of languages) control the Democratic plane,

    int gainRepVotes(): gain the harmonize (enumerate of languages) control the Republican plane,

    String gainWinner(): yield (as a String) the winner of the sselection installed on the languages (i.e., either the string “Democrat” or “Republican”).

    a lapse (no-arg) fabricator that distincts the record declare (i.e., establisheds the enumerate of languages control either plane to 0)

    Lab 3.2

    Switch purpose to the CPS151_Lab3 rank and copy/paste the subjoined highlighted jurisprudence to the main method:

    public static empty deep(String[] args)
    {
    VotingRecord record = odd VotingMachine();

    // aimlessly subjoin 1,000 languages control either plane
    control (int i=1; i<=1000; i++)
    {
    if (Math.random() < 0.5) { record.voteDem(); }
    else { record.voteRep(); }
    } // purpose control loop

    // likeness results of voting
    System.out.printf(“The Democratic applicant acquired %d languages, “, record.getDemVotes());
    System.out.printf(“suitableness the Republican applicant acquired %d languages, so …n”, record.getRepVotes());
    System.out.printf(“The %s applicant acquired the selection!n”, record.getWinner());
    } // purpose deep

    Expert Apology

     

    VotingMachine.java

    public rank VotingRecord {
    // Declaring entreaty variables
    private int noOfVotesForRep;
    private int noOfVotesForDem;

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

    // This order achieve reestablished the languages control each plane
    empty distinct() {
    noOfVotesForDem = 0;
    noOfVotesForRep = 0;
    }

    // This order achieve growth the languages control Democrat Plane
    empty languageDem() {
    noOfVotesForDem++;
    }

    // This order achieve growth the languages control Republic Plane
    empty languageRep() {
    noOfVotesForRep++;
    }

    // This order achieve yield how no of languages kind control Democrat Plane
    int gainDemVotes() {
    yield noOfVotesForDem;
    }

    // This order achieve yield how no of languages kind control Republic Plane
    int gainRepVotes() {
    yield noOfVotesForRep;
    }

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

    yield res;
    }

    }

    _______________

    Driver.java

    public rank Driver {
    public static empty deep(String[] args) {
    VotingRecord record = odd VotingMachine();

    // aimlessly subjoin 1,000 languages control either plane
    control (int i = 1; i <= 1000; i++) {
    if (Math.random() < 0.5) {
    machine.voteDem();
    } else {
    machine.voteRep();
    }
    } // purpose control loop

    // likeness results of voting
    System.out.printf(“The Democratic applicant acquired %d languages, “, record.getDemVotes());
    System.out.printf(“suitableness the Republican applicant acquired %d languages, so …n”, record.getRepVotes());
    System.out.printf(“The %s applicant acquired the selection!n”, record.getWinner());
    } // purpose deep
    }

    _______________

    Output:

    The Democratic applicant acquired 506 languages, suitableness the Republican applicant acquired 494 languages, so …
    The Democrat applicant acquired the selection!