Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Election Java Problem

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Election Java Problem

    Hello Everyone.

    I'm having difficulty solving this election problem.

    Due 11/9/2009
    The purpose of this project is to demonstrate knowledge of loops and counters.

    You will present to the user a choice of at least 3 items to chose from. Choices can be repeatedly made until the user asks to exit. At the end the counts for each choice and the percentages will be displayed. A winner needs to be declared if there is a simple majority.

    Feel free to use different candidates for a different election like SGA President, or Favorite Actor, or Favorite Video Game [ keep it clean please ].

    Sample Run


    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 1

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 3

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 3

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 2

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 3

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 1

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 2

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 3

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 3

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 1

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 1

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 2

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 3

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 3

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 1

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 44

    Ooops! Invalid Candidate!

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 3

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 0

    Results:

    Candidate Votes Percent
    Corzine 5 31.3%
    Christie 3 18.8%
    Daggett 8 50.0%

    Daggett declared winner
    Press any key to continue . . .

    Another Sample Run


    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 2

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 3

    1 - Jon Corzine(D)
    2 - Chris Christie(R)
    3 - Chris Daggett(I)

    Enter the voter's choice (1-3, 0 Exit): 0

    Results:

    Candidate Votes Percent
    Corzine 0 0.0%
    Christie 1 50.0%
    Daggett 1 50.0%

    No winner declared!
    Press any key to continue . . .

    Its not solved by using files and not a for loop.
    Here are some examples I found in my textbook that relate to the problem.
    import java.util.Scanner;
     
    public class DemoDoWhile
    {
    	public static void main(String args[])
    	{
    		int customerChoice;
     
    		Scanner input = new Scanner(System.in);
     
    		do
    		{
    			// display menu
    			System.out.println("\nIgloo Ice Cream Store");
    			System.out.println("\nMenu Choices");
    			System.out.println("\n1 - Sundae");
    			System.out.println("2 - Shake");
    			System.out.println("3 - Cone");
    			System.out.println("\n0 - Exit\n");
     
    			System.out.print("Enter the customer's choice: ");
    			customerChoice = input.nextInt();
     
    			switch (customerChoice)
    			{
    				case 0:
    				  System.out.println("\nBye bye!\n");
    				  break;
    				case 1:
    				  System.out.println("\nSundaes Use:");
    				  System.out.println("\t8 oz. ice cream");
    				  System.out.println("\t1 oz. chopped nuts");
    				  System.out.println("\t2 oz. chocolate sauce");
    				  System.out.println("\t1     cherry");
    				  break;
    				case 2:
    				  System.out.println("\nShakes Use:");
    				  System.out.println("\t10 oz. ice cream");
    				  System.out.println("\t1 oz.  chocolate sauce");
    				  break;
    				case 3:
    				  System.out.println("\nCones Use:");
    				  System.out.println("\t6 oz. ice cream");
    				  System.out.println("\t1     cone");
    				  break;
    				default:
    				  System.out.println("\nyou entered an invalid choice");
    			} // end switch
    		} while (customerChoice != 0);
    	} // end main method
    } // end class
     
    Example 2
     
    import java.util.Random;
     
    public class FlipCoinsLoop
    {
    	public static void main(String args[])
    	{
    		Random randomNumbers = new Random();
    		int numHeads = 0;
    		int numTails = 0;
    		int numFlips;
     
    		//simulate flipping a coin multiple times and tally what we have
     
    		for (numFlips = 1; numFlips <= 10; numFlips++)
    		{
     
     
    			if (randomNumbers.nextBoolean())
    			{
    				System.out.println("Heads");
    				numHeads++;
    			}
    			else
    			{
    				System.out.println("Tails");
    				numTails++;
    			}
    		}
     
    		System.out.printf("\nTally:\n");
    		System.out.printf("%s %d\n","Heads",numHeads);
    		System.out.printf("%s %d\n","Tails",numTails);
    	}
    }
     
    ex. 3
     
    public class IntDivisionCast
    {
        public static void main(String[] args)
     
        {
    		// declare variables
    		int numberVotesBob, numberVotesPat, totalNumberVotes;
    		double percentVotesBob, percentVotesPat;
     
    		// get data
    		numberVotesBob = 145;
    		numberVotesPat = 197;
     
     
    		// calc data
    		totalNumberVotes = numberVotesBob + numberVotesPat;
     
    		// integer division avoided by cast (double) "pretends" that
    		// numberVotesBob is a double for calculation
     
    		percentVotesBob = (double) numberVotesBob / totalNumberVotes * 100;
    		percentVotesPat = (double) numberVotesPat / totalNumberVotes * 100;
     
    		// display results
    		System.out.printf("\n%-12s %6.2f%%\n", "% Votes Bob",percentVotesBob);
    		System.out.printf("\n%-12s %6.2f%%\n", "% Votes Pat",percentVotesPat);
     
        }
    }
    If anyone can suggest direction or guidance for me, I would appreciate it. I was using files before to solve it and my professor said it was wrong.

    Thanks for any help

    ron
    Last edited by helloworld922; November 13th, 2009 at 08:10 PM.


Similar Threads

  1. please could you help me with this java problem im very lost
    By eyeore in forum Java Theory & Questions
    Replies: 4
    Last Post: September 7th, 2009, 09:19 AM
  2. java problem
    By Mj Shine in forum Java Theory & Questions
    Replies: 1
    Last Post: August 14th, 2009, 12:24 AM
  3. Java session problem
    By Padmaja in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: August 5th, 2009, 09:06 PM
  4. Java program for election voting machine
    By bruint in forum File I/O & Other I/O Streams
    Replies: 15
    Last Post: May 20th, 2009, 10:07 PM
  5. Java GUI problem in paintComponent?
    By Richard_ in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2009, 08:19 AM