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...
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...
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...
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...
In C++, implement a class called setOper that provides several simple set operations. The class only...
In C++, implement a class called setOper that provides several simple set operations. The class only needs to deal with sets that are closed intervals specified by two real numbers; for example, the pair (2.5, 4.5) represent the interval [2.5, 4.5]. The following operations should be supported: - Check if the value x belongs to the given interval. - Check if the value x belongs to the intersection of two intervals. - Check if the value x belongs to the...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class In the ‘Implementation of linked lists’ lecture, review the ‘Dynamic implementation of single linked list’ section. You will be adding new methods to the List class. Eight new methods are required: new constructor – creates a new single linked list from an array of integers e.g. int a[] = {1, 2, 3, 4}; List list = new List(a); toString() – returns a string representing...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT