Big Java 4

Chapter 3 – Implementing Classes

Chapter Goals

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

Self Check 3.1

Supply the body of a method public void reset() that resets the counter back to zero.

Self Check 3.2

Suppose you use a class Clock with private instance variables hours and minutes. How can you access these variables in your program?

Encapsulation

Self Check 3.3

Consider the Counter class. A counter’s value starts at 0 and is advanced by the count method, so it should never be negative. Suppose you found a negative value variable during testing. Where would you look for the error?

Self Check 3.4

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?

Self Check 3.5

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?

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

Self Check 3.6

How can you use the methods of the public interface to empty the harrysChecking bank account?

Self Check 3.7

What is wrong with this sequence of statements?
BankAccount harrysChecking = new BankAccount(10000); 
System.out.println(harrysChecking.withdraw(500));

Self Check 3.8

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?

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.

Self Check 3.10

Suppose we enhance the BankAccount class so that each account has an account number. Supply a documentation comment for the constructor
public BankAccount(int accountNumber, double initialBalance)

Self Check 3.11

Why is the following documentation comment questionable?
/**
   Each account has an account number.
   @return the account number of this account
*/
public int getAccountNumber()

Providing the Class Implementation

Implementing Constructors

Constructor Call Example

Syntax 3.3 Method Declaration

Syntax 3.3 Method Declaration

Implementing Methods

Method Call Example

Implementing Methods

ch03/account/BankAccount.java

Your browser does not support the <object> tag.

Self Check 3.12

Suppose we modify the BankAccount class so that each bank account has an account number. How does this change affect the instance variables?

Self Check 3.13

Why does the following code not succeed in robbing mom’s bank account?
public class BankRobber 
{ 
   public static void main(String[] args) 
   { 
      BankAccount momsSavings = new BankAccount(1000); 
      momsSavings.balance = 0;  
   } 
}

Self Check 3.14

The Rectangle class has four instance variables: x, y, width, and height. Give a possible implementation of the getWidth method.

Self Check 3.15

Give a possible implementation of the translate method of the Rectangle class.

Unit Testing

ch03/account/BankAccountTester.java

Your browser does not support the <object> tag. Program Run:

Unit Testing

Testing With BlueJ

Figure 6 - The Return Value of the getBalance Method in BlueJ

Self Check 3.16

When you run the BankAccountTester program, how many objects of class BankAccount are constructed? How many objects of type BankAccountTester?

Self Check 3.17

Why is the BankAccountTester class unnecessary in development environments that allow interactive testing, such as BlueJ?

Local Variables

Local Variables

Animation 3.1: Lifetime of Variables

Link to Flash animation

Self Check 3.18

What do local variables and parameter variables have in common? In which essential aspect do they differ?

Self Check 3.19

Why was it necessary to introduce the local variable change in the giveChange method? That is, why didn’t the method simply end with the statement
return payment - purchase;

Implicit Parameter

Implicit Parameters and this

Implicit Parameters and this

Figure 8 - The Implicit Parameter of a Method Call

Implicit Parameters and this

Implicit Parameters and this

Implicit Parameters and this

Self Check 3.20

How many implicit and explicit parameters does the withdraw method of the BankAccount class have, and what are their names and types?

Self Check 3.21

In the deposit method, what is the meaning of this.amount? Or, if the expression has no meaning, why not?

Self Check 3.22

How many implicit and explicit parameters does the main method of the BankAccountTester class have, and what are they called?

Shape Classes

Drawing Cars

Plan Complex Shapes on Graph Paper

Figure 10 - Using Graph Paper to Find Shape Coordinates

Classes of Car Drawing Program

ch03/car/Car.java

Your browser does not support the <object> tag.

ch03/car/CarComponent.java

Your browser does not support the <object> tag.

ch03/car/CarViewer.java

Your browser does not support the <object> tag.

Self Check 3.22

Which class needs to be modified to have the two cars positioned next to each other?

Self Check 3.23

Which class needs to be modified to have the car tires painted in black, and what modification do you need to make?

Self Check 3.24

How do you make the cars twice as big?

Drawing Graphical Shapes

Flag
Rectangle leftRectangle = new Rectangle(100, 100, 30, 60);
Rectangle rightRectangle = new Rectangle(160, 100, 30, 60);
Line2D.Double topLine = new Line2D.Double(130, 100, 160, 100);
Line2D.Double bottomLine = new Line2D.Double(130, 160, 160, 160);