In: Computer Science
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
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