Chapter 2b

Implementing Classes


Chapter Goals

Black Boxes

Levels of abstraction: A Real Life Example


Levels of abstraction: A Real Life Example

Levels of abstraction: Software Design


Levels of abstraction: Software Design

Self Check

  1. In Chapters 1 and 2, you used System.out as a black box to cause output to appear on the screen. Who designed and implemented System.out?
  2. Suppose you are working in a company that produces personal finance software. You are asked to design and implement a class for representing bank accounts. Who will be the users of your class?

Answers

  1. The programmers who designed and implemented the Java library
  2. Other programmers who work on the personal finance application

Designing the Public Interface of a Class

Behavior of bank account (abstraction):

Designing 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());

Designing the Public Interface of a Class: Method Definition

Examples:

Syntax 3.1: Method Definition

accessSpecifier returnType methodName(parameterType parameterName, . . .)
{
method body }

Example:

 
public void deposit(double amount)
{
. . .
}

Purpose:

To define the behavior of a method

Designing the Public Interface of a Class: Constructor Definition

Syntax 3.2 : Constructor Definition

 
accessSpecifier ClassName(parameterType parameterName, . . .)
{
constructor body
}

Example:

 
public BankAccount(double initialBalance)
{
. . .
}

Purpose:

To define the behavior of a constructor

BankAccount Public Interface

The public constructors and methods of a class form the public interface of the class.
public class BankAccount
{
// 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 } // private fields--filled in later }

Syntax 3.3 : Class Definition

 
accessSpecifier class ClassName
{
   constructors
   methods
   fields
}

Example:

 
public class BankAccount
{
public BankAccount(double initialBalance) { . . . }
public void deposit(double amount) { . . . }
. . .
}

Purpose:

To define a class, its public interface, and its implementation details

Self Check

  1. How can you use the methods of the public interface to empty the harrysChecking bank account?
  2. Suppose you want a more powerful bank account abstraction that keeps track of an account number in addition to the balance. How would you change the public interface to accommodate this enhancement?

Answers

  1. harrysChecking.withdraw(harrysChecking.getBalance())
  2. Add an accountNumber parameter to the constructors, and add a getAccountNumber method. There is no need for a setAccountNumber method–the account number never changes after construction.

Commenting the Public Interface

/**
Withdraws money from the bank account.
@param 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


Javadoc Method Detail


Self Check

  1. Suppose we enhance the BankAccount class so that each account has an account number. Supply a documentation comment for the constructor BankAccount(int accountNumber, double initialBalance)
  2. Why is the following documentation comment questionable?
    /**
    Each account has an account number.
    @return the account number of this account.
    */
    int getAccountNumber()

Answers

  1. /**
    Constructs a new bank account with a given initial balance.
    @param accountNumber the account number for this account
    @param initialBalance the initial balance for this account
    */
  2. The first sentence of the method description should describe the method–it is displayed in isolation in the summary table

Instance Fields

Instance Fields


Instance Fields


 Syntax 3.4 : Instance Field Declaration

 
accessSpecifier class ClassName
{
   . . . 
   accessSpecifier fieldType fieldName;
. . .
}

Example:

 
public class BankAccount
{
. . .
private double balance;
. . .
}

Purpose:

To define a field that is present in every object of a class

Accessing Instance Fields

Self Check

  1. Suppose we modify the BankAccount class so that each bank account has an account number. How does this change affect the instance fields?
  2. What are the instance fields of the Rectangle class?

Answers

  1. An instance field
    private int accountNumber;
    needs to be added to the class
  2. private int x;
    private int y;
    private int width;
    private int height;

Implementing Constructors

Constructor Call Example

Implementing Methods

Method Call Example

Syntax 3.5: The return Statement

  return expression;
 or
return;

Example:

  return balance;

Purpose:

To specify the value that a method returns, and exit the method immediately. The return value becomes the value of the method call expression.

File BankAccount.java

Self Check

  1. How is the getWidth method of the Rectangle class implemented?
  2. How is the translate method of the Rectangle class implemented?

Answers

  1. public int getWidth()
    {
    return width;
    }
  2. There is more than one correct answer. One possible implementation is as follows:
    public void translate(int dx, int dy)
    {
    int newx = x + dx;
    x = newx;
    int newy = y + dy;
    y = newy;
    }

Testing a Class

File BankAccountTester.java


Output:

   1500

Testing With BlueJ


Self Check

  1. When you run the BankAccountTester program, how many objects of class BankAccount are constructed? How many objects of type BankAccountTester?
  2. Why is the BankAccountTester class unnecessary in development environments that allow interactive testing, such as BlueJ?

Answers

  1. One BankAccount object, no BankAccountTester object. The purpose of the BankAccountTester class is merely to hold the main method
  2. In those environments, you can issue interactive commands to construct BankAccount objects, invoke methods, and display their return values

Categories of Variables

Lifetime of Variables

harrysChecking.deposit(500);
double newBalance = balance + amount;
balance = newBalance;

Self Check

  1. What do local variables and parameter variables have in common? In which essential aspect do they differ?
  2. During execution of the BankAccountTester program in the preceding section, how many instance fields, local variables, and parameter variables were created, and what were their names?

Answers

  1. Variables of both categories belong to methods–they come alive when the method is called, and they die when the method exits. They differ in their initialization. Parameter variables are initialized with the call values; local variables must be explicitly initialized.
  2. One instance field, named balance. Three local variables, one named harrysChecking and two named newBalance (in the deposit and withdraw methods); two parameter variables, both named amount (in the deposit and withdraw methods).

Implicit and Explicit Method Parameters

Implicit Parameters and this

Implicit Parameters and this

Self Check

  1. How many implicit and explicit parameters does the withdraw method of the BankAccount class have, and what are their names and types?
  2. In the deposit method, what is the meaning of this.amount? Or, if the expression has no meaning, why not?
  3. How many implicit and explicit parameters does the main method of the BankAccountTester class have, and what are they called?

Answers

  1. One implicit parameter, called this, of type BankAccount, and one explicit parameter, called amount, of type double.
  2. It is not a legal expression. this is of type BankAccount and the BankAccount class has no field named amount.
  3. No implicit parameter–the method is static–and one explicit parameter, called args.