Results 1 to 3 of 3
  1. #1
    eugenechia is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default Java Program (Vote system)

    Hello, I'm attempting to do a Java question on Voting system.

    The idea goes like this:

    There are 10 contestants in a singing competition. The java program will generate random 10 votes for any of the 10 singers. Some votes may appear more than once. (e.g contestant 4 may get 2 votes). This is done with Random number. (Contestant will be numbered from 0 to 9)

    Write a thread class VoteThread to run such that it displays the contestant number and the votes.

    Here is the desired output:

    Here are the votes:
    5 1 2 5 5 4 7 2 9 0

    Contestant 5 has 1 votes so far
    Contestant 0 has 1 votes so far
    Contestant 1 has 1 votes so far
    Contestant 4 has 1 votes so far
    Contestant 2 has 1 votes so far
    Contestant 5 has 2 votes so far
    Contestant 9 has 1 votes so far
    Contestant 7 has 1 votes so far
    Contestant 5 has 3 votes so far
    Contestant 2 has 2 votes so far

    Here are the results:
    Contestant 0 has 1 votes
    Contestant 1 has 1 votes
    Contestant 2 has 2 votes
    Contestant 3 has 0 votes
    Contestant 4 has 1 votes
    Contestant 5 has 3 votes
    Contestant 6 has 0 votes
    Contestant 7 has 1 votes
    Contestant 8 has 0 votes
    Contestant 9 has 1 votes

    Winner is Contestant 5


    Here are my codes so far

    Test:
    Java Code:
    import java.util.*;
    
    public class Test{
    	static final int TOTAL = 10;
    
    	public static void main (String[] args){
    
    	int [] votes = new int[TOTAL];
    	int [] results = new int[TOTAL];
    
    	Random contestantvote = new Random();
    
    	System.out.println("Here are the votes: ");
    
    		for(int i=0; i<votes.length; i++) {
    			votes[i]=contestantvote.nextInt(TOTAL);
    			System.out.print(votes[i]+" ");
    			}
    
    		System.out.println();
    
    		VoteThread TOTAL = new VoteThread(votes,results);
    
    	    TOTAL.start();
    
            try
    		{
    		 	TOTAL.join();
    		 }
            catch (InterruptedException e) {}
    
            System.out.println("Here are the results: ");
    
          /*  for(int counter=0;counter<votes.length;counter++) {
    			System.out.println("Contestant "+counter+" has "+votes[counter]+" votes");
    		}*/
    
    
    	}
    
    }
    VoteThread:
    Java Code:
    public class VoteThread extends Thread{
    
    private int votes [];
    private int results [];
    
    public VoteThread(int votes[], int results[]){
    
    this.votes = votes;
    this.results = results;
    }
    
    	public void run() {
    
    	for(int count=0; count<votes.length;count++)
    		{
    			results[0] = results[0] + 1;
    			System.out.println("Contestant "+ votes[count] +" has "+results[0]+ " votes so far");
    
    		
    
    			try{
    
    					sleep(355);
    
    				}catch (Exception e){}
    
    		}
    
    
    
    		}
    	}
    I'm stuck with producing the output properly for "Contestant x has y votes so far". Cant seem to formulate it.

    Can you please point me in the right direction to produce the final results of the voting system as well ?

    Thank you

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Java Code:
    int[] votes= new int[10]; // Create 10 vote-counts
    for (int i = 0; i < votes.length; ++i) {
        int idx = (int)(Math.random()*10); // Select the contestant to vote  for
        votes[idx]++; // Increase this contestant's votecount
        System.out.println("Contestant "+i+" has "+votes[i]+" votes so far."); // Print the current votes for contestant i.
    }
    Something like this should do ;)
    I die a little on the inside...
    Every time I get shot.

  3. #3
    eugenechia is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Smile

    Hi there,

    thank you for the help. However the requirement is to generate the random numbers in the main thread and do the voting in the VoteThread.

    I'm fine with the random numbers, its the voting in the VoteThread that I'm facing issues.

    I tried using your method but its not working on my end. mabbe something i din do right ?

    Cheers

Similar Threads

  1. Vote - Favorite Loop Statement
    By angryboy in forum New To Java
    Replies: 24
    Last Post: 02-15-2009, 08:15 PM
  2. Vote for my java bug
    By kjanz1899 in forum Java Applets
    Replies: 0
    Last Post: 06-29-2008, 05:29 PM
  3. Replies: 0
    Last Post: 08-09-2007, 06:30 AM
  4. Replies: 2
    Last Post: 08-07-2007, 07:22 PM
  5. Replies: 1
    Last Post: 07-27-2007, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •