Results 1 to 6 of 6
  1. #1
    nyamtoko is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default Program To Call Random Methods

    I need to write a program to create 5 bicycle objects and call random methods for any of the objects created picked at random. At the end of the execution, the program should tell which of the bicycles has won.

    Any ideas??

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Just an idea...

    • create the 5 bicycle objects
    • create the 5 methods
    • instantiate the 5 methods and put them in an array
    • randomly (using the Random class) pick a number (0-4) and use the selected number as an index to pull a method instance from the array

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    nyamtoko is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default program call random object and random method

    I can create the 5 objects and also define the various methods (not necessarily 5). what I don't know how to do is if I can instance methods outside their class definitions and put them in an array. May you elaborate further please.

    My post still says

    Program To Call Random Methods
    I need to write a program to create 5 bicycle objects and call random methods for any of the objects created, picked at random. At the end of the execution, the program should tell which of the bicycles has won.

    Any ideas??

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default hhhmmm... same wave length?

    I'm not sure if we're on the same page here.
    • You asked for ideas and I give you some ideas. Apparently you didn't need/like/understand them. You're welcome.
    • As for the new question:
    what I don't know how to do is if I can instance methods outside their class definitions and put them in an array
    Answer: Yes you can.
    Java Code:
    myClassArray[i] = new SomeClass();
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    If I understand right, your problem is how to call a "random method". If you only have a couple of methods, then the simplest way is to just use a swich statement with hard-coded method names:

    Java Code:
    Random r = previously-created instance of Random
    Bicycle b = pickRandomBicycle();
    switch (r.nextInt(5)) {
      case 0 :
        b.punctureTyre();
        break;
      case 1 :
        b.testBrakes();
        break;
      ...
    }
    A more complex approach would be to use the reflection API and have an array of Method objects corresponding to the methods you want to call randomly, then choose a random index into this array. This is not usually good program design unless you really need it for some reason (e.g. you need to create the list of methods dynamically, based on their name or something).
    Last edited by neilcoffey; 12-01-2008 at 09:30 AM.

  6. #6
    nyamtoko is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default program to call random methods

    Thanks guys. Let me work on your ideas and see what I come up with.

Similar Threads

  1. How to create object dinamically and call methods.
    By Gelembjuk in forum New To Java
    Replies: 8
    Last Post: 10-26-2008, 10:28 AM
  2. Replies: 3
    Last Post: 10-06-2008, 01:27 AM
  3. How to call methods of different classes
    By adeeb in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 07:08 AM
  4. Replies: 0
    Last Post: 01-02-2008, 02:52 PM
  5. I need to call and run another program
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 AM

Posting Permissions

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