CS 46A - Lecture 6

Cover page image

Pre-class reading

Reminders

Instance Variables

Instance Variables

Instance Variables

Instance Variables

Figure 2 - Instance Variables

Syntax 3.1 Instance Variable Declaration

Syntax 3.1 Instance Variable Declaration

Accessing Instance Variables

Encapsulation

Lectrure 6 Clicker Question 1

Consider the Counter class, that Coder Carla refines over time. Suppose Programmer Paul's code uses that class, and in his code, the statement

System.out.println(someCounter.getValue())

prints a negative number.  Which of the following statements applies?

  1. This can never happen. A counter’s value starts at 0 and is advanced by the count method, so it should never be negative.
  2. This is a consequence of integer overflow.
  3. This is caused by a bug in Carla's code
  4. This is caused by a bug in Paul's code

Specifying the Public Interface of a Class

Behavior of bank account (abstraction):

Specifying the Public Interface of a Class: Methods

Methods of BankAccount class:

We want to support method calls such as the following:

harrysChecking.deposit(2000);
harrysChecking.withdraw(500);
System.out.println(harrysChecking.getBalance());

Specifying the Public Interface of a Class: Method Declaration

Specifying the Public Interface of a Class: Method Header

Examples:

Specifying the Public Interface of a Class: Constructor Declaration

BankAccount Public Interface

The public constructors and methods of a class form the public interface of the class:

public class BankAccount
{
   // private variables--filled in later

   // Constructors
   public BankAccount()
   {
      // body--filled in later
   }
   public BankAccount(double initialBalance)
   {
      // body--filled in later
   }

   // Methods
   public void deposit(double amount)
   {
      // body--filled in later
   }
   public void withdraw(double amount)
   {
      // body--filled in later
   }
   public double getBalance()
   {
      // body--filled in later
   }
}

Syntax 3.2 Class Declaration

Syntax 3.2 Class Declaration

Lecture 6 Clicker Question 2

What does the following code print?

BankAccount harrysChecking = new BankAccount(10000); 
harrysChecking.withdraw(harrysChecking.getBalance());
harrysChecking.deposit(harrysChecking.getBalance());
System.out.println(harrysChecking.getBalance());

  1. 10000
  2. 0
  3. Something else
  4. Nothing—there is a compile-time error

Lecture 6 Clicker Question 3

Suppose you want a more powerful bank account abstraction that keeps track of an account number in addition to the balance. The following are proposed to accommodate this enhancement?

  1. Add a getAccountNumber method
  2. Add an accountNumber parameter to the constructor
  3. Add an accountNumber parameter to the getBalance method

Which should be adopted?

  1. a only
  2. b only
  3. a and b
  4. a, b, and c

Commenting the Public Interface

/**
   Withdraws money from the bank account.
   @param amount the amount to withdraw
*/
public void withdraw(double amount)
{
   //implementation filled in later 
}

/**
   Gets the current balance of the bank account.
   @return the current balance
*/
public double getBalance()
{
   //implementation filled in later
}

Class Comment

/**
   A bank account has a balance that can be changed by
   deposits and withdrawals.
*/
public class BankAccount
{
   . . .
}

Javadoc Method Summary

Figure 3 - A Method Summary Generated by javadoc

Javadoc Method Detail

Figure 4 - Method Detail Generated by javadoc

Self Check 3.9

Provide documentation comments for the Counter class of Section 3.1.

Lecture 6 Clicker Question 4

Why is the following documentation comment questionable?

/**
   Each account has an account number.
   @return the account number of this account
*/
public int getAccountNumber()
  1. The description does not specify the purpose of the method
  2. There is no @param statement
  3. The @return statement does not include the name of the parameter
  4. The @return statement does not end in a period