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; contain
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
Test constructor
import junit.framework.*;
public class EntryTest extends TestCase {
public void testDateContructor() {
new Date(5, 6, 2004);
Date date1 = new Date(6, 6, 2004);
Date date2 = new Date(23, 6, 2004);
}
6
Add methods to the Entry
Entry
Date
- Date date
- int day
- double distance
- int month
- int duration
- int year
- String comment
??? kkk(???)
??? nnn(???)
7
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(???)
10
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
11
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
public double pace() {
return this.duration / this.distance;
}
12
Design pace() method (con't)
• Unit testing
13
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.
14
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(???)
15
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
16
Design earlyThan() method (con't)
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...
}
17
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, 2004);
Date date4 = new Date(15, 12, 2004);
Date date5 = new Date(31, 12, 2004);
assertTrue(date1.earlierThan(date2));
assertTrue(date2.earlierThan(date3));
assertTrue(date3.earlierThan(date4));
assertTrue(date4.earlierThan(date5));
assertFalse(date1.earlierThan(date1));
assertFalse(date5.earlierThan(date4));
assertFalse(date4.earlierThan(date3));
assertFalse(date3.earlierThan(date2));
assertFalse(date2.earlierThan(date1));
}
}
18
Restaurant example
• Develop a program that helps a visitor navigate
Manhattan's restaurant scene.
The program must be able to provide four pieces of
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;
19
Class Diagram
Restaurant
- String name Intersection
- String food - int avenue
- String priceRange - int street
- Intersection intersection
20
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
21
Avenue 1 2 3 4 5
1 (1, 1) (1, 2) E(1, 3) (1, 4) (1, 5)
Street
24
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;
}
25
closeTo template in Restaurant class
public class Restaurant {
private String name;
private String food;
private String priceRange;
private Intersection intersection;
...
27
Unit Testing
public void testCloseTo() {
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));
}
28
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 nwCorner
- int x
- int width
- int y
- int height
29
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
y
Rectangle
Y 30
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
31
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 nwCorner
- int x
- int width
- int y
- int height
+ double distanceToO()
+ double distanceToO()
32
distanceToO examples
33
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.
36
distanceToO method implementation
public class Rectangle {
private CartPt nwCorner;
private int width;
private int height;
38
Problem Extension Statement
• Compute the distance between the rectangle’s
center and the origin
center of retangle
x
0 X
width
y
height
Y
39
Solution 1
public class Rectangle {
private CartPt nwCorner;
private int width;
private int height;
public Rectangle(CartPt nwCorner, int width, int height) {
this.nwCorner = nwCorner;
this.width = width;
this.height = height; Q: Is it right?
}
A: right, but the delegation
public double distanceToO() { is not applied.
return this.nwCorner.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()
43
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
44
Distance form circle to the origin
Circle CartPt
- CartPt center - int x
- int radius - int y
x
0 X
center of circle
y
Y
45
distanceToO template
public class Circle {
private CartPt center;
private int radius;
47
distanceToO test
48
perimeter template
public class Circle {
private CartPt center;
private int radius;
49
perimeter body
public class Circle {
private CartPt center;
private int radius;
50
perimeter Test
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.42, 0.001);
assertEquals(c2.perimeter(), 62.83, 0.001);
assertEquals(c3.perimeter(), 125.66, 0.001);
}
51
area template
public class Circle {
private CartPt center;
private int radius;
53
area Test
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);
54
Area of ring
Area of ring
x
0 X
55
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;
}
58
Overloading method
• Q: what happen with the same name area() and
area(Circle) method?
• A:
– Method area() and area(Cirlce ) in class Cirlce have
the same name but different parameter is called
overloading.
– When we invoke overloading methods, the method with
appropriate argument will do
59
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)
60
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)
61
volume method template
public class Cylinder {
private Circle baseDisk;
private int height;
63
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);
64
surface method template
public class Cylinder {
private Circle baseDisk;
private int height;
public Cylinder(Circle baseDisk, int height) {
this.baseDisk = baseDisk;
this.height = height;
}
65
surface method body
public class Cylinder {
private Circle baseDisk;
private int height;
public Cylinder(Circle baseDisk, int height) {
this.baseDisk = baseDisk;
this.height = height;
}
67
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)
68
Excercises
69
Exercise 3.1
Develop a "real estate assistant'' program. The "assistant'' helps
the real estate agent locate houses of interest for clients. The
information about a house includes its kind, the number of
rooms, the asking price, and its address. An address consists of
a house number, a street name, and a city.
• Represent the following examples using your classes:
– Ranch, 7 rooms, $375,000, 23 Maple Street, Brookline
– Colonial, 9 rooms, $450,000, 5 Joye Road, Newton
– Cape, 6 rooms, $235,000, 83 Winslow Road, Waltham
• Note:
– Ranch: A ranch is a large farm used for raising animals, especially
cattle, horses or sheep.
– Colonial: A colonial building or piece of furniture was built or made
in a style that was popular in American in the 17th and 18th
centuries.
– Cape: A cape is a large piece of land that sticks out into the sea
from the coast.
70
Exercise 3.1
Develop the following methods for the class House:
1. hasMoreRooms, which determines whether one
house has more rooms than some other house;
2. inThisCity, which checks whether the advertised
house is in some given city (assume we give the
method a city name);
3. sameCity, which determines whether one house is
in the same city as some other house.
71
Exercise 3.2
. . . Develop a program that assists bookstore employees.
For each book, the program should track the book’s title, its
price, its year of publication, and the author. A author has a
name and birth year.
• Develop the following methods for this class:
– currentBook that checks whether the book was published in
2004 or 2003;
– currentAuthor that determines whether a book was written by
a current author (born after 1940);
– thisAuthor that determines whether a book was written by the
specified author;
– sameAuthor that determines whether one book was written by
the same author as some other book;
– sameGeneration that determines whether two books were
written by two authors born less than 10 year apart.
72
WeatherRecord
-Date d
74
Class Diagram
Route
- String origin
- String destination
75