Java template for Entrypublic class Entry { private Date date; private double distance; private int durationInMinutes; private String postRunFeeling; public EntryDate date, double distan
Trang 1Methods and Containment in Unions
Week 9, 10
HOW TO DESIGN CLASS
HIERARCHIES
Basic Java Programming
Course material developed by Mai Anh Tho (tho@hcmuaf.edu.vn)
Trang 2Managing a Runner’s Logs
Example
Trang 3Recall the problem of tracking a
runner’s workouts
• Develop a program that manages a
runner's training log Every day the runner enters one entry concerning the day's
run .For each entry, the program should compute how fast the runner ran (Exercise 3.1.4 & 3.1.5 in week 1) .The runner
may also wish to determine the total
number of miles run
Trang 4• Draw a class diagram for a runner’s log
Trang 5Class diagram for a runner’s log
Trang 6Add methods to the runner’s log
- ALog rest + ??? nnn(??)
11
Trang 7• Write Java method templates for all the classes in the class diagram
Trang 8Java template for Date
this day = day;
this month = month;
this year = year;
Trang 9Java template for Entry
public class Entry {
private Date date;
private double distance;
private int durationInMinutes;
private String postRunFeeling;
public Entry(Date date, double distance, int durationInMinutes, String postRunFeeling) {
this date = date;
this distance = distance;
this durationInMinutes = durationInMinutes;
this postRunFeeling = postRunFeeling;
Trang 10Java template for ALog
}
Trang 11Java template for MTLog
Trang 12Java template for ConsLog
this first = first;
this rest = rest;
Trang 13• Give some examples for a runner’s log How many examples are at least needed?
Trang 14Examples for a runner’s log
Date d1 = new Date(5, 5, 2005);
Date d2 = new Date(6, 6, 2005);
Date d3 = new Date(23, 6, 2005);
Entry e1 = new Entry(d1, 5.0, 25, "Good");
Entry e2 = new Entry(d2, 3.0, 24, "Tired");
Entry e3 = new Entry(d3, 26.0, 156, "Great");
Alog l1 = new MTLog();
Alog l2 = new ConsLog(e1,l1);
Alog l3 = new ConsLog(e2,l2);
Alog l4 = new ConsLog(e3,l3);
Trang 15• Using the method template for ALog, design a method to compute the total number of miles run
Trang 16miles() for ALog
// to compute the total number of miles recorded in this log
}
Trang 17• Develop some examples to test the miles() method
Trang 18Examples to test miles()
Date d1 = new Date(5, 5, 2005);
Date d2 = new Date(6, 6, 2005);
Date d3 = new Date(23, 6, 2005);
Entry e1 = new Entry(d1, 5.0, 25, "Good");
Entry e2 = new Entry(d2, 3.0, 24, "Tired");
Entry e3 = new Entry(d3, 26.0, 156, "Great");
ALog l1 = new MTLog();
ALog l2 = new ConsLog(e1, l1);
ALog l3 = new ConsLog(e2, l2);
ALog l4 = new ConsLog(e3, l3);
l1.miles() should be 0.0
l2.miles() should be 5.0
l3.miles() should be 8.0
l4.miles() should be 34.0
Trang 19• Implement miles() in MTLog and ConsLog
Trang 21miles() in ConsLog
public class ConsLog extends ALog {
private Entry first;
private ALog rest;
public ConsLog(Entry first, ALog rest) {
this first = first;
this rest = rest;
}
public double miles() {
return this first.getDistance() + this rest.miles(); }
}
Trang 22getDistance() in Entry
public class Entry {
private Date date;
private double distance;
private int durationInMinutes;
private String postRunFeeling;
…
public double getDistance() {
return this distance;
}
}
Trang 23Extension of the runner’s log problem
• The runner wants to see his log for a
• Q: Design a method to fulfill this
requirement in ALog
Trang 24getLog() for ALog
public abstract class ALog {
// to compute the total number of miles recorded in this log
public abstract double miles();
// to extract those entries in this log for the given month and year
public abstract ALog getLog( int month, int year);
}
Trang 25• Develop some examples to test the getLog() method
Trang 26Examples to test getLog()
Date d1 = new Date(5, 5, 2005);
Date d2 = new Date(6, 6, 2005);
Date d3 = new Date(23, 6, 2005);
Entry e1 = new Entry(d1, 5.0, 25, "Good");
Entry e2 = new Entry(d2, 3.0, 24, "Tired");
Entry e3 = new Entry(d3, 26.0, 156, "Great");
Alog l1 = new MTLog();
Alog l2 = new ConsLog(e1,l1);
Alog l3 = new ConsLog(e2,l2);
Alog l4 = new ConsLog(e3,l3);
l1.getLog(6,2005) should be new MTLog()
l2.getLog(6,2005) should be new MTLog()
l3.getLog(6,2005) should be new ConsLog(e2, new MTLog()) l3.getLog(5,2005) should be new ConsLog(e1, new MTLog()
Trang 27getLog() for MTLog
public class MTLog extends ALog {
public MTLog() { }
public double miles() {
return 0.0;
}
public ALog getLog( int month, int year) {
return new MTLog();
}
}
Trang 28getLog() for ConsLog
public class ConsLog extends ALog {
private Entry first;
private ALog rest;
public ConsLog(Entry first, ALog rest) {
this first = first;
this rest = rest;
}
public double miles() {
return this first.getDistance() + this rest.miles();
Trang 29sameMonthInAYear() in Entry
public class Entry {
private Date date;
private double distance;
private int durationInMinutes;
private String postRunFeeling;
…
public double getDistance() {
return this distance;
}
// was this entry made in the given month and year
public boolean sameMonthInAYear(int month, int year) {
return ( this date.sameMonthInAYear(month, year); }
}
Trang 30sameMonthInAYear() in Date
private int day;
private int month;
private int year;
public Date(int day, int month, int year) {
this day = day;
this month = month;
this year = year;
}
return (this.month == month) && (this.year == year);
}
}
Trang 31ALog class diagram
11
ALog
<<abstract>> double miles()
<<abstract>> ALog getLog(int month, int year)
<<abstract>>
ConsLog
- Entry first
- ALog rest + double miles() + ALog getLog(int month, int year)
11 11
Trang 32Exercises
Trang 33Exercise 6.1.1
• Collect all the pieces of getLog() and
insert the method definitions in the class hierarchy for logs Develop examples for
sameMonthInAYear() and include them
with the test suite Draw the class diagram for this hierarchy
Trang 34Exercise 6.1.2
• Suppose the requirements for the program that tracks a runner's log includes this
request:
The runner wants to know the total
Design the method that computes this
number and add it to the class hierarchy of exercise 6.1.1
Trang 35Exercise 6.1.3
• Suppose the requirements for the program that tracks a runner's log includes this request:
A runner wishes to know the maximum
distance ever run .
Design the method that computes this number and add it to the class hierarchy of
exercise 6.1.1
Assume that the method produces 0 if the log is empty.
Trang 36Sorting Example
Trang 37Problem Statement
The runner would like to see the log with entries ordered according to the distance covered in each run, from the shortest to the longest distance
• Q: Which class should this operation
belong to?
Trang 39sortByDistance() in ALog
public abstract class ALog {
// to create from this log a new log with entries sorted by distance
public abstract ALog sortByDistance();
}
Trang 40Date d1 = new Date(5, 5, 2005);
Date d2 = new Date(6, 6, 2005);
Date d3 = new Date(23, 6, 2005);
Entry e1 = new Entry(d1, 5.0, 25, "Good");
Entry e2 = new Entry(d2, 3.0, 24, "Tired");
Entry e3 = new Entry(d3, 26.0, 156, "Great");
Alog l1 = new MTLog();
Alog l2 = new ConsLog(e1,l1);
Alog l3 = new ConsLog(e2,l2);
Alog l4 = new ConsLog(e3,l3);
l1.sortByDistance() should be l1
l2.sortByDistance() should be l2
l4.sortByDistance() should be new ConsLog(e2,
new ConsLog(e1, new ConsLog(e3, new MTLog()))
Trang 41}
Trang 42sortByDistance() in ConsLog
public class ConsLog extends ALog {
private Entry first;
private ALog rest;
Trang 43• Which class should insertInDistanceOrder()
operation belong to?
Trang 44<<abstract>> + ALog sortByDistance()
<<abstract>> ALog insertInDistanceOrder(Entry)
Trang 45insertInDistanceOrder() in ALog
public abstract class ALog {
// to create from this log a new log with entries sorted by distance
public abstract ALog sortByDistance ();
// insert the given entry into this sorted log
public abstract ALog insertInDistanceOrder (Entry e);
}
Trang 46Examples for insertInDistanceOrder()
ALog l5 = new ConsLog(new Entry(new Date(1,1,2005), 25, 4.9, "good"),
new ConsLog(new Entry(new Date(1,2,2005), 26, 4.9, "okay"), new MTLog()))
l5.insertInDistanceOrder(new Entry(new Date(1,3,2005), 25.5, 4.9, "great"))
// should be
new ConsLog(new Entry(new Date(1,1,2005), 25 , 4.9, "good"),
new ConsLog(new Entry(new Date(1,3,2005), 25.5, 4.9, "great"),
new ConsLog(new Entry(new Date(1,2,2005), 26 , 4.9, "okay"), new MTLog())))
l5.insertInDistanceOrder(new Entry(new Date(1,4,2005), 4.9, 26.5, "all right!"))
// should be
new ConsLog(new Entry(new Date(1,1,2005), 25 , 4.9, "good"),
new ConsLog(new Entry(new Date(1,2,2005), 26 , 4.9, "okay"),
new ConsLog(new Entry(new Date(1,4,2005), 26.5, 4.9, "all right!"), new MTLog())))
l5.insertInDistanceOrder(new Entry(new Date(1,5,2005), 24.0, 4.9, "bad"))
// should be
new ConsLog(new Entry(new Date(1,5,2005), 24.0, 4.9, "bad")
new ConsLog(new Entry(new Date(1,1,2005), 25 , 4.9, "good"), new ConsLog(new Entry(new Date(1,2,2005), 26 , 4.9, "okay"), , new MTLog())))
Trang 47protected ALog insertInDistanceOrder(Entry e) {
return new ConsLog(e, this );
}
}
Trang 48insertInDistanceOrder() in ConsLog
public class ConsLog extends ALog {
private Entry first;
private ALog rest;
…
public ALog sortByDistance() {
return this rest.sortByDistance().insertInDistanceOrder( this first);
}
protected ALog insertInDistanceOrder(Entry e) {
if (e hasDistanceShorterThan ( this first))
return new ConsLog(e, this );
else
return new ConsLog( this first, this rest.insertInDistanceOrder(e)); }
}
Trang 49hasDistanceShorterThan() in Entry
public class Entry {
private Date date;
private double distance;
private int durationInMinutes;
private String postRunFeeling;
…
public boolean hasDistanceShorterThan(Entry entry) {
return this distance < entry.distance;
}
}
Trang 50Exercises
Trang 516.2.1 Exercise
• Suppose the requirements for the program that tracks a runner’s log includes this
request:
The runner would like to see the log with
entries ordered according to the pace
from the fastest to the slowest
Design this sorting method Hint: Don’t forget to design methods for auxiliary tasks.
Trang 52Overlapping Shapes Example
Trang 53Remind The Problem
• Develop a drawing program that deals with at least three kinds of shapes: dots, squares, and circles .In addition, the program should also deal with overlaying shapes on each other In the following figure, for example, we have superimposed a circle on the right side of a square:
• We could now also superimpose(thêm vào) this
compounded shape on another shape and so on
Trang 54Recall the current design
CartesianPoint
- int x
- int y + int getX() + int getY() + double distanceToO() + double distanceTo(CartesianPoint that) + void translate(int dx, int dy)
AShape
# CartesianPoint location
# AShape(CartesianPoint location) + double distanceToO()
+ double area() + boolean contains(CartesianPoint point) + Rectangle boundingBox()
Trang 55Design 1
CartesianPoint
- int x
- int y + int getX() + int getY() + double distanceToO() + double distanceTo(CartesianPoint that) + void translate(int dx, int dy)
Trang 56Design 2
CartesianPoint
- int x
- int y + int getX() + int getY() + double distanceToO() + double distanceTo(CartesianPoint that) + void translate(int dx, int dy)
Trang 57+ double distanceTo(CartesianPoint that)
+ void translate(int dx, int dy)
+ double area() + boolean contains(CartesianPoint point) + Rectangle boundingBox()
AShape + double distanceToO() 22
Trang 58• Q: Which design do you prefer?
• A: I prefer Design 4 Why?
– Design 1 has an EmptyShape, but in this case a shape (regardless being composite or not) is
always non-empty
– Design 2 has a flaw: there is no explicit end (of course you could use null)
– For Design 3, when having the new class
Composite I just have to change a class name
and add some classes more without changing the current design
Trang 59AShape s1 = new Square( new CartesianPoint(4, 3), 40); AShape s2 = new Square( new CartesianPoint(3, 4), 50); AShape c1 = new Circle( new CartesianPoint(0, 0), 20); AShape c2 = new Circle( new CartesianPoint(12, 5), 20); AShape u1 = new CompositeShape(s1, s2);
AShape u2 = new CompositeShape(s1, c2);
AShape u3 = new CompositeShape(c1, u1);
AShape u4 = new CompositeShape(u3, u2);
}
}
Trang 60The user wishes to know how close a combination of shapes is
to the origin
Trang 61Modified Class Diagram
+ double distanceTo(CartesianPoint that)
+ void translate(int dx, int dy)
+ double area() + boolean contains(CartesianPoint point) + Rectangle boundingBox()
AShape + double distanceToO() 22
Trang 62distanceToO() in CompositeShape
this top = top;
this bottom = bottom;
}
this bottom.distanceToO());
}
}
Trang 63Problem 2
Add a method that determines
whether some point in the Cartesian space falls within the boundaries of some shape .
Trang 64CartesianPoint location ASingleShape(CartesianPoint location) double distanceToO()
double area() boolean contains(CartesianPoint point)
opname()
Rectangle boundingBox() double perimeter()
Dot Square
int size
Circle int radius
Rectangle int width int height
double distanceTo(CartesianPoint that)
void translate(int dx, int dy)
11
AShape double distanceToO() boolean contains(CartesianPoint point)
CompositeShape AShape top
AShape bottom double distanceToO() boolean contains(CartesianPoint point)
22
Modified Class Diagram
Trang 65contains() in CompositeShape
this top = top;
this bottom = bottom;
}
…
this bottom.contains(point);
}
}
Trang 66Problem 3
A graphics program must
compute the bounding box for a shape
Trang 67Bounding box for Composite shape
Y
a b
c
c r
(x1, y1)
(x2, y2)
(x3, y3) (x1+a, y1+b)
(x3+c, y3+c)
Trang 68boundingBox() in CompositeShape
this top = top;
this bottom = bottom;
Trang 69River Systems Example
Trang 70m(7, 5) a(5, 5)
u(3, 7)
t(1, 5) s(1, 1)
b(3, 3)
Confluence
Source
Trang 71Class Diagram (Version 1)
11
Trang 73• Q: Which version do you prefer, Version 1
or Version 2? Why?
• The previous designs need to be modified
in case that a confluence may be made
from more than two streams
Trang 74Problem 1
The EPA must represent river systems and monitor them…An EPA officer may wish to query a computer about the number of
sources that feed a river system…
Trang 75Problem 2
An EPA officer may wish to find out
whether some location is a part of a river
system, regardless of whether it is a source, a confluence, or the river mouth .
Trang 76Problem 3
An EPA officer may request the
number of miles of a river system,
either starting from the river's mouth or any of its confluence points .
Trang 77Exercises
Trang 78Exercise 6.5.1
Design a data representation for shopping lists Start from the class of grocery items developed in exercise 5.1.2 Add the following methods:
• howMany, which computes the number of items
on the shopping list;
• brandList, which produces the list of all brand names; and
• highestPrice, which determines the highest unit price among all items in the shopping list.
Trang 79Exercise 5.1.2 Class Diagram
AnItem # String branchName # double weight # double price + double unitPrice() + boolean lowerPrice(double amount) + boolean cheaperThan(AnItem that)()