PROGRAMMING IN JAVA - ASSIGNMENT: JAVA CLASSES Instruction: Give java code for a
ID: 3607603 • Letter: P
Question
PROGRAMMING IN JAVA -
ASSIGNMENT: JAVA CLASSES
Instruction: Give java code for a support class called Vote that allows a user to add "yes" and "no" votes to some total votes, and allows the user to find out which has the higher number. Code class must contain the constructor and methods specified below!
Give code for a support class called vote that allows a user to add “yes" and "no" votes to some total votes, and allows the user to find out which has the higher number More specifically, your class must contain the following constructor and methods: . A constructor of the form Vote (int yesVotes, int noVotes) This constructor sets the initial number of"yes" votes to yesVotes and the initial number of"no" votes to of the point to noVotes public void yeaVote ) This method increments the number of "yes" votes by public void noVote ) This method increments the number of"no" votes by 1 . public String getWinner ) This method should return either "yes" or "no", depending on which has the higher total so far. For example: Vote v= new Vote (2, 1); v.yesVote) v.noVote System.out.println (v.getwinner ) Initially 2 “yes" votes and 1 "no" vote Now 3 "yes" votes Now 2 "no" votes Prints "yes"Explanation / Answer
Program based on the above question
class Vote
{ int yes=0,no=0;
public Vote(int yesVotes,int noVotes) //constructor
{ yes=yesVotes; // setting initial yes to yesVotes
no=noVotes; // and no to noVotes
}
public void yesVote()
{
yes++; // increement yes votes
}
public void noVote()
{
no++; // decrement no votes
}
public String getWinner()
{if(yes>no)
return "yes";
else if(no>yes)
return "no";
return null;
}
public static void main(String args[])
{
Vote v= new Vote(2,1);
v.yesVote();
v.noVote();
System.out.println(v.getWinner());
}
}
Output
yes
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.