Big Java 4

Chapter 8 – Designing Classes

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?

Designing Good Methods - Cohesion

Designing Good Methods - Cohesion

Designing Good Methods - Cohesion

Minimizing Dependencies

Minimizing Dependencies

Separating Accessors and Mutators

 

Separating Accessors and Mutators

Minimizing Side Effects

Minimizing Side Effects

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 is it a good idea to minimize dependencies between 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?

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 Student class of Chapter 7. Suppose we add a method
void read(Scanner in)
{
   while (in.hasNextDouble())
      addScore(in.nextDouble());
}
Does this method have a side effect other than mutating the data set?

Consistency

While it is possible to eat with mismatched silverware, consistency is more pleasant.


matching siverware is more pleasant

Problem Solving: Patterns for Object Data - Keeping a Total

All classes that manage a total follow the same basic pattern

Problem Solving: Patterns for Object Data - Counting Events

A counter that counts events is incremented in methods that correspond to the events.

Problem Solving: Patterns for Object Data - Collecting Values

An object can collect other objects in an array or array list.

Problem Solving: Patterns for Object Data - Collecting Values

Problem Solving: Patterns for Object Data - Managing Properties of an Object

Problem Solving: Patterns for Object Data - Modeling Objects with Distinct States

Problem Solving: Patterns for Object Data - Modeling Objects with Distinct States

Problem Solving: Patterns for Object Data - Describing the Position of an Object

Problem Solving: Patterns for Object Data - Describing the Position of an Object

Self Check 8.10

Suppose we want to count the number of transactions in a bank account in a statement period, and we add a counter to the BankAccount class:
public class BankAccount
{
   private int transactionCount;
   ...
}
In which methods does this counter need to be updated?

Self Check 8.11

In How To 3.1, the CashRegister class does not have a getTotalPurchase method. Instead, you have to call receivePayment and then giveChange. Which recommendation of Section 8.2.4 does this design violate? What is a better alternative?

Self Check 8.12

In the example in Section 8.3.3, why is the add method required? That is, why can’t the user of a Question object just call the add method of the ArrayList<String> class?

Self Check 8.13

Suppose we want to enhance the CashRegister class in How To 3.1 to track the prices of all purchased items for printing a receipt. Which instance variable should you provide? Which methods should you modify?

Self Check 8.14

Consider an Employee class with properties for tax ID number and salary. Which of these properties should have only a getter method, and which should have getter and setter methods?

Self Check 8.15

Suppose the setName method in Section 8.3.4 is changed so that it returns true if the new name is set, false if not. Is this a good idea?

Self Check 8.16

Look at the direction instance variable in the bug example in Section 8.3.6. This is an example of which pattern?

Static Variables and Methods - Variables

Static Variables and Methods

Static Variables and Methods

staic and instance variables
A Static Variable and Instance Variables

Static Variables and Methods - Methods

Static Variables and Methods

Self Check 8.17

Name two static variables of the System class.

Self Check 8.18

Name a static constant of the Math class

Self Check 8.19

The following method computes the average of an array of numbers:
public static double average(double[] values)
Why should it not be defined as an instance method?

Self Check 8.20

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?

Packages

Organizing Related Classes into Packages

orgazing into related groups

 

In Java, related classes are grouped into packages.

Organizing Related Classes into Packages

Importing Packages

Package Names

Syntax 8.1 Package Specification

package syntax

Packages and Source Files

Self Check 8.21

Which of the following are packages?
a. java
b. java.lang
c. java.util
d. java.lang.Math

Self Check 8.22

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

Self Check 8.23

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?

Unit Test Frameworks

Unit Test Frameworks

Self Check 8.24

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

Self Check 8.25

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