3-Containment and Methods
3-Containment and Methods
Object Containment
and Methods
1
Runner's training log
• Develop a program that manages a runner's training
log. Every day the runner enters one entry
concerning the day's run. Each entry includes the
day's date, the distance of the day's run, the
duration of the run, and a comment describing the
runner's post-run feeling.
• Examples:
– on June 5, 2003: 5.3 miles in 27 minutes, feeling
good;
– on June 6, 2003: 2.8 miles in 24 minutes, feeling tired
– on June 23, 2003: 26.2 miles in 150 minutes, feeling
exhausted;
2
Class Diagram
Entry
Date
- Date date
- int day
- double distance
- int month
- int duration
- int year
- String comment
3
Define class and constructor
public class Entry {
private Date date; reference
private double distance;
private int duration;
private String comment;
public Entry(Date date, double distance, int duration,
String comment) {
this.date = date; public class Date {
this.distance = distance; private int day;
this.duration = duration; private int month;
this.comment = comment; private int year;
} public Date(int day, int month,
} int year) {
this.day = day;
this.month = month;
this.year = year;
}
}
4
The public or private modifiers
for attribute and method
• None modifier: Classes in the same package can
access this attribute / method.
10/24/22 6
Methods for containment
7
Add methods to the Entry
Entry
Date
- Date date
- int day
- double distance
- int month
- int duration
- int year
- String comment
??? kkk(???)
??? nnn(???)
8
Java template for Entry
public class Entry {
private Date date;
private double distance;
private int duration;
private String comment;
public Entry(Date date, double distance, int duration,
String comment) {
this.date = date;
this.distance = distance;
this.duration = duration;
this.comment = comment;
}
Entry
Date
- Date date
- int day
- double distance
- int month
- int duration
- int year
- String comment
??? pace(???)
11
Design pace() method
• Purpose and contract (method signature)
// computes the pace for a daily entry
public double pace()
• Examples
– new Entry(new Date(5, 6, 2004), 5.3, 27,
"good").pace() should produce 5.094
– new Entry(new Date(6, 6, 2004), 2.8, 24,
"tired").pace() should produce 8.571
– new Entry(new Date(23, 6, 2004), 26.2, 159,
"exhausted").pace() should produce 6.069
12
Design pace() method (con't)
Template
// computes the pace for a daily entry
public double pace() {
...this.date...
...this.duration...
...this.distance...
...this.comment...
}
Implement
// computes the pace for a daily entry
public double pace() {
return this.duration / this.distance;
}
13
Design pace() method (con't)
• Unit testing
pubblic class EntryTest extends TestCase {
...
14
Compare Date: early than
• A runner's log refers to Dates and a natural question
concerning comparing dates is when one occurs earlier
than another one.
Develop a method that determines whether one date
occurs earlier than another date.
• Hint:
– The first possibility is that the first date is in the year preceding
the other.
– Next, if the years are the same, the month in the first date is
before the month in the second date.
– Finally, if both the year and the month values are the same, the
date in the first date is before the day in the second date.
15
Delegation
• Q: Which class (Entry or Date) should we put
ealierThan() method in ?
• A: The ealierThan() method deals with properties of
the Date so that we delegate this computational task to
the corresponding methods in Date class
Entry
Date
- Date date
- int day
- double distance
- int month
- int duration
- int year
- String comment
??? ealierThan(???)
16
Design earlierThan() method
• Purpose and contract (method signature)
// is this date early than the other date
public boolean earlierThan(Date that)
• Examples
– new Date(30, 6, 2003).earlierThan(new
Date(1, 1, 2004)) should produce true
– new Date(1, 1, 2004).earlierThan(new
Date(1, 12, 2003)) should produce false
– new Date(15, 12, 2004).earlierThan(new
Date(31, 12, 2004)) should produce true
17
Design earlyThan() method
Template // is this date early than the other date
public boolean earlyThan(Date that) {
...this.day...this.month...this.year...
...that.day...that.month...that.year...
}
18
Design earlyThan() method
Implement
public boolean earlierThan(Date that) {
if (this.year < that.year) return true;
else if (this.year > that.year) return false;
else if (this.month < that.month) return true;
else if (this.month > that.month) return false;
else if (this.day < that.day) return true;
else return false;
}
19
public boolean earlierThan(Date that) {
if (this.year < that.year) {
return true;
}
else {
if (this.year > that.year) {
return false;
}
else {
if (this.month < that.month) {
return true;
}
else {
if (this.month > that.month) {
return false;
}
else { if (this.day < that.day) return true; }
else return false;
}
}
}
} 20
Unit Testing
pubblic class EntryTest extends TestCase {
...
public void testEarlierThan() {
Date date1 = new Date(30, 6, 2003);
Date date2 = new Date(1, 1, 2004);
Date date3 = new Date(1, 12, 2003);
Date date4 = new Date(15, 12, 2004);
Date date5 = new Date(31, 12, 2004);
assertTrue(date1.earlierThan(date2));
assertFalse(date2.earlierThan(date3));
assertTrue(date3.earlierThan(date4));
assertTrue(date4.earlierThan(date5));
assertFalse(date1.earlierThan(date1));
assertFalse(date5.earlierThan(date4));
assertFalse(date4.earlierThan(date3));
assertTrue(date3.earlierThan(date2));
assertFalse(date2.earlierThan(date1));
}
}
21
Restaurant example
• Develop a program that helps a visitor navigate
Manhattan's restaurant scene.
et
The program must be able to provide four pieces of
e
Str
information for each restaurant: its name,
the kind of food it serves, its price range,
and the closest intersection avenue
(street and avenue).
• Examples:
– La Crepe, a French restaurant, on 7th Ave and 65th Street,
moderate;
– Bremen Haus, a German restaurant on 2nd Ave and 86th Street,
moderate;
– Moon Palace, a Chinese restaurant on 10th Ave and 113th Street,
inexpensive;
22
Class Diagram
Restaurant
- String name Intersection
- String food - int avenue
- String priceRange - int street
- Intersection intersection
23
Problem Statement
• Develop a method to help visitors to find out
whether two restaurants are close to each other
• Two restaurants are "close'' to each other if they are
at most one avenue and at most one street away
from each other
24
Avenue 1 2 3 4 5
1 (1, 1) (1, 2) E(1, 3) J(1, 4) (1, 5)
Street
27
closeTo template
in Intersection class
public class Intersection {
private int avenue;
private int street;
public Intersection(int avenue, int street) {
this.avenue = avenue;
this.street = street;
}
28
closeTo template
in Restaurant class
public class Restaurant {
private String name;
private String food;
private String priceRange;
private Intersection intersection;
...
2. Chuẩn bị bột
Trung gian
4. Nướng bánh
5. Đóng gói
30
1. Nguyên liệu
2. Chuẩn bị bột
Trung gian
4. Nướng bánh
5. Đóng gói
31
closeTo method implementation
public class Intersection {
...
public boolean closeTo(Intersection that) {
return (Math.abs(this.avenue - that.avenue) <= 1) &&
(Math.abs(this.street - that.street) <= 1);
}
}
Delegate
public class Restaurant {
...
public boolean closeTo(Restaurant that) {
return this.intersection.closeTo(that.intersection);
}
32
Unit Testing
pubblic class RestaurantTest extends TestCase {
public void testCloseTo() {
Intersection i1 = new Intersection(3, 3);
Intersection i2 = new Intersection(3, 2);
assertTrue(i1.closeTo(i2));
assertFalse(i1.closeTo(new Intersection(3, 5));
Restaurant r1 = new Restaurant("La Crepe", "French",
"moderate", new Intersection(3, 3));
Restaurant r2 = new Restaurant("Das Bier", "German",
"cheap", new Intersection(3, 2));
Restaurant r3 = new Restaurant("Sun", "Chinese",
"cheap", new Intersection(3, 5));
assertTrue(r1.closeTo(r2));
assertFalse(r1.closeTo(r3));
assertFalse(r2.closeTo(r3));
}
} 33
Rectangle example
• The rectangles have width, height and are located
on the Cartesian plane of a computer canvas, which
has its origin in the northwest corner.
Rectangle
CartPt
- CartPt location
- int x
- int width
- int y
- int height
34
Problem Statement
...Design a method that computes the distance of a
Rectangle to the origin of the canvas.
• Considering that a Rectangle has many points, the
meaning of this problem is clearly to determine the
shortest distance of the Rectangle to the origin.
• This, in turn, means computing the distance
between its northwest corner and the origin
x
O X
northwest corner
= location
y
Rectangle
Y 35
Problem Analysis
We need two methods:
1. Measuring the distance of a Rectangle to the
origin
2. Measuring the distance of a CartPt to the origin
36
Delegation
• Q: Which class (Rectangle or CartPt) should we
put distanceToO() method in ?
• A: Put distanceToO() in both classes.
– The distanceToO() method deals with properties of
the CartPt so that we delegate this computational
task to the corresponding methods in CartPt class
Rectangle
CartPt
- CartPt location
- int x
- int width
- int y
- int height
+ double distanceToO()
+ double distanceToO()
37
distanceToO examples
38
distanceToO purpose and signature
public class CartPt {
private int x;
private int y;
public CartPt(int x, int y) { ... }
Tips: Math.sqrt is the name of the method that computes the square
root of its argument as a double.
41
distanceToO method implementation
public class Rectangle {
private CartPt location;
private int width;
private int height;
43
Problem Extension Statement
• Compute the distance between the rectangle’s
center and the origin
center of retangle
x x + width/2
0 X
y
y+
height/2 height
Y
width
44
Solution 1: Don't delegate – Bad!
public class Rectangle {
private CartPt location;
private int width;
private int height;
public Rectangle(CartPt location, int width, int height) {
this.location = location;
this.width = width;
this.height = height; Q: Is it right?
}
A: Right, but the delegation
public double distanceToO() { is not applied.
return this.location.distanceToO();
}
Rectangle
CartPt
- CatesianPoint northWestConner - int x
- int height - int y
- int width
+ double distanceToO()
+ double distanceToO() + CartPt translate(int dx, int dy)
+ double distanceFromCenterToO()
- CartPt center()
49
Circle example
The circle are located on the Cartesian plane of a
computer canvas, which has its center and radius.
1. Compute the distance form circle to the origin
2. Computing the perimeter of a circle
3. Computing the area of a circle.
4. Computes the area of a ring, that is, this disk with a
hole in the center
50
Distance from circle to the origin
Circle CartPt
- CartPt center - int x
- int radius - int y
x
0 X
center of circle
y
Y
51
distanceToO template
public class Circle {
private CartPt center;
private int radius;
52
distanceToO body
public class Circle {
private CartPt center;
private int radius;
53
distanceToO test
54
Computing the perimeter of a circle
perimeter template
public class Circle {
private CartPt center;
private int radius;
56
perimeter Test
public class CircleTest extends TestCase {
...
public void testPerimeter() {
Circle c1 = new Circle(new CartPt(3, 4), 5);
Circle c2 = new Circle(new CartPt(5, 12), 10);
Circle c3 = new Circle(new CartPt(6, 8), 20);
assertEquals(c1.perimeter(), 31.416, 0.001);
assertEquals(c2.perimeter(), 62.832, 0.001);
assertEquals(c3.perimeter(), 125.664, 0.001);
}
}
57
Computing the area of a circle
area template
public class Circle {
private CartPt center;
private int radius;
59
area Test
public class CircleTest extends TestCase {
...
public void testArea() {
Circle c1 = new Circle(new CartPt(3, 4), 5);
Circle c2 = new Circle(new CartPt(5, 12), 10);
Circle c3 = new Circle(new CartPt(6, 8), 20);
60
Computes the area of a ring
Area of ring
x
0 X
61
area template
public class Circle {
private CartPt center;
private int radius;
public Circle(CartPt center, int radius) {
this.center = center;
this.radius = radius;
}
// Compute the area of the circle
public double area() {
return Math.PI * this.radius * this.radius;
}
// Compute the area of the ring
public double area(Circle that) {
...this.center...this.radius...
...this.distanceToO()...this.perimeter()...this.area()...
...that.center...that.radius...
...that.distanceToO()...that.perimeter()...that.area()...
}
} 62
area of ring body
public class Circle {
private CartPt center;
private int radius;
public Circle(CartPt center, int radius) {
this.center = center;
this.radius = radius;
}
// Compute the area of the circle
public double area() {
return Math.PI * this.radius * this.radius;
}
65
Class diagram
Circle CartPt
- CartPt center
- int x
- int radius
- int y
+ double distanceToO()
+ double perimeter() 1+ double distanceToO()
+ double area()
+ double area(Circle that)
66
Cylinder example
Cylinder • The information of the cylinder
- Circle baseDisk
- int height
includes base disk and its height.
+ double volume()
• Compute the volume of the
+ double surface() cylinder
• Compute the surface area of the
cylinder
Circle CartPt
- CartPt center - int x
- int radius - int y
+ double distanceToO() + double distanceToO()
+ double perimeter()
+ double area()
+ double area(Circle that)
67
volume method template
public class Cylinder {
private Circle baseDisk;
private int height;
69
volume method test
public void testVolume(){
Circle c1 = new Circle(new CartPt(3, 4), 5);
Circle c2 = new Circle(new CartPt(5, 12), 10);
Circle c3 = new Circle(new CartPt(6, 8), 20);
70
surface method template
public class Cylinder {
private Circle baseDisk;
private int height;
public Cylinder(Circle baseDisk, int height) {
this.baseDisk = baseDisk;
this.height = height;
}
71
surface method body
public class Cylinder {
private Circle baseDisk;
private int height;
public Cylinder(Circle baseDisk, int height) {
this.baseDisk = baseDisk;
this.height = height;
}
73
Class diagram
Cylinder
- Circle baseDisk
- int height
+ double volume()
+ double surface()
Circle CartPt
- CartPt center - int x
- int radius - int y
+ double distanceToO() + double distanceToO()
+ double perimeter() + int getX()
+ double area() + int getY()
+ double area(Circle that)
74