Results 1 to 9 of 9
- 08-01-2009, 07:27 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
- 08-01-2009, 08:56 PM #2
Why don't you post your solution so that people who have the same question can search the forums and come see how you managed to solve the problem.
- 08-01-2009, 09:06 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 5
- Rep Power
- 0
fair enough :D
my problem was in generating the Thing laundry in that last loop. I had originally thought that objects must all have different names to them lest they interfere with each other. To solve that problem I wanted to create an array that would assign a number to each name on the Thing generated and it would increase. However This solution here shows that it's not necessary to do that. This code works just like I dreamed it would.Java Code:import becker.robots.*; import java.util.Random; public class SallysRoom2 extends RobotSE { SallysRoom2(City c, int s, int a, Direction dir, int t) { super(c, s, a, dir, t); } //helper method to help tell sally when she is at the hamper public boolean inHamper() { return(this.getAvenue() == 10 && this.getStreet() == 12); } //method that will tell robot to go down and return picking up everything along the way. public void startCleaning() { while(!this.inHamper()) { while(this.frontIsClear()) { while(this.canPickThing()) { this.pickThing(); } this.move(); } while(this.canPickThing()) { this.pickThing(); } if(this.inHamper()) { System.out.println("I have finished cleaning " + this.countThingsInBackpack() + " things that were cluttering my floor."); } else { this.turnRight(); this.move(); this.turnRight(); while(this.frontIsClear()) { while(this.canPickThing()) { this.pickThing(); } this.move(); } while(this.canPickThing()) { this.pickThing(); } this.turnLeft(); this.move(); this.turnLeft(); } if(this.inHamper()) { System.out.println("I have finished cleaning " + this.countThingsInBackpack() + " things that were cluttering my floor."); } } } public static void main(String[] args) { int randSt; int randAve; City SallysRoom = new City(11, 13); Random randomThingGenerator = new Random(); int thingCounter = randomThingGenerator.nextInt(151); SallysRoom2 Sally = new SallysRoom2(SallysRoom, 0, 0, Direction.EAST, 0); //build north and south wall for(int a = 0; a < 11; a++) { new Wall(SallysRoom, 0, a, Direction.NORTH); new Wall(SallysRoom, 12, a, Direction.SOUTH); } //build east and west wall for(int b = 0; b < 13; b++) { new Wall(SallysRoom, b, 10, Direction.EAST); new Wall(SallysRoom, b, 0, Direction.WEST); } //make hamper new Wall(SallysRoom, 12, 10, Direction.NORTH); //randomly disperse laundry for(int c = 0; c < thingCounter; c++) { randSt = randomThingGenerator.nextInt(13); randAve = randomThingGenerator.nextInt(11); Thing laundry = new Thing(SallysRoom, randSt, randAve); } //Sally cleans her room. Sally.startCleaning(); } }
- 07-30-2010, 03:47 AM #4
I'm not sure that helps...
I was hoping, as Mr. Beans suspected would happen, to find some direction regarding the creation of random objects. Thank you for posting your code, but what about class RobotSE and class Thing? It would be much easier to see if your code accomplishes the same or a similar thing as I would like my code to accomplish.
Here's my situation: I have an abstract class Vehicle with several inherited concrete classes (e.g. Car, Truck, Motorcycle, etc.) and I want to generate random vehicles. I've seen reflection used to obtain an inheritance path (as a List of super classes), but I haven't found anything regarding the use of reflection to obtain a List of subclasses; is this possible?
By the way, Mr. Beans, nice avatar you have there! (I'll get a life now :))
-
No, it's not possible to use reflection to get a list of child classes. You really should start your own thread though for your own question.
- 07-30-2010, 04:23 AM #6
Thank you for your speedy response, Fubarable, and I'm sorry! I thought I was following protocol by not starting a new thread since my topic and his topic are one in the same.
-
It's best to start your own thread (since it is your question after all), and then link to any related threads. But regardless, your suggestion about using reflection won't work. Perhaps you want to use a factory of some sort here (but this is just a guess as I'm no expert in this by any means).
- 07-30-2010, 04:35 AM #8
Thanks, I'll do that in the future. By the way, I did find an article explaining how to obtain a list of classes in a particular package that implement a particular interface here.
-
Similar Threads
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 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, 04:25 PM -
Generating a random number
By oridov in forum New To JavaReplies: 2Last Post: 11-29-2008, 05:12 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
generating random numbers in a 5x5 array.
By acidacid in forum New To JavaReplies: 3Last Post: 08-14-2007, 03:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks