Big Java Chapter 8 PowerPoint
Big Java Chapter 8 PowerPoint
Chapter Goals
Discovering Classes
A class represents a single concept from the problem domain.
Name for a class should be a noun that describes concept.
Concepts from mathematics:
Point
Rectangle
Ellipse
Discovering Classes
Actors (end in -er, -or) objects do some kinds of work for
you:
Scanner
Random // Better name: RandomNumberGenerator
Minimizing Dependencies
A class depends on another class if its methods use that class
in any way.
CashRegister depends on Coin
10
Minimizing Dependencies
11
Minimizing Dependencies
Example: printing BankAccount balance
Recommended
System.out.println("The balance is now $" +
momsSavings.getBalance());
12
13
14
15
16
17
18
19
20
21
22
23
24
Does this method have a side effect other than mutating the
data set?
Answer: Yes the method affects the state of the Scanner
argument.
25
Consistency
While it is possible to eat with mismatched silverware, consistency
is more pleasant.
26
27
28
29
You may need a method to report the count to the user of the class.
30
31
32
33
Some properties should not change after they have been set in
the constructor
Dont supply a setter method
public class Student
{
private int id;
...
public Student(int anId) { id = anId; }
public String getId() { return id; }
// No setId method
...
}
Copyright 2014 by John Wiley & Sons. All rights reserved.
34
Problem Solving: Patterns for Object Data Modeling Objects with Distinct States
Some objects have behavior that varies depending on what has
happened in the past.
If a fish is in a hungry state, its behavior changes.
35
Problem Solving: Patterns for Object Data Modeling Objects with Distinct States
Supply constants for the state values:
public static final int NOT_HUNGRY = 0;
public static final int SOMEWHAT_HUNGRY = 1;
public static final int VERY_HUNGRY = 2;
36
Problem Solving: Patterns for Object Data Modeling Objects with Distinct States
Determine where the state affects behavior:
public void move()
{
if (hungry == VERY_HUNGRY)
{
Look for food.
}
...
}
37
Problem Solving: Patterns for Object Data Describing the Position of an Object
To model a moving object:
You need to store and update its position.
You may also need to store its orientation or velocity.
If the object moves along a line, you can represent the position
as a distance from a fixed point:
private double distanceFromTerminus;
38
Problem Solving: Patterns for Object Data Describing the Position of an Object
A bug in a grid needs to store its row, column, and direction.
There will be methods that update the position. You may be told
how much the object moves:
public void move(double distanceMoved)
{
distanceFromTerminus =
distanceFromTerminus + distanceMoved;
}
39
Problem Solving: Patterns for Object Data Describing the Position of an Object
If the movement happens in a grid, you need to update the row
or column, depending on the current orientation.
public void moveOneUnit()
{
if (direction == NORTH) { row--; }
else if (direction == EAST) { column++; }
else if (direction == SOUTH) { row++; }
else if (direction == WEST) { column; }
}
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Packages
Package: Set of related classes
Important packages in the Java library:
59
60
61
62
Importing Packages
Can use a class without importing: refer to it by its full name
(package name plus class name):
java.util.Scanner in = new java.util.Scanner(System.in);
Inconvenient
import directive lets you refer to a class of a package by its
class name, without the package prefix:
import java.util.Scanner;
Now you can refer to the class as Scanner without the package
prefix.
Can import all classes in a package:
import java.util.*;
63
Package Names
Use packages to avoid name clashes:
java.util.Timer
vs.
javax.swing.Timer
64
65
66
67
68
69
70
Philosophy:
Whenever you implement a class, also make a companion test class.
Run all tests whenever you change your code.
71
72
73
74
75