Results 1 to 3 of 3
Thread: Java Program (Vote system)
- 03-01-2010, 07:59 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
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:
VoteThread: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"); }*/ } }
I'm stuck with producing the output properly for "Contestant x has y votes so far". Cant seem to formulate it.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){} } } }
Can you please point me in the right direction to produce the final results of the voting system as well ?
Thank you
- 03-01-2010, 08:53 AM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Something like this should do ;)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. }I die a little on the inside...
Every time I get shot.
- 03-01-2010, 09:23 AM #3
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
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
-
Vote - Favorite Loop Statement
By angryboy in forum New To JavaReplies: 24Last Post: 02-15-2009, 08:15 PM -
Vote for my java bug
By kjanz1899 in forum Java AppletsReplies: 0Last Post: 06-29-2008, 05:29 PM -
please help on java program for printer : this printer is connected to system which
By for453 in forum Java 2DReplies: 0Last Post: 08-09-2007, 06:30 AM -
Need to write a program to execute a list of system commands
By mdthahir in forum New To JavaReplies: 2Last Post: 08-07-2007, 07:22 PM -
Need to write a program to execute a list of system commands
By mdthahir in forum NetworkingReplies: 1Last Post: 07-27-2007, 05:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks