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

Methods and containment in unions (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 (359.68 KB, 176 trang )

A2 Programming Course

Methods and Containment in Unions
Week 10,11,12,13

1


List

2


Managing Inventory



A sales clerk in a toy store needs to know not only the name of the toy,
but also its price, warehouse availability.



The representation of an inventory as a list of toys.

3


Data definition
In Scheme:




In Scheme, we would use a list to represent a complete inventory

In Java:
Java doesn't provide built-in empty and cons . According to the
Scheme-based data definition, the class of Inventory is a union:

Inventory, which is the abstract class of all kind of
inventories;
;; Inventory is one of:
;; --- a empty

MTInventory, which represents an empty inventory; and

;; --- a (cons Toy Inventory)
Cons, which represents the construction of a new



(define-struct Toy (name price available ))

inventory from a Toy and an existing Inventory.

;; A toy is: (make-Toy String Number Number)

4


Class diagram



Following Scheme, we also know that an MTInventory isn't really a structure, so that we don't need any fields for it.



A Cons though consists of two things, and therefore the Cons class requires two field definitions: one for the first Toy and one for the rest of
the Inventory.

<<abstract>>
Inventory

self-referential

Toy
MTInventory

Cons
-Toy first
-Inventory rest

-String name
-double price
-int avilable
5


Define classes an constructors (cont)
// class Inventory
public abstract class Inventory {
public Inventory() {

}
}
//class MTInventory
public class MTInventory extends Inventory{
public MTInventory() {
}
}
//class Cons
public class Cons extends Inventory{
private Toy first;
private Inventory rest;
public Cons(Toy first, Inventory rest ) {
this.first = first;
this.rest= rest;
}
}

6


Define classes and constructors
//class Toy
public class Toy {
private String name;
private double price;
private int available;

public Toy(String name, double price, int available) {
this.name = name;
this.price = price;

this.available = available;
}
}

7


Test Constructor
public class TestInventory extends TestCase {
public void testConstructor(){
Toy doll = new Toy("doll", 17.95, 5);
Toy robot = new Toy("robot", 22.05, 3);
Toy gun = new Toy ("gun", 15.0,4);

Inventory i1= new MTInventory();
Inventory i2= new Cons(doll, i1);
Inventory i3= new Cons(robot, i2);
Inventory i4= new Cons(gun, i3);
System.out.println(i4);
}
}

8


toString() method






Q: How can we print the contain of an object.
A:overriding toString() method of class Object.
Q: Do we need to add toString() in Inventory class? Why?
A: No !

public String toString(){
return "name: " + this.first.getName() + ", price: " +

this.first.getPrice()+ “,available: “ +

this.first.getAvailable()+ “ \n” + this.rest;
} // class Cons

public String toString(){
return "";

Deligation?

} // class MTInventory

9


Getter in classes Toy
public class Toy {
private String name;
private int wage;



public String getName() {
return this.name;
}
public double getPrice() {
return this.price;
}
public int getAvailable() {
return this.available;
}

}

10


Employee problem (again)



The accountant wants to compute the wage of not only an employee
but also all employees in the company.



Similarly, representing all employees is a list of employees.

11


Data definition

In Java:

In Scheme:



In Scheme, we would use a list to represent all employees in the company

Java doesn't provide built-in empty and cons . According to the
Scheme-based data definition, the class of ALo Employee is a union:

ALoEmployee, which is the abstract class of all kind of
Employee list;

;; ALoEmployee is one of:
;; --- a empty
;; --- a (cons Employee

MTLoEmployee, which represents an empty list; and
ALoEmployee)
ConsEmployee, which represents the construction of a new



(define-struct Employee (name hours))

list from an Employee and an existing list of Employee.

;; An employee is: (make-Employee String Number )


12


Class diagram


Following Scheme, we also know that an MTLoEmployee isn't really a structure, so that we don't need any fields for it.



A ConsEmployee though consists of two things, and therefore the ConsEmployee class requires two field definitions: one for the
first Employee and one for the rest of the ALoEmployee.

<<abstract>>
ALoEmloyee

self-referential

MTLoEmployee

ConsEmployee
-Employee first
-ALoEmployee rest

Employee

-String name
-int hours
13



Define classes an constructors (cont)
// class ALoEmployee
public abstract class ALoEmployee {
public ALoEmployee() {
}
}
//class MTLoEmployee
public class MTLoEmployee extends ALoEmployee {
public MTLoEmployee() {
}
}
//class ConsEmployee
public class ConsEmployee extends ALoEmployee{
private Employee first;
private ALoEmployee rest;
public ConsEmployee(Employee first, ALoEmployee rest) {
this.first = first;
this.rest = rest;
}

14


Define classes and constructors
//class Employee

public class Employee {
private String name;
private int hours;


public Employee(String name, int hours) {
this.name = name;
this.hours = hours;
}

15


toString() method





Q: How can we print the contain of an object.
A:overriding toString() method of class Object.
Q: Do we need to add toString() in ALoEmployee class? Why?
A: No !

public String toString(){
return this.first+ “\n " + this.rest;
} // class ConsEmployee

public String toString(){
return "";
} // class MTLoEmployee

16



toString () in Employee

public class Employee {
private String name;
private int hours;


public String toString() {
return “name: “ + this.name + “, hours: “ + this.hours;
}

}
17


Test Constructor
public class TestALoEmployee extends TestCase {
public void testConstructor(){

Employee nam = new Employee("Nam",40);
Employee mai = new Employee("Mai",30);
Employee minh = new Employee("Minh",102);

ALoEmployee l1 = new MTLoEmployee();
ALoEmployee l2 = new ConsEmployee(nam,l1);
ALoEmployee l3 = new ConsEmployee(mai,l2);
ALoEmployee l4 = new ConsEmployee(minh,l3);
System.out.println(l4);


}
}

18


List with containment arrow

19


Managing a Runner’s Logs Example



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 disposition.



Naturally the program shouldn't just deal with a single log entry but sequences of log
entries.

20


Data definition
In Scheme:




In Scheme, we would use a list to represent a
complete runner's log

In Java:
Java doesn't provide built-in empty and cons . According to
the Scheme-based data definition, the class of Logs is a
union:

ALog, which is the abstract class of all logs;
;; A Log is one of:
;; --- a empty

MTLog, which represents an empty log; and

;; --- a (cons Entry Log)
ConsLog, which represents the construction of a



(define-struct Entry (Date distance duration

new log from an entry and an existing log.

comment))
;; An entry is: (make-Entry Date Number Number
String)




(define-struct Date (day month year))
;; A date is: (make-Date Number Number Number)
21


Class diagram


Following Scheme, we also know that an MTLog isn't really a structure, so that we don't need any fields for it.



A ConsLog though consists of two things, and therefore the ConsLog class requires two field definitions: one for the first Entry
and one for the rest of the Log.

<<abstract>>
ALog

self-referential

MTLog

ConsLog
-Entry first
- ALog rest

Entry
- Date date
- double distance

- int durationInMinutes
1
- String postRunFeeling

Date
- int day
- int month
1 - int year

22


Define classes an constructors (cont)
// class ALog
public abstract class ALog {
}

//class MTLog
public class MTLog extends ALog {
public MTLog() {
}
}

//class ConsLog
public class ConsLog extends ALog {
private Entry first;
private ALog rest;

public ConsLog(Entry first, ALog rest) {
this.first = first;

this.rest = rest;
}
}

23


Define classes and constructors
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;
}
} //class Entry

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;


24


toString() method






Q: How can we print the contain of an object.
A: overriding toString() method of class Object.
Q: Do we need to add toString() in ALog class? Why?
A: No !

public String toString(){
return this.first+ " \n" +this.rest;
} // class ConsLog

public String toString(){
return "";
} // class MTLog

25


×