CSE 131 Module 4: Encapsulation

Practice Problems


Directions: Consider the following class definition and then answer the questions below. (We recommend doing these practice exercises on paper. Do not bother typing them into a computer. You'll learn more doing them by hand.)

public class Account {
   private int balance;

   public Account(int openingBalance) {
      balance = openingBalance;
   }

   public void deposit(int dollars) {
      balance = balance + dollars;
   }

   public boolean withdraw(int dollars) {
      if (dollars <= balance) {
         balance = balance - dollars;
         return true;
      } else
         return false;
   }

   public boolean transfer (int dollars, Account destination) {
      if (withdraw(dollars)) {
         destination.deposit(dollars);
         return true;
      } else
         return false;
   }

   public String toString() {
      return ("$" + balance + ".00");
   }
}

  1. What output would be printed as the result of executing the following code?
    Account alice, bob;                             // line 1
    alice = new Account(100);                       // line 2
    alice.deposit(25);                              // line 3
    System.out.println("Alice has " + alice);
    bob = new Account(100);                         // line 5
    System.out.println("Bob can withdraw $125.00? " + bob.withdraw(125));
    System.out.println("Bob can withdraw $25.00? " + bob.withdraw(25));
    System.out.println("Bob has " + bob);
    Account charlie = alice;                        // line 9
    System.out.println("Charlie can withdraw $25.00? " + charlie.withdraw(25));
    System.out.println("Charlie has " + charlie);
    System.out.println("Alice has " + alice);
    alice.transfer(50, charlie);
    System.out.println("After transfer...");
    System.out.println("Charlie has " + charlie);
    System.out.println("Alice has " + alice);
    

  2. During the execution of the code in Problem 1, how many Account reference variables have been (cumulatively) declared after completing execution of lines 1, 2, 3, 5, and 9? (That is, hand simulate the code, and at each numbered line, say how many Account reference variables have been declared up to that point.)

  3. During the execution of the code in Problem 1, how many Account objects have been instantiated after completing execution of lines 1, 2, 3, 5, and 9?

  4. What would be printed as the result of executing the following code? Explain.
    System.out.println("This account has " + (new Account(60)));
    

  5. In Problem 4, what happens to the Account object created? If we added some code right after that line, would that code be able to use the Account object that was created?

  6. What's wrong with the following?
    Account foo;            // line 1
    foo.withdraw(25);       // line 2
    
    Suggest a line that could be added between lines 1 and 2 in order to avoid the error on line 2. (Alternatively, suggest a modification to line 1 that would avoid the problem.)

Solutions