Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Learn Java By Examples

FPT-Aptech

LAB-5
Learn Java By Example No. Task 1 Khai bo v ci t lp tru tng 2 Khai bo v ci t interface 3 Bi tp t lm 4 T c Class Object Time 30 30 1h 1day Note ver

Prerequiste
No. Tools

1 2 3 4

My tnh ci t JDK 1.6.0 Phn mm BlueJ 2.5.3 v cc Extension c bn B a CD Learn Java By Examples Bi tp lp trnh hng i tng vi Java.

1. Khai bo v ci t lp tru tng


Trn thc t, ty vo yu cu nghip v trong nhng bi ton c th ta mi xc nh c khi no th s dng cc lp tru tng trong lp trnh. Thng thng, u hiu nhn bit cn phi s dng lp tru tng l trong bi ton tn ti mt nhm cc lp c nhng thuc tnh v hnh vi tng t nhau, cc thao tc nghip v ca bi ton thng xuyn lm vic vi mt danh sch cc i tng hoc thao tc ln nhng i tng theo mt chu trnh xc nh. Do vy ng di gc ngi phn tch, lp tru tng ng vai tr nh mt cng c dng phn loi cc lp khc nhau vo trong mt nhm phc v cho qu trnh pht trin ng dng d dng v linh ng hn. Vit mt chng trnh s dng i tng ha (Graphic) v cc i tng hnh hc c bn (Shape)

/** * Write a description of class Shape here. * * @author (your name) * @version (a version number or a date) */ public abstract class Shape { /**

Learn Java By Examples


* Constructor for objects of class Shape */ public Shape() { // To do: } public abstract void paint(Graphic g); }

FPT-Aptech

/** * Write a description of class Graphic here. * * @author (your name) * @version (a version number or a date) */ public class Graphic { /** * Constructor for objects of class Graphic */ public Graphic() { // To do: } public void drawPoint() { System.out.print("."); } public void drawLine() { System.out.print("_"); } public void drawRectangle() { System.out.print("[]"); } }

/** * Write a description of class Client here. * * @author (your name) * @version (a version number or a date) */ public class Client { /** * Constructor for objects of class Client */ public Client() { // To do: } public void drawShape(Shape shape, Graphic g) { shape.paint(g); } }

Learn Java By Examples

FPT-Aptech

/** * Write a description of class Point here. * * @author (your name) * @version (a version number or a date) */ public class Point extends Shape { /** * Constructor for objects of class Point */ public Point() { // To do: } public void paint(Graphic g) { g.drawPoint(); } }

/** * Write a description of class Line here. * * @author (your name) * @version (a version number or a date) */ public class Line extends Shape { /** * Constructor for objects of class Line */ public Line() { // To do: } public void paint(Graphic g) { g.drawLine(); }

Learn Java By Examples


}

FPT-Aptech

/** * Write a description of class Rectangle here. * * @author (your name) * @version (a version number or a date) */ public class Rectangle extends Shape { /** * Constructor for objects of class Rectangle */ public Rectangle() { // initialise instance variables } public void paint(Graphic g) { g.drawLine(); } }

Kch bn 1: S dng cc lp va khai bo vit chng trnh java theo kch bn sau: - Khai bo mt mng cc i tng hnh hc Shape gm 18 phn t. - Lu vo mng 6 hnh ch nht, 4 ng thng v 10 im. - V ln mn hnh console cc hnh va to. Cu hi: Qx1 Hy pht trin tip thm mt s lp mi t lp Shape? Qx2 C th nh ngha mt i tng hnh hc mi da trn c im i tng c to thnh t nhng i tng Shape c bn?

2. Khai bo v s dng interface


Trong v d trc v SwitchButton v ElectricLamp ta c s quan h lp nh sau:

Nhc im ca ng dng ny l s lin kt gia SwitchButton v ElectricLamp kh cht. Nh vy mi switchbutton c to ra ch c th iu khin bt v tt c mt loi i tng l ElectricLamp. Tuy nhin trn thc t, c rt nhiu loi i tng cng c

Learn Java By Examples

FPT-Aptech

kh nng bt tt c v d nh qut in (ElectricFan), hay i (Radio), khi kh nng ti s dng ca SwitchButton cho cc thit b mi ny l khng th. Ngn ng Java a ra mt gii php cho vn ny l khi nim v Interface. Interface gip cho ngi phn tch thit k c th phn loi mt nhm cc i tng thuc cc lp khc nhau da trn cc hnh vi tng t nhau. Nh trong v d trn ta c ElectricLamp, ElectricFan, Radio v.v l cc nhm i tng u c kh nng bt v tt (Switchable), do vy trong thit k ta c th to ra mt Interface ly tn l Switchable vi cc tnh nng c m t trc (bt v tt). Khi i tng SwitchButton ch cn kt ni ti mt Switchable l c th iu khin c i tng ny, v do vy SwitchButton lc ny c th c s dng iu khin rt nhiu cc loi thit b Switchable khc nhau nh ElectricLamp, Radio, ElectricFan v.v Sau y l s quan h mi c s dng Inteface:

Thit k chi tit cc lp:

Phn m t interface Switchable:


/** * Write a description of interface Switchable here. * * @author (your name) * @version (a version number or a date) */ public interface Switchable { void switchOn(); void switchOff(); }

M t cho lp SwitchButton:

Learn Java By Examples

FPT-Aptech

/** * Write a description of class SwitchButton here. * * @author (your name) * @version (a version number or a date) */ public class SwitchButton { /** * Fields */ private Switchable switchObj; /** * Constructor for objects of class SwitchButton */ public SwitchButton() { // To do: } public void connectTo(Switchable switchObj) { this.switchObj=switchObj; } public void switchOn() { switchObj.switchOn(); } public void switchOff() { switchObj.switchOff(); } }

Phn ci t cho cc ElectricLamp, Radio, ElectricFan mt interface Switchable:


/** * Write a description of class ElectricLamp here. * * @author (your name) * @version (a version number or a date) */ public class ElectricLamp implements Switchable { /** * Fields */ private String status; /** * Constructor for objects of class ElectricLamp */ public ElectricLamp() { // To do: status="Dark!"; } public void switchOn() { status="Light!"; System.out.println(status); } public void switchOff() {

Learn Java By Examples


status="Dark!"; System.out.println(status); } }

FPT-Aptech

/** * Write a description of class Radio here. * * @author (your name) * @version (a version number or a date) */ public class Radio implements Switchable { /** * Fields */ private String status; /** * Constructor for objects of class Radio */ public Radio() { // To do: status="............"; } public void switchOn() { status="^&%&^%^$^%@!)(^#!"; System.out.println(status); } public void switchOff() { status="............"; System.out.println(status); } }

S dng cc lp va nh ngha vit chng trnh java theo kch bn sau: - Khai bo mt mng cc i tng Switchable gm 5 phn t. - Khai bo v khi to mt mng cc Switchbutton gm 5 phn t. - Lu vo mng cc Switchable 3 bng n v 2 chic i. - Vit on chng trnh m phng qu trnh bt v tt cc bng n v i. Cu hi: Qx1 So snh vi v d SwitchButton-ElectricLamp khng dng interface? Qx2 So snh gia interface v abstract?

3. Bi tp t lm
Tm hiu cc bi tp trong ti liu Bi tp lp trnh hng i tng vi Java.

Learn Java By Examples

FPT-Aptech

4. Hc cch t hc, t thc hnh vi b CD Learn Java By Examples

You might also like