Big Java book Cover

Chapter 3 – Implementing Classes

Instance Variables and Encapsulation

Instance Variables

Instance Variables

Instance Variables

Figure 2 - Instance Variables
Figure 2 Instance Variables

Syntax 3.1 Instance Variable Declaration

Syntax 3.1 Instance Variable Declaration

Instance Variables

clocks

 

These clocks have common behavior, but each of them has a different state. Similarly, objects of a class can have their instance variables set to different values.

Encapsulation

Encapsulation

thermostat

 

A thermostat functions as a "black box" whose inner workings are hidden.

Question 1 Feb 11

Suppose you are using a class Clock with private instance variables hours and minutes. How can you print the hours variable in from the main method of your program?
  1. System.out.println(hours);
  2. System.out.print(Clock.hours);
  3. Create a clock object, myClock, then use System.out.print(myClock.hours);
  4. You can only print hours using a public method of the clock class.

Specifying the Public Interface of a Class - BankAccount

Specifying the Public Interface of a Class - BankAccount

Specifying the Public Interface of a Class: Method Declaration

Specifying the Public Interface of a Class

Specifying Constructors

Specifying Constructors: BankAccount

Specifying Constructors: BankAccount

BankAccount Public Interface

The constructors and methods of a class go inside the class declaration:
public class BankAccount
{
   // private instance 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
   }
}

Specifying the Public Interface of a Class

Syntax 3.2 Class Declaration

Syntax 3.2 Class Declaration

Using the Public Interface

Commenting the Public Interface

Commenting the Public Interface - Documenting a method

Commenting the Public Interface - Documenting a method

Commenting the Public Interface - Documenting a class

Method Summary

screen shot of method summary
Figure 3 A Method Summary Generated by javadoc

Method Details

method details
Figure 4 Method Detail Generated by javadoc

Question 2 Feb 11

How can you use the public interface of BankAccount class to empty the harrysChecking bank account?
  1. harrysChecking.withdraw(harrysChecking.balance)
  2. harrysChecking.withdraw(harrysChecking.getBalance())
  3. harrysChecking.withdraw(BankAccount.getBalance())
  4. withdraw(harrysChecking.getBalance())

Self Check 3.7

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

Providing the Class Implementation

Providing Instance Variables

Providing Instance Variables

backpacker analogy with instance variables

 

Like a wilderness explorer who needs to carry all items that may be needed, an object needs to store the data required for its method calls.

Providing Constructors

Providing Constructors - Tracing the Statement

Steps carried out when the following statement is executed:

BankAccount harrysChecking = new BankAccount(1000);  

Providing Constructors - Tracing the Statement

diagram of constructor
Figure 5 How a Constructor Works

Providing Constructors

Assembling a house

 

A constructor is like a set of assembly instructions for an object.

Providing Methods

Providing Methods - continued

Table 1 Implementing Classes

implementing classes

Self Check 3.12

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;  
   } 
}

Unit Testing

Unit Testing

Unit Testing

engineer testing a part

 

An engineer tests a part in isolation.
This is an example of unit testing.

Unit testing with Bluej

unit testing with Bluej
Figure 6 The Return Value of the getBalance Method in BlueJ

section_4/BankAccountTester.java

Your browser does not support the <object> tag.

Program Run:

Unit Testing - Building a program

Self Check 3.15

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

Self Check 3.16

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

Problem Solving: Tracing Objects

Problem Solving: Tracing Objects

Problem Solving: Tracing Objects

Problem Solving: Tracing Objects

Self Check 3.17

Consider a Car class that simulates fuel consumption in a car. We will assume a fixed efficiency (in miles per gallon) that is supplied in the constructor. There are methods for adding gas, driving a given distance, and checking the amount of gas left in the tank. Make a card for a Car object, choosing suitable instance variables and showing their values after the object was constructed.

Self Check 3.18

Trace the following method calls:

Car myCar(25);
myCar.addGas(20);
myCar.drive(100);
myCar.drive(200);
myCar.addGas(5); 

Self Check 3.19

odometer

Suppose you are asked to simulate the odometer of the car, by adding a method getMilesDriven.
Add an instance variable to the object’s card that is suitable for computing this method’s result.


Self Check 3.20

Trace the methods of Self Check 18, updating the instance variable that you added in Self Check 19

Local Variables

Local Variables

Self Check 3.21

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

The this Reference

The this Reference

The this Reference

The this Reference

The this Reference

Figure 8 - The Implicit Parameter of a Method Call
Figure 7 The Implicit Parameter of a Method Call

The this Reference

The this Reference

Question 1 Feb 13

How many implicit and explicit parameters does the withdraw method of the BankAccount class have?
  1. One implicit parameter, one explicit parameter
  2. No implicit parameter, one explicit parameter
  3. One implicit parameter, no explicit parameter
  4. No implicit parameter, no explicit parameter

Self Check 3.25

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

Question 2 Feb 13

How many implicit and explicit parameters does the main method of the BankAccountTester class have?
  1. One implicit parameter, one explicit parameter
  2. No implicit parameter, one explicit parameter
  3. One implicit parameter, no explicit parameter
  4. No implicit parameter, no explicit parameter

Shape Classes

Plan Complex Shapes on Graph Paper

Figure 10 - Using Graph Paper to Find Shape Coordinates

Figure 9 Using Graph Paper to Find Shape Coordinates

Drawing Cars

Goal: draw two cars - one in top-left corner of window, and another in the bottom right
Figure 9 - The Car Component Draws Two Car Shapes
Figure 8 The Car Component Draws Two Car Shapes

Drawing Cars

The program that produces the drawing is composed of three classes:

Drawing Cars

section_8/Car.java

Your browser does not support this feature

section_8/CarComponent.java

Your browser does not support this feature

section_8/CarViewer.java

Your browser does not support this feature

Self Check 3.27

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

Self Check 3.28

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.29

How do you make the cars twice as big?