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

Methods and union of classes (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 (322.82 KB, 125 trang )

A2 Programming Course

Methods and Unions of
Classes
Week 7,8,9

1


The Drawing Program
Develop a drawing program that deals with at least three kinds of shapes: dots,
squares, and circles. The shapes are located on a Cartesian grid whose origin is
in the northwest.
A dot is located in the grid and is drawn as a small disk of a fixed size (3 pixels).
A square's location is specified via its north-west corner in the grid and its size.
A circle's essential properties are its center point and its radius.

2


Scheme data definitions
• In the terminology of Scheme data definitions, a shape is one of
three possible kinds of values. More specifically, if we were to
develop this program in Scheme, we would introduce five data
definitions: one for shapes, one for dots, one for squares, one for
circles, and one for Cartesian points
• ;; A Shape is one of:
;; --- a Dot
;; --- a Square
;; --- a Circle
• (define-struct Dot (loc))


;; A Dot is a structure: (make-Dot cartPt)
• (define-struct Square (loc size))
;; A Square is a structure: (make-Square cartPt Number)
• (define-struct Circle (loc radius))
;;A Circle is a structure: (make-Circle cartPt Number)
• (define-struct CartPt (x y))
;; A CartPt is a structure: (make-CartPt Number Number)

3


New approach
AShape
# CartesianPoint location
1

Dot

Square
- int size

CartesianPoint
- int x
- int y

Circle
- int radius

• Dot, Square, and Circle are SUBCLASSES or REFINED CLASSES
of AShape.

• Because all instances of AShape are instances of either Dot,
Square, or Circle, we say that AShape is an ABSTRACT CLASS
or SUPERCLASS

4


Class diagram





When people discuss classes, they often identify the commonalities (tương
đồng) and the differences among the subclasses. Also, they tend to
attribute the commonalities to the superclass, and the differences to the
subclasses.

A look at the diagram suggests that the three subclasses have one thing in common,
namely, a location. That is, each dot, square, or circle has a location.
Then again, circles differs from squares in that one has a radius and the other a size
attribute; the dot doesn't have any other attributes.

AShape
# CartesianPoint location
1

Dot

Square

- int size

CartesianPoint
- int x
- int y

Circle
- int radius
5


Java data definitions

//class CartesianPoint
public class CartesianPoint {
private int x;
private int y;
public CartesianPoint(int x, int y){
this.x=x;
this.y=y;
}
}
// class Shape
public abstract class Shape{
protected CartesianPoint location;
}
//class Dot
public class Dot extends AShape {
public Dot(CartesianPoint location) {
this.location = location;

}
}

//Class Square
public class Square extends AShape {
private int side;
public Square(CartesianPoint location, int
side) {
this.location = location;;
this.side = side;
}
//class Circle
public class Circle extends AShape {
private int radius;
public Circle(CartesianPoint location, int
radius) {
this.location = location;
this.radius = radius;
}
6


Keyword
• The keyword abstract in front of a class
indicates that there are no instances of this
class; it only exists to collect the instances of
other classes under a single header.
• The keyword extends makes the Dot class a
refinement or an extension of AShape
therefore INHERITS all of AShape's fields.


7


The protected or abstract modifiers for
attribute and method
• protected: the class itself and its subclass can
access this attribute / method.
• abstract: (just for method) a method signature.
An abstract method in an abstract class announces
that the concrete subclasses have CONCRETE
METHODS with the same signature and a proper
method body.
Q: Review public and private modifiers for
attribute and method.
8


Modifiers for class
• None modifier: Classes in the same package
can see this class.
• public: Classes in all packages can see this class.
• abstract: this is abstract class.

Encapsulation
9


Test constructors
public class TestCartesianPoint extends TestCase {

public void testConstructor(){
CartesianPoint caPt1= new CartesianPoint (4,3);
CartesianPoint caPt2= new CartesianPoint (5,12);
CartesianPoint caPt3= new CartesianPoint (6,8);
} //test for class CartesianPoint
public class TestDot extends TestCase {
public void testConstructor(){
CartesianPoint caPt1= new CartesianPoint (4,3);
CartesianPoint caPt2= new CartesianPoint (5,12);
CartesianPoint caPt3= new CartesianPoint (6,8);
Dot d1 = new Dot(caPt1);
Dot d2 = new Dot(caPt2);
Dot d3 = new Dot(caPt3);
} // test for class Dot
10


Test constructors

public class TestCircle extends TestCase {
public void testConstructor(){
CartesianPoint caPt1= new CartesianPoint (4,3);
CartesianPoint caPt2= new CartesianPoint (5,12);
CartesianPoint caPt3= new CartesianPoint (6,8);
Circle cr1= new Circle (caPt1, 5);
Circle cr2= new Circle (caPt2, 10);
Circle cr3= new Circle (caPt3, 12);
}
} //test for class Circle
public class TestSquare extends TestCase {

public void testConstructor(){
CartesianPoint caPt1= new CartesianPoint (4,3);
CartesianPoint caPt2= new CartesianPoint (5,12);
CartesianPoint caPt3= new CartesianPoint (6,8);
Square s1= new Square (caPt1,5);
Square s2= new Square (caPt3,10);
Square s3= new Square (caPt3,12);
}
} //test for class Square

11


Zoo example - Exercise 7.2.1(HTDP)
• Develop a program that helps a zoo keeper take care of the
animals in the zoo. For now the zoo has lions, snakes, and
monkeys. Every animal has a name and weight. The zoo
keeper also needs to know how much meat the lion eats per
day, the length of each snake, and the favorite food for each
monkey
• Examples:





The lion Leo weighs 300 pounds and eats 5 pounds of meat every day;
The snake Boa weighs 50 pounds and is 5 feet long;
The monkey George weighs 150 poundds and loves bananas.
The monkey Mina weighs 120 pounds and loves to eat kiwi


12


Scheme data definitions
• In Scheme, we should have data definitions for a collection of zoo
animals. More specifically, if we were to develop this program in
Scheme, we would introduce four data definitions: one for animal,
one for lion, one for snake, one for monkey.
• ;; An Animal is one of:
;; --- a Lion
;; --- a Snake
;; --- a Monkey
• (define-struct Lion (name weight meat))
;; A Lion is a structure: (make-Lion String Number Number)


(define-struct Snake (name weight length))
;; A Snakeis a structure: (make-Snake String Number Number)

• (define-struct Monkey (name weight food))
;;A Monkey is a structure: (make-Monkey String Number String)
13


Class diagram

14



Java definitions
public abstract class Animal {
public class Snake extends Animal{
protected String name;
private int length;
protected int weight;
public Snake(String name, int weight,
protected Animal(String name, int weight) { int length) {
this.name = name;
super (name, weight);
this.weight = weight;
this.length = length;
}
}
} //class Animal
} // class Snake
public class Lion extends Animal{
private int meat;
public Lion(String name, int weight, int
meat) {
super (name, weight);
this.meat = meat;
}
} //class Lion

public class Monkey extends Animal{
private String food;
public Monkey(String name, int
weight, String food) {
super (name, weight);

this.food = food;
}
} //class Monkey
15


“this” and “super” keyword
• this: references the attribute and method in
itself.
• super: references the attribute and method
in superclass.

16


Test constructor

public class TestLion extends TestCase {
public void testConstructor(){
Lion leo = new Lion ("Leo", 300,5);
Lion samba = new Lion ("Samba", 200,3);
Lion Cleopon = new Lion ("Cleopon", 250,5);
}
} // test for class Lion
public class TestSnake extends TestCase {
public void testConstructor(){
Snake boa = new Snake ("Boa", 50,5);
Snake mic = new Snake ("Mic", 45,4);
Snake bu = new Snake ("Bu", 55,6);
}

} // test for class Snake
public class TestMonkey extends TestCase {
public void testConstructor(){
Monkey george = new Monkey ("George", 150, "banana");
Monkey mina = new Monkey ("Mina", 120, "Kiwi");
Monkey slan = new Monkey ("Slan", 100, "Kiwi");
}
} // test for class Monkey

17


Exercises
Part I: Exercise 4.0.4 to 4.1.4 (HTDCH)
Do in class 4.1.1

18


Extended exercises
• Exercise 7.2.2 (HTDP).   The administrators of
metropolitan transportation agencies manage
fleets of vehicles. Develop data definitions for
a collection of such vehicles. The collection
should include at least buses, limos, cars, and
subways. Add at least two attributes per class
of vehicle.

19



Prepare for next week
How to design class hierarchy
• III  Fun Methods    
  5  Methods and Unions of Classes

20


Relax &

…Do Exercises …

GOOD JOB!

21


Recall the Drawing Program

AShape
# CartesianPoint location
1

Dot

Square
- int size

CartesianPoint

- int x
- int y

Circle
- int radius
22


Requirements
1.
2.
3.
4.




Compute the area of a shape
Compute the distance of a shape to the origin
Determine whether some point is inside the shape
Compute the bounding box of a shape
All of these methods clearly work with shapes in
general but may have to compute different results
depending on the concrete shape on which they
are invoked
In an object-oriented language, we can indicate
that all AShape's have such method with ABSTRACT
methods
23



Abstract Methods
• An abstract method isn't a method; it is just a
method signature preceded with the keyword
abstract
• An abstract method in an abstract class
announces that the concrete subclasses have
CONCRETE METHODS with the same signature
and a proper method body

24


1. Computing Area of A Shape

25


×