NOTE: The completed code must pass in the following compiler. Please make absolu
ID: 3775695 • Letter: N
Question
NOTE: The completed code must pass in the following compiler. Please make absolutely sure it does before posting: http://codecheck.it/codecheck/files?repo=bj4fp&problem=ch03/c03_exp_3_14
And PLEASE post this as text and not as an image.
~
Implement a VotingMachine class that can be used for a simple election. Have methods to clear the machine state, to vote for a Democrat, to vote for a Republican, and to get the tallies for both parties. Extra credit if your program gives the nod to your favored party if the votes are tallied after 8 p.m. on the first Tuesday in November, but acts normally on all other dates. (Hint: Use the GregorianCalendar class - see Programming Project 2.1.)
Use the following class as your main class:
You need to supply the following class in your solution:
Complete the following file:
VotingMachine.java
Explanation / Answer
/* Class VotingSimulation.java */
import java.util.GregorianCalendar;
public class VotingSimulation
{
public static void main(String [] args)
{
VotingMachine vm = new VotingMachine();
vm.clear();// Clearing votes in Vote machine
vm.voteForDemocrat(); //Democrat
vm.voteForRepublican(); //Republican
vm.voteForDemocrat(); //Democrat
vm.voteForRepublican(); //Republican
vm.voteForRepublican(); //Republican
System.out.print("Democrats: ");
System.out.println(vm.getDemocratVotes());//Printing Total DemocratVotes
System.out.print("Republicans: ");
System.out.println(vm.getRepublicanVotes()); //Printing Total Republic Votes
String democrat = new String(" for the Democrat and ");
System.out.println("Out of " + vm.getTotal() + " votes the total votes in VoteMachine have been " + vm.getDemocratVotes() + democrat + vm.getRepublicanVotes() + " for the republican");
System.out.println("Votes have now been reset");
}
}
========================================================
/* Class name VotingMachine.java */
/*
* Write a description of class VotingMachine here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class VotingMachine
{
// instance variables - replace the example below with your own
private int democratvotes;
private int republicanvotes;
private int totalvotes;
public VotingMachine() //Intialising variables with 0
{
democratvotes = 0;
republicanvotes = 0;
totalvotes = 0;
}
public void voteForRepublican() //For Republic Votes
{
republicanvotes = republicanvotes + 1;
totalvotes = totalvotes + 1;
}
public void voteForDemocrat() //For Democratic Votes
{
democratvotes = democratvotes + 1;
totalvotes = totalvotes + 1;
}
public int getDemocratVotes()
{
return democratvotes;
}
public int getRepublicanVotes()
{
return republicanvotes;
}
public int getTotal()//Total votes
{
return totalvotes;
}
public void clear()//Clearing
{
democratvotes = 0;
republicanvotes = 0;
totalvotes = 0;
}
}
=======================================
Expected out put
==================
Democrats: 2
Republicans: 3
Out of 5 votes the total votes in VoteMachine have been 2 for the Democrat and 3 for the republican
Votes have now been reset
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.