To become familiar with the process of implementing classes
To be able to implement and test simple methods
To understand the purpose and use of constructors
To understand how to access instance variables and local variables
To be able to write javadoc comments
To implement classes for drawing graphical shapes
Instance Variables and Encapsulation
Figure 1 Tally counter
Simulator statements:
Counter tally = new Counter();
tally.click();
tally.click();
int result = tally.getValue(); // Sets result to 2
Each counter needs to store a variable that keeps track of the number of simulated button clicks.
Instance Variables
Instance variables store the data of an object.
Instance of a class: an object of the class.
An instance variable is a storage location present in each object of the class.
The class declaration specifies the instance variables:
public class Counter
{
private int value;
...
}
An object's instance
variables store
the data required
for executing
its methods.
Instance Variables
An instance variable declaration consists of the following parts:
access specifier (private)
type of variable (such as int)
name of variable (such as value)
You should declare all instance variables as private.
Instance Variables
Each object of a class has its own set of instance variables.
Figure 2 Instance Variables
Syntax 3.1 Instance Variable Declaration
Instance Variables
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.
The Methods of the Counter Class
The click method advances the counter value by 1:
public void click()
{
value = value + 1;
}
Affects the value of the instance variable of the object on which the method is invoked
The method call concertCounter.click();
Advances the value variable of the concertCounter object
The Methods of the Counter Class
The getValue method returns the current value:
public int getValue()
{
return value;
}
The return statement
Terminates the method call
Returns a result (the return value) to the method's caller
Private instance variables can only be accessed by methods of the same class.
Encapsulation
Encapsulation is the process of hiding implementation details and providing methods for data access.
To encapsulate data:
Declare instance variables as private and
Declare public methods that access the variables
Encapsulation allows a programmer to use a class without having to know its implementation.
Information hiding makes it simpler for the implementor of a class to locate errors and change implementations.
Encapsulation
A thermostat functions as a "black
box" whose inner workings are hidden.
When you assemble classes, like Rectangle and String, into programs you are like a contractor installing a thermostat.
When you implement your own classes you are like the manufacturer who puts together a thermostat out of parts.
Supply the body of a method public void unclick() that undoes an unwanted
button click.
Answer:
public void unclick()
{
value = value - 1;
}
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?
Answer: You can only access them by invoking the methods of the Clock class.
Self Check 3.3
Consider the Counter class. A counter’s value starts at 0 and is advanced by the click method, so it should never be negative. Suppose you found a negative value variable during testing. Where would you look for the error?
Answer: In one of the methods of the Counter class.
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?
Answer: The programmers who designed and implemented the Java library.
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?
Answer: Other programmers who work on the personal finance application.
Specifying the Public Interface of a Class
In order to
implement a class,
you first need to
know which methods
are required.
Essential behavior of a bank account:
deposit money
withdraw money
get balance
Specifying the Public Interface of a Class
We want to support method calls such as the following:
Here are the method headers needed for a BankAccount class:
public void deposit(double amount)
public void withdraw(double amount)
public double getBalance()
Specifying the Public Interface of a Class: Method Declaration
A method's body consisting of statements that are executed when the method is called:
public void deposit(double amount)
{
implementation - filled in later
}
You can fill in the method body so it compiles:
public double getBalance()
{
// TODO: fill in implementation
return 0;
}
Specifying the Public Interface of a Class
BankAccount methods were declared as public.
public methods can be called by all other methods in the program.
Methods can also be declared private
private methods only be called by other methods in the same class
private methods are not part of the public interface
Specifying Constructors
Initialize objects
Set the initial data for objects
Similar to a method with two differences:
The name of the constructor is always the same as the name of the class
Constructors have no return type
Specifying Constructors: BankAccount
Two constructors
public BankAccount() public BankAccount(double initialBalance)
Usage
BankAccount harrysChecking = new BankAccount(); BankAccount momsSavings = new BankAccount(5000);
Specifying Constructors: BankAccount
The constructor name is always
the same as the class name.
The compiler can tell them apart because they take different arguments.
A constructor that takes no arguments is called a no-argument constructor.
BankAccount's no-argument constructor - header and body:
public BankAccount()
{
constructor body—implementation filled in later
}
The statements in the constructor body will set the instance variables of the object.
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
public constructors and methods of a class form the public interface of the class.
These are the operations that any programmer can use
Syntax 3.2 Class Declaration
Using the Public Interface
Example: transfer money
// Transfer from one account to another
double transferAmount = 500;
momsSavings.withdraw(transferAmount);
harrysChecking.deposit(transferAmount)
Programmers use objects of the BankAccount class to carry out meaningful tasks
without knowing how the BankAccount objects store their data
without knowing how the BankAccount methods do their work
Commenting the Public Interface
Use documentation
comments to
describe the classes
and public methods
of your programs.
Java has a standard form for documentation comments.
A program called javadoc can automatically generate a set of HTML pages.
Documentation comment
placed before the class or method declaration that is being documented
Commenting the Public Interface - Documenting a method
Start the comment with a /**.
Describe the method’s purpose.
Describe each parameter:
start with @param
name of the parameter that holds the argument
a short explanation of the argument
Describe the return value:
start with @return
describe the return value
Omit @param tag for methods that have no arguments.
Omit the @return tag for methods whose return type is void.
End with */
Commenting the Public Interface - Documenting a method
Example:
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
implementation—filled in later
}
Example:
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
implementation—filled in later
}
Commenting the Public Interface - Documenting a class
Place above the class declaration.
Supply a brief comment explaining the class's purpose.
Example:
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
. . .
}
Provide documentation
comments
for:
every class
every method
every parameter
variable
every return value
Method Summary
Figure 3 A Method Summary Generated by javadoc
Method Details
Figure 4 Method Detail Generated by javadoc
Self Check 3.6
How can you use the methods of the public interface to empty the harrysChecking
bank account?
BankAccount harrysChecking = new BankAccount(10000);
System.out.println(harrysChecking.withdraw(500));
Answer: The withdraw method has return type void. It doesn’t return a value. Use the getBalance method to obtain the balance after the withdrawal.
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?
Answer: 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.
Self Check 3.9
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)
Answer:
/**
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
*/
Self Check 3.10
Why is the following documentation comment questionable?
/**
Each account has an account number.
@return the account number of this account
*/
public int getAccountNumber()
Answer:
The first sentence of the method description should describe the method—it is displayed in isolation in the summary table.
Providing the Class Implementation
The private
implementation of
a class consists of:
instance variables
the bodies
of constructors
the bodies of methods.
Providing Instance Variables
Determine the data that each bank account object contains.
What does the object need to remember so that it can carry out its methods?
Each bank account object only needs to store the current balance.
BankAccount instance variable declaration:
public class BankAccount
{
private double balance;
// Methods and constructors below
. . .
}
Providing 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
Constructor's job is to initialize the instance variables of the object.
The no-argument constructor sets the balance to zero.
public BankAccount()
{
balance = 0;
}
The second constructor sets the balance to the value supplied as the construction argument.
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
Providing Constructors - Tracing the Statement
Steps carried out when the following statement is executed:
BankAccount harrysChecking = new BankAccount(1000);
Create a new object of type BankAccount.
Call the second constructor
because an argument is supplied in the constructor call
Set the parameter variable initialBalance to 1000.
Set the balance instance variable of the newly created object to initialBalance.
Return an object reference, that is, the memory location of the object.
Store that object reference in the harrysChecking variable.
Providing Constructors - Tracing the Statement
Figure 5 How a Constructor Works
Providing Constructors
A constructor is like a set of
assembly instructions for an object.
Suppose we modify the BankAccount class so that each bank account has an account number. How does this change affect the instance variables?
Answer: An instance variable needs to be added to the class:
private int accountNumber;
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;
}
}
Answer: Because the balance instance variable is accessed from the main method of BankRobber.
The compiler will report an error because main is not a method of the BankAccount class and main has no access to BankAccount instance variables.
Self Check 3.13
The Rectangle class has four instance variables: x, y, width, and height. Give a
possible implementation of the getWidth method.
Answer:
public int getWidth()
{
return width;
}
Self Check 3.14
Give a possible implementation of the translate method of the Rectangle class.
Answer: 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;
}
Unit Testing
BankAccount.java can not be executed:
It has no main method
Most classes do not have a main method
Before using BankAccount.java in a larger program:
You should test in isolation
Unit test: verifies that a class works correctly in isolation, outside
a complete program.
Unit Testing
To test a class, either
use an environment for interactive testing, or
write a tester
class
to execute test instructions.
Tester class: a class with a main method that contains statements to test
another class.
Typically carries out the following steps:
Construct one or more objects of the class that is being tested
Invoke one or more methods
Print out one or more results
Print the expected results
Unit Testing
An engineer tests a part in isolation.
This is an example of unit testing.
Unit Testing with Bluej
Figure 6 The Return Value of the getBalance Method in BlueJ
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.
Answer:
Self Check 3.18
Trace the following method calls:
Car myCar(25);
myCar.addGas(20);
myCar.drive(100);
myCar.drive(200);
myCar.addGas(5);
Answer:
Self Check 3.19
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.
Answer:
Self Check 3.20
Trace the methods of Self Check 18, updating the
instance variable that you added in Self Check 19.
Answer:
Local Variables
Local variables are declared in the body of a method:
When a method exits, its local variables are removed.
Parameter variables are declared in the header of a method:
public void enterPayment(double amount)
Local Variables
Local and parameter variables belong to methods:
When a method runs, its local and parameter variables come to life
When the method exits, they are removed immediately
Instance variables belong to objects, not methods:
When an object is constructed, its instance variables are created
The instance variables stay alive until no method uses the object any longer
Instance variables are initialized to a default value:
Numbers are initialized to 0
Object references are set to a special value called null
A null object reference refers to no object at all
You must initialize
local variables:
The compiler complains if you do not
Self Check 3.21
What do local variables and parameter variables
have in common? In which essential aspect do they differ?
Answer: 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.
Self Check 3.22
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;
Answer: After computing the change due, payment and purchase were set to zero. If the
method returned payment - purchase, it would always return zero.
Self Check 3.23
Consider a CashRegister object reg1 whose payment instance variable has the
value 20 and whose purchase instance variable has the value 19.5. Trace the call
reg1.giveChange(). Include the local variable change. Draw an X in its column
when the variable ceases to exist.
Answer:
The this Reference
Two types of inputs are passed when a method is called:
The object on which you invoke the method
The method arguments
In the call momsSavings.deposit(500) the method needs to know:
The account object (momsSavings)
The amount being deposited (500)
The implicit parameter of a method is the object on which the method is invoked.
All other
parameter variables are called
explicit parameters.