Question

Using your favorite IDE (e.g., Eclipse or NetBeans), create a new Java application (project) named CPS151_Lab3,...

Using your favorite IDE (e.g., Eclipse or NetBeans), create a new Java application (project) named CPS151_Lab3, or, if you are using DrJava, create a folder named CPS151_Lab3.

Then, to either your project or folder, add:

  • a main class (if one is not automatically created),
  • a VotingMachine class that models a voting machine. Make sure your new class is in the same package as the main class.

Add to the VotingMachine class:

  • private integer data fields for the number of votes for each party (Democrat or Republican); initially, no votes have been cast for either party (data fields are initialized to 0).
  • public 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 (i.e., add 1 to the number of votes for the Democratic party),
    • void voteRep(): vote for a Republican (i.e., add 1 to the number of votes for the Republican party),
    • 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").

Lab 3.2

Switch back to the main class and copy/paste the following below 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

Homework Answers

Answer #1
Thanks for the question. Here is the simple sort class VotingMachine.java class.
I am embedded the main() method inside and ran, and its working all good:!

Here is the code.

========================================================================================


public class VotingMachine {

    private int democratVotes;
    private int republicVotes;

    public VotingMachine() {
        democratVotes = 0;
        republicVotes = 0;
    }

    public void clear(){
        democratVotes=0;
        republicVotes=0;
        System.out.println("Reset done.");
    }

    public void voteDem(){
        democratVotes+=1;
    }

    public void voteRep(){
        republicVotes+=1;
    }

    public int getDemVotes() {
        return democratVotes;
    }

    public int getRepVotes() {
        return republicVotes;
    }

    public String getWinner(){

        if(democratVotes>republicVotes){
            return "Democrat";
        }else if(democratVotes<republicVotes){
            return "Republican";

        }else{
            return "Neither!";

        }
    }

    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
}

===================================================================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a JAVA project named IA01 that does the following: Display a randomly-determined even number to...
Create a JAVA project named IA01 that does the following: Display a randomly-determined even number to the user The number should be between 2 and 10 inclusive Prompt the user to enter the value of half that number You can assume the user will enter valid numbers, not letters or gibberish They only get one chance to answer per number Tell the user if they are right or wrong Ask the user if they want to try another even number...
Question 3 a) Add a new class named CAS (Contract Academic Staff) to the project. It...
Question 3 a) Add a new class named CAS (Contract Academic Staff) to the project. It must inherit from Professor, but add a new String attribute named term. (The term must be a six digit string, with the year followed by two digits to represent the term: "01" for Winter, "05" for Spring, and "09" for Fall. Thus, 201705 represents the Spring 2017 term. Override print so that it prints CAS data like: McGarrity, Ivan Department: English Term: 201705 Write...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with a Program class and write the following two methods (headers provided) as described below: - A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...