Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By Tolls

Thread: Simulating objects selecting objects

  1. #1
    Asef is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Simulating objects selecting objects

    How can I simulate a voter selecting a candidate randomly from a group of candidates with the vote being counted if the voter and candidate are objects?
    Last edited by Asef; 10-09-2012 at 02:30 AM. Reason: Not enough detail

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default 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.
    Asef likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Asef is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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;
    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default 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.
    Asef likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Asef is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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++;
    }
    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default 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 **

  8. #8
    Asef is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default 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

  1. Replies: 12
    Last Post: 04-29-2012, 02:55 PM
  2. GUI Objects
    By larry_d1990 in forum New To Java
    Replies: 3
    Last Post: 02-02-2011, 12:13 PM
  3. Objects
    By Silverlining in forum New To Java
    Replies: 3
    Last Post: 11-11-2009, 02:36 PM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  5. how many objects ?
    By kevinsong in forum Advanced Java
    Replies: 16
    Last Post: 07-16-2008, 05:59 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
  •