Instance Variables

Note:  Please keep the programs that you create today, in case you have a dispute about your grades for the ICEs at the end of the quarter. When you're working with a partner, each person should save their own, individual copy

Part 1: Code Tracing: Instance Variables, Simple Outputbb

 

Download the file the InstVar_Demo_2.java file (note the _2 in the name - this is the second file, not the first), and trace the program from start to finish.  Make sure that you keep an extra column in your trace table for the TRUE/FALSE answers to the while loop, a column for the output,  a column for the value of mary.howManyThingsPickedUp, and a column for the value of maxDistance.  Notice that we're not going to use the normal 'counting loop' pattern here, but you should be able to figure out exactly what's going on by tracing the code.

 

Part 2: Finding Errors: Instance Variables

 

Download the file the ICE_06_Errors_2.java file, and find all the errors that prevent the program from running correctly.  You should keep a list of the errors using an Error Table (which you also download from the website)

 

Part 3: Writing Code: Instance Variables

 

Continue to work using your (now fixed) ICE_06_Errors_2.java file.  You should add to your robot the ability to keep track of the number of left turns that the robot makes.  This will end up looking pretty similar to the previous ICE, in that you'll need a method named leftTurnCounted which both make a left turn and remember that the robot has made that turn, and another method named printNumberOfLeftTurns, which will actually tell you, the user, how many turns have been made since the robot was created.

 

Part  4: A Robot That Remembers Where It Started

            Using the Starting Template, create a robot that will remember where it was created.  In order to do this, you'll need two separate storage places – one integer for the avenue, and one for the street.  You should create a new command, named RememberLocation, that copies the street and avenue that the robot is currently on into these variables.  You should then create another method, named PrintStartingLocation  which will print out a message telling the user the street and avenue that the robot started on.  Make sure that you call RememberLocation in main before moving the robot!

            For example, if the Robot starts off at the intersection (1,2), and you call RememberLocation there, and then the robot moves to (3,5), the PrintStartingLocation command might print:

 

This Robot was created at street #1, avenue #2

 

HINT: You can get the robot’s current avenue using the getAvenue() command, and the current street using the getStreet() command.

 

Part  5: A Robot That Knows How Far It’s Traveled (Optional)

 

            Create a robot that will remember where it was created – you can either start from the Starting Template, or make a copy of the program you wrote in the previous exercise.  You should then create a method, named PrintDistanceMoved, which will print out a message telling the user how far the robot has moved from the starting point.

            For example, if the Robot starts off at the intersection (1,2), and then moves to (3,5), the message might print:

 

This Robot has moved:

2 streets south

3 avenues east

 

HINT: We don’t care what path the robot takes – we only care how much distance it has covered since it was created.

 

HINT: You can get the robot’s current avenue using the getAvenue() command, and the current street using the getStreet() command.

 

 

Part  6: Instance Variables: A Battery Powered Robot  (Optional)

 

You should create a robot that can only do 20 actions before 'running out of batteries'.  The possible actions that the robot can do are: moving forwards 1 intersection, turning left 90 degrees, picking a Thing up, or putting a Thing down.  After doing 20 actions, you (as the programmer) need to call a command (method) named Recharge on the robot, after which time it will be able to do 20 more actions.

 

I'd encourage you to try and solve this on your own, and use the below hints as needed.  Please name your new robot type RechargableRobot.

 

HINT: If you're not 100% certain of what to do, it would be a good idea to think through your approach, then run it by someone else.  The instructor wouldn't be a bad choice, although running it by other people in the class should be helpful, too.

 

HINT: In order to accomplish this, you'll need to keep count of the number of actions that the robot has done.  This counter obviously can't disappear between commands.

 

HINT: The counter needs to be an instance variable.

 

HINT: You'll need to write your own versions of move(), turnLeft(), etc.  Name them all something like moveBattery(), turnLeftBattery(), etc.  We'll see a better way to do this in this course, soon.

 

HINT: Yes, calling the normal move() allows one to get around all the work we're doing.  For now, ignore that problem.

 

HINT: You'll probably need to initialize the instance variable to be something in the constructor of the RechargableRobot.  Although you can get around doing this, I'd recommend it, just for the practice.

 

Part 7: Practice With Instance and Local Variables (Optional)

 

            Create a new type of robot, called a IceCubeBot, which will dispense ice cubes to people want some ice – kinda like a self-mobile ice box.  The robot should start out with a limited number of ice cubes (say, 15), and provides the following method:

 

public void DispenseIce(int howManyCubes)

 

            Each time the robot 'dispenses' an ice cube, the Robot should print out a message saying so ("Here's some ice!"), and then put a Thing down in the intersection.  It should put a number of ice cubes down specified by howManyCubes.  The robot should not dispense more ice cubes than it starts with.

            In order to accomplish this, you'll have to use both instance and local variables in various places  Make sure to comment each local or instance variable, explaining why you chose to make the variable a local (or instance), as opposed to an instance (or local) variable.

            Once you've got that working, add a parameter to the constructor, so that you can create two different IceCubeBots in main – one of which starts with 5 cubes, the other with 10.