4 Replies - 5722 Views - Last Post: 16 September 2010 - 04:06 PM Rate Topic: -----

#1 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Presidential Ballot - Loops and Objects

Posted 15 September 2010 - 09:58 PM

First off, I am confused on input of chars in an IF Statement in order to confirm the votes.
I do not know how to structure my loop so that it asks you to vote for an unlimited amount of times, until you tell it to close polls.


import java.util.Scanner; 

public class Ballot {
	// Declarations
	private static String namePresident1; // Holds name of first president
	private static String namePresident2; // Holds name of second president
	private static String nameVicePresident1; // Holds the name of first vice president
	private static String nameVicePresident2; // Holds the name of second vice president
	private static int votesPresident1; // Holds votes of first president
	private static int votesPresident2; // Holds votes of second president
	private static int votesVicePresident1; // Holds votes of first vice president
	private static int votesVicePresident2; // Holds votes of second vice resident
	private int myVoteForPresident; // Holds vote of a single voter for president (0 = no choice, 1 = first president, 2 = second president
	private int myVoteForVicePresident; // Holds vote of a single voter for vice president (0 = no choice, 1 = first vice president, 2 = second vice president
	
	// Constructor
	public Ballot() {
		this.namePresident1 = "";
		this.namePresident2 = "";
		this.nameVicePresident1 = "";
		this.nameVicePresident2 = "";
		this.votesPresident1 = 0;
		this.votesPresident2 = 0;
		this.votesVicePresident1 = 0;
		this.votesVicePresident2 = 0;
		this.myVoteForPresident = 0;
		this.myVoteForVicePresident = 0;
	}
	
	// Global variables ---> Local variables
	public Ballot(int myVoteForPresident, int myVoteForVicePresident) {
		this.myVoteForPresident = myVoteForPresident;
		this.myVoteForVicePresident = myVoteForVicePresident;
	}
	
	// Returns myVoteForresident
	public int getMyVoteForPresident() { return myVoteForPresident; }

	// Returns myVoteForVicePresident
	public int getMyVoteForVicePresident() { return myVoteForVicePresident; }
	
	// Sets names of the presidents
	public static void setPresident(String name1, String name2) {
		namePresident1 = name1;
		namePresident2 = name2; 
	}
	
	// Sets the names of the vice presidents
	public static void setVicePresident (String name1, String name2) {
		nameVicePresident1 = name1;
		nameVicePresident2 = name2;
	}
	
	// Resets vote counts to 0
	public static void resetVotes() {
		votesPresident1 = 0;
		votesPresident2 = 0;
		votesVicePresident1 = 0;
		votesVicePresident2 = 0;
	}
	
	// Prints the votes that each president currently holds
	public static String getCurrentVotePresident() {
		return "President: " + 
			namePresident1 + " currently has " + votesPresident1 + " votes. " +
			namePresident2 + " currently has " + votesPresident2 + " votes. " ;
	}
	
	// Prints the votes that each vice president currently holds
	public static String getCurrentVoteVicePresident() {
		return "Vice President: " + 
			nameVicePresident1 + " currently has " + votesVicePresident1 + " votes. " +
			nameVicePresident2 + " currently has " + votesVicePresident2 + " votes. " ;
	}
	 
        // Non-static Method: gets an individual's votes, confirms them, and records them.
	public void getAndConfirmVotes() {
		System.out.println("Vote for President:\n");
		System.out.println("Enter a 0 for no vote, a 1 for " + namePresident1 + ", and a 2 for " + namePresident1);
		Scanner presidentChoice = new Scanner(System.in);
		myVoteForPresident = presidentChoice.nextInt();
		
		System.out.println("Vote for Vice President:\n");
		System.out.println("Enter a 0 for no vote, a 1 for " + nameVicePresident1 + ", and a 2 for " + nameVicePresident1);
		Scanner vicePresidentChoice = new Scanner(System.in);
		myVoteForVicePresident = vicePresidentChoice.nextInt();
		
		System.out.println(confirmVotes());
		
		char userInput;
		Scanner confirm = new Scanner(System.in);
		userInput = confirm.nextChar();
	
		if (confirm.nextChar == 'y')
		{
			?????????????????????????????????;
		}
		else if (confirm.nextChar == 'n')
		{
			?????????????????????????????????;
		}
	}

	// Returns vote choice for a single race from an individual (0 = no choice, 1 = first candidate, and 2 = second candidate)
	private ??? getAVote(String name1, String name2) {
		return ??????????????????;
	}

        // Returns a vote choice for president and vice president from an individual
	private ??? getVotes() {
		return ??????????????????;
		
	}

	// Displays voters vote for president and vice president and asks whether the voter accepts these choices returns true or false according to a yes-or-no response
	private String confirmVotes() {
		return  "My vote for president: " + myVoteForPresident + "\n" +
			"My vote for vice president: " + myVoteForVicePresident + "\n" +
			"Are you satisfied with the results? (yes or no)";
	}

	// a private method that will add an individual's votes to the appropriate static variables.
	private void recordVotes() {
		if      (myVoteForPresident == 1)     { votesPresident1++; }
		else if (myVoteForPresident == 2)     { votesPresident2++; }
		if      (myVoteForVicePresident == 1) { votesVicePresident1++; }
		else if (myVoteForVicePresident == 2) { votesVicePresident2++; }
	} 
}



//Conducts an election. 
 
import java.util.Scanner;

public class Election extends Ballot
{
	public static void main(String[] args) 
	{
		Ballot vote = new Ballot();
		
		// The candidates for president are Barrack and John. 
		setPresident("Barrack", "John");
		// The candidates for vice president are Joe and Sarah.
		setVicePresident("Joe", "Sarah");
		
		// Initial vote count:
		System.out.println("Initial vote count:");
		System.out.println(getCurrentVotePresident());
		System.out.println(getCurrentVoteVicePresident());
		System.out.println();

			
		// Use a loop to record the votes of many voters.
		for (int i = 0, i < i, i++)
		{
			// Create a voter
			System.out.println(getAndConfirmVotes());


		// Create a new Ballot object for each voter. 
		new Ballot(getMyVoteForPresident(), getMyVoteForVicePresident());
		System.out.println(getCurrentVotePresident());
		System.out.println(getCurrentVoteVicePresident());
		System.out.println();
		}

		// After all the voters are done. Present results.
		System.out.println(getCurrentVotePresident());
		System.out.println(getCurrentVoteVicePresident());
			
		// Reset all votes and terminate
		resetVotes();
		System.out.println(getCurrentVotePresident());
		System.out.println(getCurrentVoteVicePresident());
		System.out.println();
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Presidential Ballot - Loops and Objects

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Presidential Ballot - Loops and Objects

Posted 16 September 2010 - 06:33 AM

boolean keepVoting = true;

while(keepVoting){
     //go through the process
     
     //at the end of the loop...
     System.out.println("continue voting? y/n");
     char in = scan.nextChar();
     if(in == 'y'){
          continue;
     }
     else{
          keepVoting = false;
     }
}


this is the basic structure for a user-induced loop.

This post has been edited by Luckless: 16 September 2010 - 06:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Presidential Ballot - Loops and Objects

Posted 16 September 2010 - 08:47 AM

Scanner doesn't have a nextChar() method. However, you can use next() and get the charAt(0) from it.
Was This Post Helpful? 0
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Presidential Ballot - Loops and Objects

Posted 16 September 2010 - 08:55 AM

I don't know why I did that. I know this. lol, fixed

boolean keepVoting = true;

while(keepVoting){
     //go through the process
     
     //at the end of the loop...
     System.out.println("continue voting? y/n");
     String in = scan.next();
     if(in.equals("y"){
          continue;
     }
     else{
          keepVoting = false;
     }
}

This post has been edited by Luckless: 16 September 2010 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Presidential Ballot - Loops and Objects

Posted 16 September 2010 - 04:06 PM

View PostLuckless, on 16 September 2010 - 10:55 AM, said:

I don't know why I did that. I know this. lol, fixed

boolean keepVoting = true;

while(keepVoting){
     //go through the process
     
     //at the end of the loop...
     System.out.println("continue voting? y/n");
     String in = scan.next();
     if(in.equals("y"){
          continue;
     }
     else{
          keepVoting = false;
     }
}


Or to make it less error prone and to scan the full line (a problem people constantly have issues with), you can do this:
boolean keepVoting = true;

while(keepVoting){
     //go through the process
     
     //at the end of the loop...
     System.out.println("continue voting? y/n");
     String in = scan.nextLine();
     if(in.charAt(0) == 'y'){
          continue;
     }
     else{
          keepVoting = false;
     }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1