

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


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 } }