Results 1 to 9 of 9
  1. #1
    mandangalo is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default problems generating random objects

    Solution found. Please delete
    Last edited by mandangalo; 08-01-2009 at 08:29 PM. Reason: Solution found. please delete

  2. #2
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    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.

  3. #3
    mandangalo is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    fair enough :D
    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();		
    	}
    }
    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.

  4. #4
    rgrant222's Avatar
    rgrant222 is offline Member
    Join Date
    Jul 2010
    Location
    Lawrenceville, Ga
    Posts
    17
    Rep Power
    0

    Question 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 :))

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

    Default

    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.

  6. #6
    rgrant222's Avatar
    rgrant222 is offline Member
    Join Date
    Jul 2010
    Location
    Lawrenceville, Ga
    Posts
    17
    Rep Power
    0

    Default

    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.

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

    Default

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

  8. #8
    rgrant222's Avatar
    rgrant222 is offline Member
    Join Date
    Jul 2010
    Location
    Lawrenceville, Ga
    Posts
    17
    Rep Power
    0

    Default

    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.

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

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  3. Generating a random number
    By oridov in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 05:12 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 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
  •