Question

In: Computer Science

Implement a VotingMachine class that can be used for a simple election. Include the methods to...

Implement a VotingMachine class that can be used for a simple election. Include the methods to voteForDemocrat and voteForRepublican. Add a method to clear out all votes. Additionally, add a method to print the results.

Write the driver code to simulate the voting. in java

Solutions

Expert Solution

import java.util.*;

public class VotingMachine {
    
    private int democrateVote;
    private int republicanVote;
    
    // Create a class constructor for the VotingMachine class
    public VotingMachine(){
        democrateVote = 0;
        republicanVote = 0;
    }
    
    // Increases the count of democrate vote
    private void voteForDemocrat(){
        democrateVote++;
    }
    
    // Increases the count for republican vote
    private void voteForRepublican(){
        republicanVote++;
    }
    
    // Clearing votes
    private void clearVotes(){
        this.democrateVote = 0;
        this.republicanVote = 0;
    }
    
    
    // This shows the summary of votes
    private void printResults(){
        System.out.println("Total votes for Democrate: "+this.democrateVote+" and total vote for republican: "+this.republicanVote);
    }
    
    public static void main(String[] args) throws Exception {
        VotingMachine vm = new VotingMachine();
        
        // Voting 4 times for Republican
        vm.voteForRepublican();
        vm.voteForRepublican();
        vm.voteForRepublican();
        vm.voteForRepublican();
        
        //Voting 3 times for Democrate
        vm.voteForDemocrat();
        vm.voteForDemocrat();
        vm.voteForDemocrat();
        
        // Pritning the final result
        vm.printResults();
        
        //Clearing votes
        vm.clearVotes();
        
        // Pritning votes after clearing
        vm.printResults();
    }
}

Output:

Total votes for Democrate: 3 and total vote for republican: 4
Total votes for Democrate: 0 and total vote for republican: 0

Related Solutions

Overview For this assignment, design and implement the methods for a class that can be used...
Overview For this assignment, design and implement the methods for a class that can be used to represent a quadratic equation. int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp All that needs to be done for this assignment is to add the class definition and method implementation to the above CPP file. The Quadratic class Data Members The class contains three data members: an integer...
Implement the Nickel class. Include Javadoc comments for the class, public fields, constructors, and methods of...
Implement the Nickel class. Include Javadoc comments for the class, public fields, constructors, and methods of the class. I have added the Javadoc comments but please feel free to format them if they are done incorrectly. public class Nickel implements Comparable { private int year; /** * The monetary value of a nickel in cents. */ public final int CENTS = 5; /** * Initializes this nickel to have the specified issue year. * * @param year * * @pre....
In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Problem: Implement a class named StringParser, along with specified methods, that can read words from a...
Problem: Implement a class named StringParser, along with specified methods, that can read words from a text file, parse those words (such as finding palindromes), and write those words to a new file More specifically: 1. Write the StringParser class so that it has the following methods: a) public static void main(String[] args) -- The main driver of the program. Prompts the user for an input file, an output file, and a choice for what kinds of words to output....
in JAVA: implement a class called tree (from scratch) please be simple so i can understand...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand thanks! tree(root) node(value, leftchild,rightchild) method: insert(value)
Problem 2 (C++): Design and implement a class complexType, that can be used to process complex...
Problem 2 (C++): Design and implement a class complexType, that can be used to process complex numbers. A number of the form a +ib, in which i2 = -1 and a and b are real numbers, is called a complex number. We call a the real part and b the imaginary part of a + ib. Complex numbers can also be represented as ordered pairs (a, b). The class you will design should have the following features. Constructor Your class...
In this project you will implement the DataFrame class along with the all the methods that...
In this project you will implement the DataFrame class along with the all the methods that are represented in the class definition. DataFrame is a table with rows and columns – columns and rows have names associated with them also. For this project we will assume that all the values that are stored in the columns and rows are integers. After you have tested your class, you have to execute the main program that is also given below. DataFrame Class...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will perform some analysis of data considered to be DNA. DNA shall be represented arrays of chars containing only the characters A, C, G and T. In addition to the six methods you will implement, six other methods exist in the class, which use Strings instead of char arrays to represent DNA. These other methods simply invoke the methods you are to implement, so all...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should take in an int and return arr at that index if it is a valid index notEqual The notEqual should take in another myArray object and return true if the objects are not equal to one another and false if they are equal. operator-(float) Will subtract the float value that is passed in from each of the values in arr Copy constructor Will take...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT