Results 1 to 6 of 6
Thread: Program To Call Random Methods
- 11-29-2008, 03:14 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
- 11-29-2008, 05:03 PM #2
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-30-2008, 08:20 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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??
- 11-30-2008, 03:38 PM #4
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:
Answer: Yes you can.what I don't know how to do is if I can instance methods outside their class definitions and put them in an array
CJSLJava Code:myClassArray[i] = new SomeClass();
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-01-2008, 08:02 AM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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:
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).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; ... }Last edited by neilcoffey; 12-01-2008 at 08:30 AM.
Neil Coffey
Javamex - Java tutorials and performance info
- 12-01-2008, 11:49 AM #6
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
How to create object dinamically and call methods.
By Gelembjuk in forum New To JavaReplies: 8Last Post: 10-26-2008, 09:28 AM -
[SOLVED] Can two threads call two different synchronized instance methods of an Objec
By piyu.sha in forum Threads and SynchronizationReplies: 3Last Post: 10-06-2008, 12:27 AM -
How to call methods of different classes
By adeeb in forum New To JavaReplies: 2Last Post: 06-06-2008, 06:08 AM -
How can I call java class methods in Oracle 10g PL/SQL
By searcher34 in forum JDBCReplies: 0Last Post: 01-02-2008, 01:52 PM -
I need to call and run another program
By coco in forum Advanced JavaReplies: 1Last Post: 07-31-2007, 05:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks