CS 49J - Lecture 6

Cover page image

Cay S. Horstmann

Homework Show and Tell

GridWorld

BoxBug

Critters

Crab Critters

Account Puzzle

What is wrong?

public class Account
{
   private double balance;
   public Account() { balance = 0; }
   public double getBalance() { return balance; }
   public void deposit(double amount) { balance += amount; }
   public void withdraw(double amount) { balance -= amount; }
}

public class CheckingAccount extends Account // Charges $1 per withdrawal
{
   private double balance;
   public void withdraw(double amount)
   {
      balance = balance - amount - 1;
   }
}

Account Puzzle 2

public class Account
{
   private double balance;
   public Account() { balance = 0; }
   public double getBalance() { return balance; }
   public void deposit(double amount) { balance += amount; }
   public void withdraw(double amount) { balance -= amount; }
}

public class CheckingAccount extends Account // Charges $1 per withdrawal
{
   private double balance;
   public void withdraw(double amount)
   {
      balance = balance - amount - 1;
   }
}

Account Puzzle 3

What is wrong?

public class Account
{
   private double balance;
   public Account() { balance = 0; }
   public double getBalance() { return balance; }
   public void deposit(double amount) { balance += amount; }
   public void withdraw(double amount) { balance -= amount; }
}

public class CheckingAccount extends Account // Charges $1 per withdrawal
{
   private double balance;
   public void withdraw(double amount)
   {
      withdraw(amount + 1);
   }
}

Account Puzzle 4

class Account
{
   private double balance;
   public String type = "Generic";
   public Account() { balance = 0; }
   public double getBalance() { return balance; }
   public void deposit(double amount) { balance += amount; }
   public void withdraw(double amount) { balance -= amount; }
}

class CheckingAccount extends Account // Charges $1 per withdrawal
{
   public String type = "Checking";
   public void withdraw(double amount)
   {
      super.withdraw(amount + 1);
   }
}