You are on page 1of 2

Lp trnh hng i tng

Lp k53cb, k53cc
Thi gian : 60
Cu 1 (2 im)
a) Trong ngn ng lp trnh java, a tha k c ci t bng cu trc no ?
b) Chuyn g xy ra nu c nhiu hn mt khi catch khp vi loi i tng c nm.
c) Lit k 2 nhim v quan trng nht ca cc phng thc set
d) V mt hot ng, lp tru tng (abstract class) khc g vi lp c th (concrete class)
Cu 2 (2 im)
Cho ci t ca lp AppleBroker nh sau :
class AppleBroker extends Broker { String policy() { return (buy apple); }; }
a) Hy vit ci t ca lp Broker trong c mt phng thc advice sao cho on code sau
cho kt qu l dng You should buy apple c in ra mn hnh.
Broker broker = new AppleBroker(); broker.advice();
b) Hy vit ci t ca lp DellBroker l lp con ca Broker sao cho on code sau cho kt qu
l dng You should buy Dell c in ra mn hnh.
Broker broker = new DellBroker(); broker.advice;
Cu 3. (2 im)
on chng trnh di y c li hay khng ? nu c th dng no c li, gii thch ? Nu khng,
cho bit output ca chng trnh, gii thch.
a)
Integer a = new Integer(2);
Integer b = new Integer(2);
if (a = = b) System.out.println(true) else System.out.println(false);
b)
class A {
int b = 1;
public static void main(String[] args) {
System.out.println(b is + b);
}
}
Cu 4. (4 im)
Trong bi ton qun l thng tin v mt s phng tin giao thng nh xe ti, xe bus, xe con v xe
my. Mt gii php n gin l xy dng cho mi loi xe mt class nh sau :
class Car {
private String reg_num; // Registration number
private String brand; // Brand
private int max_pass; // Max no of passengers
private Color color; // Color
private Person owner; // Owner
}
class Truck {
private String reg_num; // Registration number
private String brand; // Brand
private int max_load; // Max load in kg

private Color color; // Color


private Person owner; // Owner
}
class Bus {
private String reg_num; // Registration number
private String brand; // Brand
private int max_pass; // Max no of passengers
private Color color; // Color
private Person owner; // Owner
}
class Motorbike {
private String reg_num; // Registration number
private String brand; // Brand
private Color color; // Color
private Person owner; // Owner
}
Tuy nhin, gii php ny khng cho php chng trnh ng dng thao tc vi cc loi xe khc nhau
mt cch ng b, trong khi vn c th phn bit gia cc loi xe nu cn.
Hy dng cc k thut tru tng ha v k tha sa gii php thit k trn sao cho c th i x
vi tt c cc loi phng tin giao thng (vehicle) mt cch ng b. Ngha l tt c nm trong
mt cy phn cp ca cc lp i tng, trong mi loi xe vn c i din bi mt class ring
do chng c cc thuc tnh khc nhau. Cy phn cp cn nhm xe bus v xe con vo mt nhm xe
ch khch (pasenger vehicle).
Lu : ch cn vit cc lp cng constructor v cc thuc tnh. Khng cn ci bt c phng thc
no khc.