Ice01 Instance Variables

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

ICE ICE 12.1: Finding Errors: Instance Variables

Download the file, find and fix all the errors that prevent the program from running correctly.

You should keep a list of the errors using an Program Debugging Table (which you also download from the website)

When you get done with this exercise, go on to the next section. Go Next

ICE ICE 12.2: Writing Code: Instance Variables

Continue to work using your (hopefully now fixed) file.

You should add to your Robot the ability to keep track of the number of left turns that the Robot makes.

You'll need to create 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 the user how many turns have been made since the robot was created.

When you get done with this exercise, show your Instructor, then go on to the next section. Go Next

ICE ICE 12.3: A Robot That Remembers Where It Started

Using the file as a start, create a robot that will remember where it was created (on what street and avenue)

HINTNOTE: The file above is a new type of starting template in that it also extends the Robot class and sets up the Scanner class and creates a keyboard object. Use this special template or alter it as you see fit (you don't even have to use the Scanner object if you don't want to), but you may want to remember it's location or save for future use, as this will be the only time this special Starting Template will be offered.

In order to do this, you'll need to create two (2) separate storage places—one integer for the Avenue, and one integer for the Street.

HINTHINT: Creating two instance variables would work well for this! Remember that instance variables are storage locations that are declared inside a class but outside the methods (and outside the constructor as well).

private int iStartedOnStreet = 0;
private int iStartedOnAvenue = 0;

You should create a new method named rememberLocation() that copies the street and avenue that the robot is currently on into these variables.

HINTHINT: Using the getStreet() and getAvenue() methods to assign these values would work nicely! Perhaps something along these lines:

this.iStartedOnStreet = this.getStreet();
this.iStartedOnAvenue = this.getAvenue();

You should then include some code that would print out the starting location that 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 you start 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 message might print:

The Robot started on Street 1 and Avenue 2.

HINTHINT: Using System.out.println to print out the message and starting street and avenue location would work nicely here. Perhaps something along these lines:

System.out.println("The Robot started on Street " + this.iStartedOnStreet +
" and Avenue " + this.iStartedOnAvenue + ".");

When you get done with this exercise, show your Instructor, then go on to the next section. Go Next

ICE ICE 12.4: A Robot That Knows How Far It's Traveled

Create a robot that will remember where it was created – you can either start with a new Starting Template, or make a copy of the program you wrote in the previous exercise and rename it (remember to rename the class name in main as well).

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

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

HINTHINT: You can get the robot’s current street using the getStreet() command and the current avenue using the getAvenue() command. A good way to do this is to assign these values to a couple of new local variables declared inside of the PrintDistanceMoved() method. Perhaps something along these lines:

int tempStreet = this.getStreet();
int tempAve = this.getAvenue();

HINTHINT: You might create two more local variables (e.g., movedStreets and movedAvenues) to hold the data that is the result of subtracting the starting street from tempStreet and the starting avenue from tempAvenue. Perhaps something along these lines:

int movedStreets = tempStreet - this.iStartedOnStreet;
int movedAvenues = tempAve - this.iStartedOnAvenue;

Finally, you would then print out the data from movedStreets and movedAvenues with some appropriate messages. Perhaps something along these lines:

System.out.println("This Robot has moved: ");
System.out.println(movedStreets + " Streets South");
System.out.println(movedAvenues + " Avenues East");

When you get done with this exercise, show your Instructor, then go on to the next section. Go Next

ICE ICE 12.5: Instance Variables: A Battery-Powered Robot

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, picking a Thing up, or putting a Thing down.

After doing 20 actions, you (as the programmer) need to call a method named recharge() on the robot, after which time it will be able to do 20 more actions.

public void Recharge() // recharge the robot by setting actions back to zero
    this.numOfActions = 0;
    System.out.println("Battery Recharged!!!");

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.

HINTHINT: 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.

HINTHINT: In order to accomplish this, you'll need to keep count of the number of actions that the robot has done and also the maximum number of actions the robot should do. These countesr obviously can't disappear between commands, so using instance variables works well here. Perhaps something along these lines:

private int numOfActions; // number of actions executed instance-variable
private int maxNumOfActions; // maximum number of actions instance-variable

HINTHINT: You'll need to write your own versions of move(), turnLeft(), etc. Name them all something like moveBattery(), turnLeftBattery(), etc. (we'll learn a better way to do this in an upcoming lecture). Perhaps something along these lines:

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

HINTBIG HINT: You'll probably want to initialize the instance variable to an extra something added to the constructor of the RechargableRobot. Although you can get around doing this, I'd recommend it, just for the practice. Here's how this constructor might look:

RechargableRobot( City c, int st, int ave, Direction dir, int num, int numActions) // <-- Notice the additional argument numActions
    super(c, st, ave, dir, num); // You don't add numActions here because that is not part of the original Robot superclass
    this.maxNumOfActions = numActions; // set the max number of actions 

Then, down in main, when you create your new object from the RechargableRobot class, you would add a sixth argument to its parameter, something like this:

RechargableRobot Jo = new RechargableRobot(toronto, 3, 0, Direction.EAST, 0, 20); // 20 is the maximum number of actions

HINTHINT: For each of your new methods that will track the various Robot actions--like moveBattery(), turnLeftBattery(), pickThingBattery(), putThingBattery(), etc--you can use various if statements to compare whether numOfActions is greater-or-equal-to maxNumOfActions, do something if true, and then at the end increment numOfActions by 1. Here is an example how one of these methods, moveBattery(), might look:

public void moveBattery() // battery counting move

    if( this.numOfActions >= this.maxNumOfActions )

    this.numOfActions = this.numOfActions + 1;

When you get done with this exercise, show your Instructor, then go on to the next section. Go Next

ICE ICE 12.6: Practice with Instance Variables: IceCubeBot (Optional)

Create a new type of robot called a IceCubeBot, which will dispense ice cubes (Things) 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 (example "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 variables 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 (2) different IceCubeBots in main – one of which starts with 15 cubes, the other with only 10.

When you get done with this exercise, show your Instructor.