Professional Documents
Culture Documents
Lab5 BlueJ
Lab5 BlueJ
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.
/** * Write a description of class Shape here. * * @author (your name) * @version (a version number or a date) */ public abstract class Shape { /**
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); } }
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(); }
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?
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
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:
M t cho lp SwitchButton:
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(); } }
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.
FPT-Aptech