CPSC 124, Winter 1998
Sample Answers to Lab 6


This page contains sample answers to some of the exercises from Lab #6 in CPSC 124: Introductory Programming, Winter 1998. See the information page for that course for more information.


Exercise 1: A simple bank account class to go along with the given main program could be written as follows:


class BankAccount {
     // An object of this class represents a bank account.  The balance
     // and interest rate are stored as instance variables in the object.
     // The object can handle deposits and withdrawals.  It also can add
     // one month's interest on the current balance.  (Note: this way
     // of calculating interest is not really correct, since it doesn't
     // take into account the fact that the balance can change over the
     // course of a month.)

   private double monthlyInterestRate;  // Monthly interest rate, given as
                                        // a decimal (such as 0.05) rather
                                        // than a percentage.

   private double balance;  // Amount of money currently in the account

   BankAccount(double initialBalance, double annualInterestRate) {
         // The constructor initializes the instance variables with the
         // values provided in the parameters initialBalance and annualRate.
         // Note that the monthly interest rate is 1/12 of the specified
         // annual rate.
      monthlyInterestRate = annualInterestRate / 12;
      balance = initialBalance;
   }

   double getBalance() {
         // Returns the current balance in the account.
      return balance;
   }

   void deposit(double amount) {
         // Adds the specified amount to the current account balance.
      balance += amount;
   }

   void withdraw(double amount) {
         // Subtracts the specified amount from the current account balance.
      balance -= amount;
   }

   void doMonthlyInterest() {
         // Adds one month's interest to the current account balance.
      balance = balance * (1 + monthlyInterestRate);
   }

} // end of class BankAccount

Exercise 2: The original exercise 2 was canceled (and became the third individual programming assignment for the course). The following essay question was substituted:

The BankAccount class that you wrote contains two instance variables, representing the balance and the interest rate for the account. Why is it a good idea for these variables to be declared as private? What, in general, is the effect of declaring a variable to be private, and under what circumstances do you think should this be done?

Sample Answer: If a variable is declared to be private, then it is not directly accessible from anywhere outside the class where it is defined. This gives the person who writes that class complete control over what can be done with the variable. The only methods that can ever manipulate the variable are methods in that class. This does not necessarily mean that the variable "can't be changed" or "can't be seen" from outside the class. It can be changed if the person who writes the class provides a method for changing it, such as the deposit() and withdraw() methods in the BankAccount class. The point is that even if such a method is provided, the variable can't be changed except by calling that method.

The sample BankAccount class is really too simple and unrealistic for it to make much difference whether the balance and interest rate are declared to be private. However, in a class that was meant to be used in actual banking software, it would be very useful to use private variables. Suppose that other classes could directly change the value of the balance. Now suppose you want to modify your banking software system so that any change to the balance of an account is simultaneously recorded to an auditing file. You would have to search through every line of code in the entire system to find all the places where the balance is changed. On the other hand, if the balance variable is declared to be private in the BankAccount class, then all you have to do is modify the methods in that single class. Since the balance can only be changed by calling those methods, any change to the balance will then be properly recorded in the audit file.


David Eck, 20 February 1998