CS111 Lab 12 Exercises

Buggles with Memory: using Instance Variables

To start, download the lab12_programs folder from the CS111 download folder on nike.

Task 1: SquareBuggleWorld

In the SquareBuggleWorld folder, you will find a SquareBuggleWorld.java file. In this file, implement an extension of a Buggle which meets the following contract:

public void square(int n);
Draws a square of length n (if n>0). Drops the number of squares drawn into the initial square. Assume Buggle starts at bottom left corner of square.

public void repeat()
Draws the square of the same size as the last square drawn.

Note that this subclass of Buggle will require instance variables to keep track of the state of the Buggle. Think carefully about what instance variables are required. You will also need to define a default constructor method to initialize the instance variables.

Task 2: HurdleWorld

You are familiar with the HurdleWorld problem from Lab 5, in which a Buggle ran a race by moving east and jumping over 1-cell vertical walls (hurdles), to get to the finish line (righthand wall of the grid). In the implementation given here, the number of hurdles jumped is counted, and displayed in the output window at the end of the race.

For this, and for other Buggle Worlds you have seen, you have typically used parameters to keep track of the state of the Buggle during iteration or recursion.In this exercise, you will modify the Hurdler class in HurdleWorld.java to include an instance variable which stores the number of hurdles. You will need a Constructor method to initialize the instance variable, and you should modify the running() method to incorporate the use of the instance variable.

To begin, run the HurdleWorld.html file, and study the HurdleWorld.java code to understand how the existing program works.

Object Implementations and Contracts

Task 3: Bank Accounts

In lecture, you learned that an interface is a java keyword which specifies a contract for a class without an implementation. An interface is useful for data abstraction because you can understand how to use the interface without understanding the details of the actual implementation. You then have the flexibility of using any data structures you wish to define the actual object.

Given the following interface for a Bank Account:

public interface BankAccount {

//Constructor with no money in savings and checking

public int getSavings();

public int getChecking();

public int getTotal();

public void depositToSavings (int account);

public void transferFromSavingsToChecking(int account);

public void withdrawFromChecking(int amount);

}

Implement a bank account class using instance variables which are:

  • integers,
  • IntLists,
  • Arrays, and
  • Vectors.