Big Java 3

Chapter 8 – Designing Classes

Chapter Goals

Choosing Classes

Choosing Classes

Self Check 8.1

What is the rule of thumb for finding classes?

Self Check 8.2

Your job is to write a program that plays chess. Might ChessBoard be an appropriate class? How about MovePiece?

Cohesion

Cohesion

Coupling

Coupling

High and Low Coupling Between Classes

Self Check 8.3

Why is the CashRegister class from Chapter 4 not cohesive?

Self Check 8.4

Why does the Coin class not depend on the CashRegister class?

Self Check 8.5

Why should coupling be minimized between classes?

Accessors, Mutators and Immutable Classes

Self Check 8.6

Is the substring method of the String class an accessor or a mutator?

Self Check 8.7

Is the Rectangle class immutable?

Side Effects

Side Effects

Self Check 8.8

If a refers to a bank account, then the call a.deposit(100) modifies the bank account object. Is that a side effect?

Self Check 8.9

Consider the DataSet class of Chapter 6. Suppose we add a method
void read(Scanner in)
{
   while (in.hasNextDouble())
      add(in.nextDouble());
}
Does this method have a side effect?

Common Error: Trying to Modify Primitive Type Parameters

Common Error: Trying to Modify Primitive Type Parameters

double savingsBalance = 1000;
harrysChecking.transfer(500, savingsBalance);
System.out.println(savingsBalance);
...
void transfer(double amount, double otherBalance)
{
   balance = balance - amount;
   otherBalance = otherBalance + amount;
}

Common Error: Trying to Modify Primitive Type Parameters

double savingsBalance = 1000;
harrysChecking.transfer(500, savingsBalance);
System.out.println(savingsBalance);
...
void transfer(double amount, double otherBalance)
{
   balance = balance - amount;
   otherBalance = otherBalance + amount;
}

Common Error: Trying to Modify Primitive Type Parameters

double savingsBalance = 1000;
harrysChecking.transfer(500, savingsBalance);
System.out.println(savingsBalance);
...
void transfer(double amount, double otherBalance)
{
   balance = balance - amount;
   otherBalance = otherBalance + amount;
}

Common Error: Trying to Modify Primitive Type Parameters

double savingsBalance = 1000;
harrysChecking.transfer(500, savingsBalance);
System.out.println(savingsBalance);
...
void transfer(double amount, double otherBalance)
{
   balance = balance - amount;
   otherBalance = otherBalance + amount;
}

Animation 8.1 – Trying to Modify Primitive Type Parameters

Link to Flash animation

Call by Value and Call by Reference

Call by Value Example

harrysChecking.transfer(500, savingsAccount);

Preconditions

Preconditions

Preconditions

Syntax 8.1 Assertion

assert condition;

Example:

assert amount >= 0;

Purpose:

To assert that a condition is fulfilled. If assertion checking is enabled and the condition is false, an assertion error is thrown.

Postconditions

Self Check 8.10

Why might you want to add a precondition to a method that you provide for other programmers?

Self Check 8.11

When you implement a method with a precondition and you notice that the caller did not fulfill the precondition, do you have to notify the caller?

Static Methods

Self Check 8.12

Suppose Java had no static methods. Then all methods of the Math class would be instance methods. How would you compute the square root of x?

Self Check 8.13

Harry turns in his homework assignment, a program that plays tic-tac-toe. His solution consists of a single class with many static methods. Why is this not an object-oriented solution?

Static Fields

Static Fields

A Static Field and Instance Fields

Self Check 8.14

Name two static fields of the System class.

Self Check 8.15

Harry tells you that he has found a great way to avoid those pesky objects: Put all code into a single class and declare all methods and fields static. Then main can call the other static methods, and all of them can access the static fields. Will Harry's plan work? Is it a good idea?

Scope of Local Variables

Scope of Local Variables

Scope of Class Members

Overlapping Scope

Self Check 8.16

Consider the deposit method of the BankAccount class. What is the scope of the variables amount and newBalance?

Self Check 8.17

What is the scope of the balance field of the BankAccount class?

Organizing Related Classes into Packages

Important Packages in the Java Library

Package Purpose Sample Class
java.lang Language support Math
java.util Utilities Random
java.io Input and output PrintStream
java.awt Abstract Windowing Toolkit Color
java.applet Applets Applet
java.net Networking Socket
java.sql Database Access ResultSet
javax.swing Swing user interface JButton
org.omg.CORBA Common Object Request Broker Architecture IntHolder

Syntax 8.2 Package Specification

package packageName;

Example:

package com.horstmann.bigjava;

Purpose:

To declare that all classes in this file belong to a particular package.

Importing Packages

Package Names and Locating Classes

Base Directories and Subdirectories for Packages

Self Check 8.18

Which of the following are packages?
  1. java
  2. java.lang
  3. java.util
  4. java.lang.Math

Self Check 8.19

Is a Java program without import statements limited to using the default and java.lang packages?

Self Check 8.20

Suppose your homework assignments are located in the directory /home/me/cs101 (c:\me\cs101 on Windows). Your instructor tells you to place your homework into packages. In which directory do you place the class hw1.problem1.TicTacToeTester?

The Explosive Growth of Personal Computers

Unit Testing Frameworks

Self Check 8.21

Provide a JUnit test class with one test case for the Earthquake class in Chapter 5.

Self Check 8.22

What is the significance of the EPSILON parameter in the assertEquals method?