CS111 Lab 12 Answers

Task 1: SquareBuggleWorld

class SquareBuggle extends Buggle {
  
  // Instance Variables
  private int sizeOfLastSquare;
  private int numberSquaresDrawn;
  
  // Default Constructor
  public SquareBuggle () {
    // Initialize the instance variables.
    sizeOfLastSquare = 0;
    numberSquaresDrawn = 0;
  }
  
  // Draw a square with sides of length size, for size>0.
  // Drops the number of squares drawn in the initial corner when done.
  public void square (int size) {
    if (size > 0) { // draw a square
      if (size == 1) { // square of size one
        paintCell(getColor()); // paint cell the color of the Buggle
      } else { // size > 1
        for (int i = 0; i < 4; i++) { // draw side and turn left four times
          forward(size - 1);
          left();
        }
      }
      sizeOfLastSquare = size; // store the size of the square drawn
      numberSquaresDrawn = numberSquaresDrawn + 1; // increment the number of squares drawn
      dropInt(numberSquaresDrawn); // drop number in corner of square
    }
  }
  
  // Draw the same square as the last square drawn (same side length).
  public void repeat () {
    square(sizeOfLastSquare);
  }
  
}

Task 2: HurdleWorld

class Hurdler extends Buggle {
  
  public int numHurdles;
  
  public Hurdler() {
    numHurdles = 0;
  }
    
  
  public void running() {
    if (atFinishLine()) {
      //do nothing
    } else if (isFacingWall()) {
      jumpHurdle();
      numHurdles = numHurdles + 1;
      running();
    } else {
      forward();
      running();
    }
    
  }
  
  public boolean atFinishLine() {
  // Return true if buggle is facing wall (as opposed to hurdle) and false otherwise.
  // Should leave buggle in same position and heading as when it starts.
  // Should not leave any marks behind.
    boolean result;
    if (isFacingWall()) {
      brushUp();
      left();
      forward();
      right();
      result = isFacingWall();
      left();
      backward();
      right();
      brushDown();
    } else {
      result = false;
    }
    return result;
  }
  
  public void jumpHurdle() {
      left();
      forward();
      right();
      forward();
      right();
      forward();
      left();
  }
    
}

Task 3: BankAccount

BankAccount interface

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); 
}

SimpleBankAccount1

Implementing BankAccount as three instance integer instance variables savings, checking, and total.
public class SimpleBankAccount1 implements BankAccount {
  // Instance Variables
  private int savings, checking;   // can declare variables of same type on same line
  private int total;               // or on a separate line
  
  // Default Constructor
  public SimpleBankAccount1 () {
    savings = 0;
    checking = 0;
    total = 0;
  }
  
  // Public Instance Methods
  public int getSavings () {
    return savings;
  }
  
  public int getChecking () {
    return checking;
  }
  
  public int getTotal () {
    return total;
  }
  
  public void depositToSavings (int amount) {
    savings = savings + amount;
    total = total + amount;
  }
  
  public void transferFromSavingsToChecking (int amount) {
    if (amount <= savings) {
      savings = savings - amount;
      checking = checking + amount;
    }
  }
  
  public void withdrawFromChecking (int amount) {
    if (amount <= checking) {
      checking = checking - amount;
      total = total - amount;
    }
  }
}

SimpleBankAccount2

Implementing BankAccount as two instance integer instance variables savings and total.
public class SimpleBankAccount2 implements BankAccount {
  // Instance Variables
  private int savings, total;
  
  // Default Constructor
  public SimpleBankAccount2 () {
    savings = 0;
    total = 0;
  }
  
  // Public Instance Methods
  public int getSavings () {
    return savings;
  }
  
  public int getChecking () {
    return total - savings;
  }
  
  public int getTotal () {
    return total;
  }
  
  public void depositToSavings (int amount) {
    savings = savings + amount;
    total = total + amount;
  }
  
  public void transferFromSavingsToChecking (int amount) {
    if (amount <= savings) {
      savings = savings - amount;
    }  
  }
  
  public void withdrawFromChecking (int amount) {
    if (amount <= getChecking()) {
      total = total - amount;
    }  
  }
}

SimpleBankAccount3

Implementing BankAccount as an IntList accountInfo with three nodes which hold the values of the savings account, checking account, and total of both accounts.
public class SimpleBankAccount3 implements BankAccount {
  // Instance Variables
  private IntList accountInfo;
  
  // Default Constructor
  public SimpleBankAccount3 () {
    accountInfo = prepend(0, prepend(0, prepend(0, empty())));
  }
  
  // Public Instance Methods
  public int getSavings () {
    return head(accountInfo);
  }
  
  public int getChecking () {
    return head(tail(accountInfo));
  }
  
  public int getTotal () {
    return head(tail(tail(accountInfo)));
  }
  
  public void depositToSavings (int amount) {
    accountInfo = prepend(getSavings()+amount,
                    prepend(getChecking(),
                      prepend(getTotal()+amount, empty())));
  }
  
  public void transferFromSavingsToChecking (int amount) {
    if (amount <= getSavings()) {
      accountInfo = prepend(getSavings()-amount,
                      prepend(getChecking()+amount, tail(tail(accountInfo))));
    }
  }
  
  public void withdrawFromChecking (int amount) {
    if (amount <= getChecking()) {
      accountInfo = prepend(getSavings(),
                      prepend(getChecking()-amount,
                        prepend(getTotal()-amount, empty())));
    }
  }
}

SimpleBankAccount4

Implementing BankAccount as an integer array accountInfo with three slots which hold the values of the savings account, checking account, and total of both accounts.
public class SimpleBankAccount4 implements BankAccount {
  // Instance Variables
  private int[] accountInfo;
  
  // Default Constructor
  public SimpleBankAccount4 () {
    accountInfo = new int[3];
    for (int i = 0; i < 4; i++) { // initialize values in array; actually not necessary
      accountInfo[i] = 0;            // since Java initializes integers to zero
    }
  }
  
  // Public Instance Methods
  public int getSavings () {
    return accountInfo[0];
  }
  
  public int getChecking () {
    return accountInfo[1];
  }
  
  public int getTotal () {
    return accountInfo[2];
  }
  
  public void depositToSavings (int amount) {
    accountInfo[0] = getSavings() + amount;
  }
  
  public void transferFromSavingsToChecking (int amount) {
    if (amount <= getSavings()) {
      accountInfo[0] = getSavings() - amount;
      accountInfo[1] = getChecking() + amount;
    }
  }
  
  public void withdrawFromChecking (int amount) {
    if (amount <= getChecking()) {
      accountInfo[1] = getChecking() - amount;
      accountInfo[2] = getSavings() + getChecking();
    }
  }
}

SimpleBankAccount5

Implementing BankAccount as a Vector accountInfo with two elements which hold the values of the savings and checking accounts.
public class SimpleBankAccount5 implements BankAccount {
  // Instance Variables
  private Vector accountInfo;
  
  // Default Constructor
  public SimpleBankAccount5 () {
    accountInfo = new Vector();
    accountInfo.addElement(new Integer(0));
    accountInfo.addElement(new Integer(0));
  }
  
  // Public Instance Methods
  public int getSavings () {
    return ((Integer)accountInfo.elementAt(0)).intValue();
  }
  
  public int getChecking () {
    return ((Integer)accountInfo.elementAt(1)).intValue();
  }
  
  public int getTotal () {
    return getSavings() + getChecking();
  }
  
  public void depositToSavings (int amount) {
    accountInfo.setElementAt(new Integer(getSavings()+amount), 0);
  }
  
  public void transferFromSavingsToChecking (int amount) {
    if (amount <= getSavings()) {
      accountInfo.setElementAt(new Integer(getSavings()-amount), 0);
      accountInfo.setElementAt(new Integer(getChecking()+amount), 1);
    }
  }
  
  public void withdrawFromChecking (int amount) {
    if (amount <= getChecking()) {
      accountInfo.setElementAt(new Integer(getChecking()-amount), 1);
    }
  }
}