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

Mutable immutable (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 (260.08 KB, 59 trang )

A2 Programming Course

Section 02

Methods for Classes
week 3, 4

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 year

Class

Date
Data type

- int day
- int month
- int year

Property or field

Method

2




Define Class, Constructor and test Constructor
class Date {
int day;
int month;
int year;
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

Property or field

Data type
Method
4


Define Class, Constructor and test Constructor
class GPSLocation {
double latitude /* degrees */;
double longitude /* degrees */;
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


Star example



Suppose we wish to represent a star information which has first name, last
name, instrument he uses and his sales.



change the star's sales with a given sales.

16


Class Diagram
Class

Star

Data type


- String firstName
- String lastName
- String instrument
- int sales
+ ???incrementSales(???)

Property or field

Method

17


Define Class
Define structure

Define class

(define-struct Star (firstName lastName instrument public class Star {
sales))

private String firstName;
private String lastName;
private String instrument;
private int sales;

Constructor:
(make-Star

}

Constructor
public class Star {
private String firstName;
private String lastName;
private String instrument;
private int sales;
public Star(String firstName, String lastName,

String instrument, int sales) {

this.firstName = firstName;
this.lastName = lastName;
this.instrument = instrument;
this.sales = sales;
}
}

18


Test Star Constructor
Test
(make-Star “Abba” “John” “vocals”

Test
import junit.framework.*;

12200)
public class TestStar extends TestCase {
(define aStar1 (make-Star "Elton" "John"


public void testConstructor(){

"Guitar" 20000)

new Star ("Abba", "John", "vocals", 12200);
Star aStar1 = new Star ("Elton", "John", "guitar", 20000);

(define aStar2 (make-Star "Debie"

Star aStar2 = new Star ("Debie", "Gission", "organ", 15000);

"Gission" "organ" 15000)
}
}

19


incrementSales template
incrementSales function:
(define (incrementSales aStar)

incrementSales method
public class Star {
private String firstName;

…(Star-firstName aStar)…

private String lastName;


…(Star-lastName aStar)…

private String instrument;

…(Star-instrument aStar)…

private int sales;

…(Star-sales aStar)…)

public Star(String firstName, String lastName,

String instrument, int sales) {

this.firstName = firstName;
this.lastName = lastName;
this.instrument = instrument;
this.sales = sales;
}
public ??? incrementSales(???){
…this.firstName …
…this.lastName …
…this.instrument..
…this.sales…
}
}

20



incrementSales body
incrementSales function:

incrementSales method

(define (incrementSales aStar sales)

public class Star {
private String firstName;

(make-Star

private String lastName;

(Star-firstName aStar)

private String instrument;

(Star-lastName aStar)

private int sales;

(Star-instrument aStar)

public Star(String firstName, String lastName,

(sales)))

String instrument, int sales) {


this.firstName = firstName;
this.lastName = lastName;
this.instrument = instrument;
this.sales = sales;
}
public Star incrementSales(int sales){
return new Star(this.firstName,

this.lastName, this.instrument,

sales);

}
Immutable

}

21


Test incrementSales
Test incrementSales function
(define aStar1 (make-Star "Elton" "John" "Guitar" 20000))
(define aStar2 (make-Star "Debie" "Gission" "organ"

Test incrementSales method
import junit.framework.*;
public class TestStar extends TestCase {
public void testConstructor(){


15000))


}

(incrementSales (make-Star "Abba" "John" "vocals"

public void testIncrementSales(){

10000))

assertTrue(new Star ("Abba", "John", "vocals", 12200).incrementSales(20000)

(incrementSales aStar1)

.equals(new Star ("Abba", "John", "vocals",32200)));

(incrementSales aStar2)

Star aStar1 = new Star ("Elton", "John", "guitar", 20000);
Star aStar2 = new Star ("Debie", "Gission", "organ", 15000);
assertTrue(aStar1.incrementSales(30000).equals(new Star ("Elton", "John", "guitar",
50000)));
assertTrueaStar2.incrementSales(20000).equals(new Star ("Debie", "Gission", "organ",
15000)));
}
}

22



Discuss more: equals method




Q: Why we do not use Java built-in equals method?
Our equals method:

public class Star {
private String firstName;
private String lastName;
private String instrument;
private int sales;

public boolean equals(Object obj){
if (null == obj || ! (obj instanceof Star))
return false;
else {
Star that = ( (Star) obj);
return this.firstName.equals(that.firstName)
&& this.lastName.equals(that.lastName)
&& this.instrument.equals(that.instrument)
&& this.sales == that.sales;
}
}

23



mutableIncrementSales body
incrementSales method
public class Star {
private String firstName;
private String lastName;
private String instrument;
private int sales;
public Star(String firstName, String lastName,

String instrument, int sales) {

this.firstName = firstName;
this.lastName = lastName;
this.instrument = instrument;
this.sales = sales;
Setter

}
public void setSales(int sales){
this.sales = sales ;
}

Mutable
}

24


Discuss more: getSales method

Q: Do we use “selector” this.sales outside Star class
A: No
Solution: getSales method
public class Star {
private String firstName;
private String lastName;
private String instrument;
private int sales;
public Star(String firstName, String lastName, String instrument, int sales) {
this.firstName = firstName;
this.lastName = lastName;
this.instrument = instrument;
this.sales = sales;
}

public int getSales(){
return this.sales;
}
}

25


×