Docsity

PROGRAMMING IN JAVA - ASSIGNMENT: JAVA CLASSES, Assignments of Java Programming

Toggle sidebar
bg1
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!
pf2

Partial preview of the text

Download PROGRAMMING IN JAVA - ASSIGNMENT: JAVA CLASSES and more Assignments Java Programming in PDF only on Docsity!

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!

Code: class Vote { int yes = 0 ; // instance variable to keep track of "yes" votes int no = 0 ; // instance variable to keep track of "no" votes // constructor that takes initial values for "yes" and "no" votes public Vote( int yesVotes, int noVotes) { yes = yesVotes; // set initial "yes" votes to given value no = noVotes; // set initial "no" votes to given value } // method to increment number of "yes" votes public void yesVote() { yes++; // increment "yes" vote count } // method to increment number of "no" votes public void noVote() { no++; // increment "no" vote count } // method to determine which option has more votes so far public String getWinner() { if (yes > no) { return "yes"; // "yes" has more votes } else if (no > yes) { return "no"; // "no" has more votes } else { return null ; // both have equal votes } } // main method to test the Vote class public static void main(String args[]) { Vote v = new Vote( 2 , 1 ); // create new Vote object with initial values v.yesVote(); // increment "yes" vote count v.noVote(); // increment "no" vote count System.out.println(v.getWinner()); // print current winner } }