Big Java 4

Chapter 8 – Designing Classes

Chapter Goals

Discovering Classes

Discovering 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

Dependency

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?

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

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 other than mutating the data set?

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 – A Method Cannot Modify a Numeric Parameter

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

Assertion

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

Static Methods

Self Check 8.12

Suppose Java had no static methods. How would you use the Math.sqrt method for computing the square root of a number x?

Self Check 8.13

The following method computes the average of an array list of numbers:
public static double average(ArrayList<Double> values)
Why must it be a static method?

Static Variables

A Static Variable and Instance Variables

Static Variables

Self Check 8.14

Name two static variables 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 variables static. Then main can call the other static methods, and all of them can access the static variables. Will Harry's plan work? Is it a good idea?

Scope of Local Variables

Scope of Local Variables

Overlapping Scope

Overlapping Scope

Self Check 8.16

Consider the following program that uses two variables named r. Is this legal?
public class RectangleTester 
{ 
   public static double area(Rectangle rect) 
   {  
      double r = rect.getWidth() * rect.getHeight(); 
      return r; 
   } 
   public static void main(String[] args) 
   { 
      Rectangle r = new Rectangle(5, 10, 20, 30); 
      double a = area(r); 
      System.out.println(r); 
   } 
}

Self Check 8.17

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

Packages

Organizing Related Classes into Packages

Syntax 8.2 Package Specification

Package Specification

Importing Packages

Package Names

Packages and Source Files

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:\Users\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

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?