Results 1 to 8 of 8
- 10-09-2012, 02:00 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
-
Re: Simulating objects selecting objects
Consider providing much more detail regarding the nature of your problem. Your question currently is much too general and vague to be answerable.
- 10-09-2012, 11:10 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Simulating objects selecting objects
You'll need a Collection of Candidates.
A Collection of Voters.
Those Collections can be simple arrays if it's a fixed number of each, or something like an ArrayList, or some form of Set maybe.
Loop over the Voters and have each one castVote by randomly selecting a Candidate.
You'll also need some way of storing the number of votes each Candidate has received...
Leading capitals above represent things that are classes/objects. Camel case (initial lower case letter) represents possible methods.
That should give you more than enough to work with to produce some actual code.
Here's the API as well, if you need to know what some of the stuff above does.Please do not ask for code as refusal often offends.
** This space for rent **
- 10-10-2012, 04:15 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Re: Simulating objects selecting objects
Thanks Tolls, how can I store the number of votes each candidate has received if I'm looping over the voters and having each one castVote by randomly selecting a Candidate (as described above) like below. I've a Candidate class with an array list of candidates and a Voter class with an array list of voters.
Main Class
for (int i = 0; i < Voter.voters.size(); i++) {
Voter.castVote();
}
Voter Class
public static Candidate castVote(){
Candidate chosenCandidate = Candidate.candidates.get(new Random().nextInt(Candidate.candidates.size()));
return chosenCandidate;
}
- 10-10-2012, 11:03 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Simulating objects selecting objects
For noting the votes, you could Map the Candidate to an Integer, or an easier way would simply be to have a 'votes' attribute on the Candidate, and an addVote() method which simply increments it each time a vote its cast for that Candidate.
Please do not ask for code as refusal often offends.
** This space for rent **
- 10-17-2012, 02:05 AM #6
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Re: Simulating objects selecting objects
How can I make the time taken to generate and register a vote random. By register I mean increment the tally for the selected candidate.
I'm using the following code to generate a vote.
public static Candidate SelectedCandidate = Candidate.Candidates.get(new Random().nextInt(Candidate.Candidates.size()));
I'm using the following code to register a vote. VotingBooth is a class.
public static void increment(){
if(VotingBooth.SelectedCandidate == Candidate.Candidate1){
Candidate.Candidate1.NoOfVotes++;
}
else if(VotingBooth.SelectedCandidate == Candidate.Candidate2){
Candidate.Candidate2.NoOfVotes++;
}
else if(VotingBooth.SelectedCandidate == Candidate.Candidate3){
Candidate.Candidate3.NoOfVotes++;
}
}
- 10-17-2012, 10:50 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Simulating objects selecting objects
pause the thread?
Not sure exactly what you're mimicking, but that's how you would create a delay.
You might want multiple threads, though, but that really depends on your requirements.Please do not ask for code as refusal often offends.
** This space for rent **
- 10-27-2012, 08:05 PM #8
Member
- Join Date
- Oct 2012
- Posts
- 7
- Rep Power
- 0
Re: Simulating objects selecting objects
I've created an empty constructor for Booth threads using the code below. The Booth class extends Thread.
public Booth(){}
I've created Booth threads and added them to an arraylist called Booths.
for(int i = 0; i < 4; i++){
Booth m = new Booth();
Booth.Booths.add(m);
}
I've selected a random Booth thread from the arraylist using the code below.
Booth SelectedBooth = Booth.Booths.get(new Random().nextInt(Booth.Booths.size()));
I've printed the selected Booth using the code below but I'm getting unformatted output.
System.out.println(SelectedBooth);
How can I format the output so that the Booth's ID is printed if the Booth's ID is its index position in the arraylist. Do I need to use the tostring method of the java.util library, if so how?
Public String toString(){
What code do I need here
}
Otherwise do I need an ID field in the constructor, if so how can I change the for loop so that each Booth is given an ID that increments starting with 0 instead of m?
Similar Threads
-
Noob question - Create objects using objects as parameters
By pantaloc in forum New To JavaReplies: 12Last Post: 04-29-2012, 03:55 PM -
GUI Objects
By larry_d1990 in forum New To JavaReplies: 3Last Post: 02-02-2011, 01:13 PM -
Objects
By Silverlining in forum New To JavaReplies: 3Last Post: 11-11-2009, 03:36 PM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 05:25 PM -
how many objects ?
By kevinsong in forum Advanced JavaReplies: 16Last Post: 07-16-2008, 06:59 PM
Bookmarks