Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I'm trying to make the code repeat itself after the user types NO for the question type yes if you are happy with your

Hello,

I'm trying to make the code repeat itself after the user types "NO" for the question "type yes if you are happy with your vote". I understand I have to make a while() loop to make this possible but I can't figure it out.

below I paste VoteRecorder file and the VoteRecorderDEMO file

import java.util.Scanner;

public class VoterRecorder { private static Scanner record = new Scanner(System.in); private static String nameCandidatePresident1; private static String nameCandidatePresident2; private static String nameCandidateVicePresident1; private static String nameCandidateVicePresident2; private static int votesCandidatePresident1; private static int votesCandidatePresident2; private static int votesCandidateVicePresident1; private static int votesCandidateVicePresident2;

private int myVoteForPresident, myVoteForVicePresident;

public VoterRecorder() { // constructor myVoteForPresident = -1; myVoteForVicePresident = -1; /*--------------------------------------------------- * Method: voterrecord * * purpose; this let use set value for our liking for this program * * pre condition; N/A * * post condition: President and vice president are really for use * * parameter list; string (President 1 , President 2, vice President 1 , vice President 2 * * return: N/A */ }

public static void setCandidatesPresident(String name1, String name2) { // sets president candidates name nameCandidatePresident1 = name1; nameCandidatePresident2 = name2; votesCandidatePresident1 = 0; votesCandidatePresident2 = 0; } /* * this method : setCandidatesPresident * * purpose: set P * * pre-condition name of P1, P2 is +p1+ with a addition printed "text" * * post-condition: * * parameter list: string result= "" of (VP1,VP2 addition text print "has", " voters; " * * return : result Static void string */ public static String getCurrentVotePresident() { // returns String containing vote counts for president String result = ""; result += nameCandidatePresident1 + " has " + votesCandidatePresident1 + " voters; " + nameCandidatePresident2 + " has " + votesCandidatePresident2 + " voters;"; return result; } /* * this method : getCurrentVoteVicePresdeident * * purpose: names of two of VicePresident set string * * pre-condition name of VP1, VP2 are set * * post-condition: N/A * * parameter list: string = name1,2 * * return : N/A */ public static void setCandidatesVicePresident(String name1, String name2) { // sets vice president candidates name nameCandidateVicePresident1 = name1; nameCandidateVicePresident2 = name2; votesCandidateVicePresident1 = 0; votesCandidateVicePresident2 = 0; } /* * method: static string getCurrentVoteVicePresident * * purpose: set result of input of VP's votes * * Pre-condition: string result ="" * * post- condition :return result; * * parameter: name of VP1 , Vp2 and text "has,voters" * * return: the result return */ public static String getCurrentVoteVicePresident() { // returns String containing vote counts for vice president String result = ""; result += nameCandidateVicePresident1 + " has " + votesCandidateVicePresident1 + " voters; " +nameCandidateVicePresident2 + "has " + votesCandidateVicePresident2 + " voters;"; return result; } /* * this method : resetVotes * * purpose: once finshed with the result and ready to work for another voter this will set that value back to 0 * * pre-condition name of P1, P2 is +p1+ with a addition printed "text" * * post-condition: * * parameter list: P1 and P2 are set to 0 and VP1 and VP2 are set to 0 * * return : N/A */ public static void resetVotes() { // resets number of votes votesCandidatePresident1 = 0; votesCandidatePresident2 = 0; votesCandidateVicePresident1 = 0; votesCandidateVicePresident2 = 0; } /* * this method : resetVotes * * purpose: each votes will go to its apportate ojective in respect of the chosen candidate // saved votes for each vaule of candidate * * pre-condition if (VP1 is voted set off Voter P1) same goes for the VP1 . * * post-condition: * * parameter list: P1, VP1 =1; P2, VP2 =2 * * return : N/A */ public void recordVotes() { if(myVoteForPresident == 1) { votesCandidatePresident1++; } else if(myVoteForPresident == 2) { votesCandidatePresident2++; }

if(myVoteForVicePresident == 1) { votesCandidateVicePresident1++; } else if(myVoteForVicePresident == 2) { votesCandidateVicePresident2++; } } /* * this method : getAvote * * purpose: displays the of string and return votes of choice for each candidate * * pre-condition: int vote with in {} if true print out and returns the choises of voters * * post-condition: * * parameter list: println ( "please choose a canidate , 0,1,2,) * * return : return vote */ private int getAVote(String name1, String name2) { int vote; while(true) { System.out.println("Please choose a candidate:"); System.out.println("0 - No one"); System.out.println("1 - " + name1); System.out.println("2 - " + name2); vote = record.nextInt(); if(vote >= 0 && vote <= 2) break; } return vote; } /* * this method : getVoter * * purpose: display canidates names for and the voters with "text" * * pre-condition: system.outprint "text";// voteforpresident P1 name , P2 name , VP1 and VP2 * * post-condition: * * parameter list: "text" and names of canidates * * return : N/A */ private void getVotes() { // gets vote for both president and vice president System.out.println(" YOU ARE VOTING FOR PRESIDENT "); myVoteForPresident = getAVote(nameCandidatePresident1, nameCandidatePresident2); System.out.println(" YOU ARE VOTING FOR VICE PRESIDENT "); myVoteForVicePresident = getAVote(nameCandidateVicePresident1, nameCandidateVicePresident2); recordVotes(); } /* * this method : confirmVotes * * purpose: display the votes and the option to countien with with another vote// asking if happy with the votes they have chosen * * pre-condition: if , esles if , "text" with candidate names of P1,P2,VP1,and VP2 * * post-condition: * * parameter list: println "text on votes with candidate names " if one the voteer trigger on eit will follow on displaying the names and result. * * return : return true if not apply then fasle and wont display */ private boolean confirmVotes() { if(myVoteForPresident == 0) { System.out.println(" Your vote for president is no one"); } else if(myVoteForPresident == 1) { System.out.println(" Your vote for president is " + nameCandidatePresident1); } else if(myVoteForPresident == 2) { System.out.println(" Your vote for president is " + nameCandidatePresident2); }

if(myVoteForVicePresident == 0) { System.out.println(" Your vote for vice president is no one"); } else if(myVoteForVicePresident == 1) { System.out.println(" Your vote for vice president is " + nameCandidateVicePresident1); } else if(myVoteForVicePresident == 2) { System.out.println(" Your vote for vice president is " + nameCandidateVicePresident2); } System.out.println("Type yes if you are happy with your vote"); String input = record.next(); if(input.toUpperCase().compareTo(" YES ") == 0) return true; else return false; } public void getAndConfirmVotes() { getVotes(); boolean vlad = confirmVotes(); } }

_______________________________________________________________________

import java.util.Scanner;

public class VoterRecorderDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in);

VoterRecorder.setCandidatesPresident("annie", "bob"); VoterRecorder.setCandidatesVicePresident("john", "susan"); while(true) { VoterRecorder newVoter = new VoterRecorder(); newVoter.getAndConfirmVotes(); System.out.println(" Type yes if there is another voter "); String input = sc.next(); if(input.toUpperCase().compareTo("YES") == 0) continue; else break; } System.out.println(VoterRecorder.getCurrentVotePresident()); System.out.println(); System.out.println(VoterRecorder.getCurrentVoteVicePresident());

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions