Results 1 to 13 of 13
  1. #1
    Murd is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default How to Randomly call objects in an Array?

    Hello,

    I've finished some code which references a bunch of different java files using inheritance. However, I would like to create an array which randomly calls an object from each method (draw, move, and erase) from the multiple files. I'm having some trouble doing so and I'm not sure how to randomly call a .draw, .move, and .erase... help? I'm new to Java so patience would be appreciated. Thank you.

    Start Code :

    import java.util.Random;

    public class testShape
    {
    public static void main(String[] args)
    {
    Shape inheritance[] = new Shape[5];

    inheritance[0] = new Rectangle();
    inheritance[1] = new Ellipse();
    inheritance[2] = new Triangle();
    inheritance[3] = new Square();
    inheritance[4] = new Circle();

    for (int i=0; i<3; ++i)
    {
    inheritance[i].draw();
    inheritance[i].move();
    inheritance[i].erase();
    }
    }
    }

    End Code

    - Murd

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to Randomly call objects in an Array?

    Take a look at the Random class.

  3. #3
    Murd is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to Randomly call objects in an Array?

    Currently it complies out the first 3 (Rectangle, Ellipse, and Triangle) using the for loop. I'm just not sure how I would go about randomizing what it outputs...

  4. #4
    Murd is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to Randomly call objects in an Array?

    Thanks for your post Junky. I've been researching the Random class online but cannot find much about how it works within an Array. I have successfully used it before on another program but, when I tried to incorporate it within this one It did not work as expected....

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to Randomly call objects in an Array?

    Well once you have read about the Random class you will learn that it can be used to generate random numbers. The you can write your code to do different things based upon which random number is generated.

  6. #6
    Murd is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to Randomly call objects in an Array?

    I figured you could use the Random class to randomize the results. I just don't understand how to link it between the varying files; when I've used the random class before it was all within one java file. I just started studying inheritance and I'm extremely confused... lol

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

    Default Re: How to Randomly call objects in an Array?

    You've got an array and you can use a Random object in the code you've got above to randomly choose an int that is between 0 and the size of your array - 1. Then use this int as an array index to extract your random item from the array.

    Please let us know if this isn't clear.

  8. #8
    Murd is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to Randomly call objects in an Array?

    Thank you for your post Fubarable. I apologize but this isn't clear... perhaps I don't understand the Random class as I thought I did. Can you extend on your explanation?

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to Randomly call objects in an Array?

    create an array
    create a Random object
    declare an int variable
    assign a random number generated to int variable
    access the element in the array with an index of the int variable

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

    Default Re: How to Randomly call objects in an Array?

    Let's break it down:

    Have you had a look at the API for the Random class (link here)? If so, what two methods can you use to get random ints?

    If you want to choose a random int to use as an index to get a random item from your array, what range of ints are you interested in? What should be the smallest and the largest int randomly returned?

    edit: I'm going to bow out as this is really Junky's question (sorry Junky).

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: How to Randomly call objects in an Array?

    Try writing some code and experiment. If you have problems then come back here and post your code and include error messages if you get them.

  12. #12
    Murd is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to Randomly call objects in an Array?

    Alright will do. I appreciate your help.

  13. #13
    Join Date
    Nov 2011
    Posts
    28
    Rep Power
    0

    Default Re: How to Randomly call objects in an Array?

    Reading Java docs will tell you everything you need to know. I feel like you are likely over complicating the Random class in your head, because it is a pretty simple class to use. Don't even have to worry about proper seeding unlike in C++. (I believe you don't at least)

Similar Threads

  1. an error in call array
    By HearT.Hunt3r in forum New To Java
    Replies: 17
    Last Post: 09-14-2011, 05:11 PM
  2. Function call returning array of objects
    By ShitalJain in forum New To Java
    Replies: 4
    Last Post: 06-25-2011, 09:39 AM
  3. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  4. Dynamically create objects, set value and call
    By buntyindia in forum New To Java
    Replies: 5
    Last Post: 05-25-2011, 05:59 PM
  5. Array of Objects
    By sfe23 in forum New To Java
    Replies: 19
    Last Post: 02-04-2009, 05:57 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
  •