Tải bản đầy đủ (.pptx) (116 trang)

Methods and containment arrows (lập TRÌNH cơ bản SLIDE)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (284.78 KB, 116 trang )

Basic Programming Course

Methods and Containment
Arrows
Week 4,5,6

1


Date example
• Develop a program that helps you keep track
of daily. One date is described with three
pieces of information: a day, a month, and a
Class
year
Date
Data type

- int day
- int month
- int year

Property or field
Method

2


Define Class, Constructor and test Constructor
public class 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;
}
}
import junit.framework.*;
public class TestDate extends TestCase {
public void testConstrutor(){
new Date(5, 6, 2003); // denotes June 5, 2003
new Date(6, 6, 2003); // denotes June 6, 2003
new Date(23, 6, 2000); // denotes June 23, 2000
}

3


GPS Location example
• Develop a GPS navigation program for cars.
The physical GPS unit feeds the program with
the current location. Each such location
consists of two pieces of information: the
latitude and the longitude.
Class

GPSLocation
- double latitude
- double longitude

Data type

Property or field
Method
4


Define Class, Constructor and test Constructor
public class GPSLocation {
private double latitude /* degrees */;
private double longitude /* degrees */;
public GPSLocation(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
}
import junit.framework.*;
public class TestGPSLocation extends TestCase {
public void testConstrutor(){
new GPSLocation (33.5, 86.8);
new GPSLocation (40.2, 72.4);
new GPSLocation (49.0, 110.3);
}

5


Class
A class is a generic description of a “thing”. It has:
1. Attribute: describes the “thing”

Attributes = Properties = Information = Fields = Data =
Variables

2. Behavior: describes what the “thing” does
Behaviors = Methods = Functions = Operations

3. State: describes the state that “thing” belongs to
A “thing” in a computer system ‘come alive – they
have behavior

6


Car example
With a Car
• Attributes which we concern: manufacturer,
color, speed,…
• Behaviors can happen with a car: how to start
up, how to speed up,..
• States:
– when a car in good shape, it is in use state,
– When it is damaged, it becomes not workable
state,..
7


Book example
A Book in library has:
• Attributes that we concern : name, author, publisher, price,…
• Behaviors can modeled with this book: borrow, payback,…

• States:
– If it is ready to be borrow, it in ready state
– if it is borrowed, it becomes busy state
– when it is old, damaged and taken stock of, it is in out of
stock (thanh lý) state.
–…

8


Room example
A Room in hotel has:
• Attributes we concern: name, kind (may be: usual,
deluxe, VIP,…), price per day,…
• Behavior can happen with a room: rent,…
• States:
– If it is ready to be rent, it is in ready state
– if it is rent, it becomes busy state
– when it is damaged, it is in-correct (sửa chữa)
state
–…
9


Point example
A point in monitor has:
• Attributes: x coordinate, which tells us where the
pixel is in the horizontal direction, and y coordinate,
which tells us where the pixel is located in the
downwards vertical direction and color

• Behaivors:
– Computes how far some pixel is from the origin
– Computes the distance between 2 pixels,…

• State: ?
So, some real-life objects do not have state.

10


Student example
A student has:
• Attributes we concern: id, first name, last name, his
head teacher, math score, physic score, chemistry
score,…
• Behavior:
– Compute his average score.
– Check whether the student is supervised by some
teacher,
– Transfer this student for another head teacher.
• State: ?
11


Rectangle example
With a Rectangle:
• Attributes that we concern: height, width,
north west point,…
• Behaviors:
– compute perimeter

– compute area
– Determine the distance form this rectangle to the
origin,…

• State:?
12


Object
• An object is an instance of a class
• Example:
– The Land Cruiser, red color and speed is 300 km/h is an instance of Car
class.
– The For Escape, white color and speed is 250 km/h is an instance of
Car class.
– The book Harry Porter, author is Rowling J.K, is published by Amazon,
price 20$ is an instance of Book class.
– The book Nhat ky Dang Thuy Tram, author is Dang Thuy Tram,
published by Tuoi Tre, price 3$ is an instance of Book class.
– The deluxe room- No. 201, price 50 $ per day is an instance of Room
class.
– A red point (2,3) is an instance of Point class.
– The student Nguyen Van A, his head teacher is Miss Thuy, has 8 in
Math, 9 in Physic and 7 in Chemistry is is an instance of Student class.
– A rectangle has height 5 cm, width 3 cm, north west point (2,3) is an
instance of Rectangle class.
13


The public or private modifiers

for attribute and method
• None modifier: Classes in the same package
can access this attribute / method.
• public: Classes in all packages can access this
attribute / method.
• private: Only the class itself can access this
attribute / method.
Encapsulation
14


Group discussion
•Find more class examples in real life?
•Find more class examples in real life which
do not have behavior, then add their
behavior?
•Distinguish class and instance of class
(object).

15


Relax &

…Do Exercises …

GOOD JOB!
16



Entry example
• Sometimes information not only consist of several pieces of
information, but one of its constituents consists of several
pieces, too. Take a look at this problem:
• 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.
Entry
- Date date
- double distance
- int durationInMinutes
- String postRunFeeling

Date
- int day
- int month
1 - int year
17


Define class and constructor
(define-struct Date (day
month year))
(define-struct Entry (date
duration comment))

public class 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;
}
}
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;
}
18
}


Test constructor
(make-date 5 6 2003)
(define date1 (make-date 6 6
2003))
(define date2 (make-date 23 6
2003))

(make-Entry (make-date 5 6
2003) 5.3 27 “good”)
(make-Entry date1 2.8 24
“tired”)
(make-Entry date2 26.2 159
“exhausted”)

package entry;
public class TestDate extends TestCase {
public void testContructor(){
new Date(5, 6, 2004);
Date date1 = new Date(6, 6, 2004);
Date date2 = new Date(23, 6, 2004);
}
}
package entry;
import junit.framework.*;
public class TestEntry extends TestCase {
public void testContructor(){
new Entry(new Date(5, 6, 2004), 5.3, 27,
"good");
Date date1 = new Date(6, 6, 2004);
new Entry(date1, 2.8, 24, "tired");
Date date2 = new Date(23, 6, 2004);
new Entry(date2, 26.2, 159, "exhausted");
}

19



Constructor
• Default constructor
• Overloading constructor
• Intitiate value for properties.

20


Exercise 3.1.4
Here is a revision of the problem of managing a runner's log (see
the figure):
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 in minutes per mile. ...Develop a method that computes the
pace for a daily entry.
Entry
- Date date
- double distance
- int durationInMinutes
- String postRunFeeling

Date
- int day
- int month
1 - int year

??? pace(???)
21



Solution 3.1.4: pace()
• Method implementation
public double pace() {
return this.durationInMinutes / this.distance;
}

• Unit testing
public void testPace() {
Entry entry = new Entry(new Date(5, 6, 2004), 5.3, 27, "good");
Assert.assertEquals(entry.pace(), 5.094, 0.001);
entry = new Entry(new Date(6, 6, 2004), 2.8, 24, "tired");
Assert.assertEquals(entry.pace(), 8.571, 0.001);

}

entry = new Entry(new Date(23, 6, 2004), 26.2, 159, "exhausted");
Assert.assertEquals(entry.pace(), 6.069, 0.001);

22


Exercise 3.1.5
• A runner's log refers to Dates (see the figure) 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.
Entry
- Date date
- double distance
- int durationInMinutes
- String postRunFeeling

Date
- int day
- int month
1 - int year
??? ealierThan(???)

23


Method Implementation
public class Date {
private int day;
private int month;
private int year;
public Date(int day, int month, int year) { … }
public boolean earlierThan(Date that) {
if (this.year < that.year) return true;
if (this.year > that.year) return false;
if (this.month < that.month) return true;
if (this.month > that.month) return false;

}


}

if (this.day < that.day) return true;
return false;

24


Unit Testing
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);
Assert.assertTrue(date1.earlierThan(date2));
Assert.assertTrue(date2.earlierThan(date3));
Assert.assertTrue(date3.earlierThan(date4));
Assert.assertTrue(date4.earlierThan(date5));

}

Assert.assertFalse(date1.earlierThan(date1));
Assert.assertFalse(date5.earlierThan(date4));
Assert.assertFalse(date4.earlierThan(date3));
Assert.assertFalse(date3.earlierThan(date2));
Assert.assertFalse(date2.earlierThan(date1));
25



×