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

Bài Tập Mẫu 01: (3 điểm)

Yêu cầu:
Lớp DATE
package BaiMau01;

public class DATE {


int d;
int m;
int y;

//Getter and Setter


public int getD() {return d;}
public void setD(int d) {this.d = d;}

public int getM() {return m;}


public void setM(int m) {this.m = m;}

public int getY() {return y;}


public void setY(int y) {this.y = y;}
//Contructor
public DATE() {
this.d = 0;
this.m = 0;
this.y = 0;
}
public DATE(int d, int m, int y) {
this.d = d;
this.m = m;
this.y = y;
}
@Override
public String toString() {
return d+"/"+m+"/"+y;
}
}
Lớp NhanSu
package BaiMau02;

import java.util.Scanner;

public class NHANSU {


String MaNhanSu;
String Hoten;
DATE NS;
//Getter and Setter
public String getMaNhanSu() {
return MaNhanSu;
}
public void setMaNhanSu(String maNhanSu) {
MaNhanSu = maNhanSu;
}

public String getHoten() {


return Hoten;
}
public void setHoten(String hoten) {
Hoten = hoten;
}

public DATE getNS() {


return NS;
}
public void setNS(DATE NS) {
this.NS = NS;
}
//Constructor
public NHANSU() {
MaNhanSu = "";
Hoten = "";
this.NS = new DATE();
}
public NHANSU(String maNhanSu, String hoten, DATE NS) {
this.MaNhanSu = maNhanSu;
this.Hoten = hoten;
this.NS = NS;
}
//các phương thức
public void nhap() {
Scanner sc = new Scanner(System.in);
System.out.print("mã nhân sự:");
MaNhanSu = sc.nextLine();
System.out.print("Họ tên:");
Hoten = sc.nextLine();
System.out.println("Ngày Sinh:");
System.out.print("Ngày:");
int d = sc.nextInt();
System.out.print("Tháng:");
int m = sc.nextInt();
System.out.print("Năm:");
int y = sc.nextInt();
this.NS = new DATE(d, m, y);
}

public void xuat(){


System.out.println("Mã Nhân sự : " + MaNhanSu);
System.out.println("Họ Tên : " + Hoten);
System.out.println("Ngày Sinh : " + NS);
}
}
Lớp ThiHanh và Hà m Main
package BaiMau01;

import java.util.Scanner;

public class ThucHien {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

NHANSU dsns[];
System.out.println("Nhập bao nhiêu nhân sự:");
int n = sc.nextInt();
dsns = new NHANSU[n];
//Nhập nhân sự
for(int i=0; i< dsns.length; i++)
{
dsns[i] = new NHANSU();
dsns[i].nhap();
}
//xuất Nhân sự
for(NHANSU ns : dsns) {
System.out.println("**********************");
ns.xuat();
}
}
}
Bài Tập Mẫu 02: (7 điểm)

Abstract Class

Abstract Method

Yêu cầu:
Lớp Phương tiệ n
package baiMau02;

public abstract class PhuongTien {


protected String nhanhieu;
protected int namsx;
protected String tenhang;
//Getter và Setter
public String getNhanhieu() {return nhanhieu;}
public void setNhanhieu(String nhanhieu) {
this.nhanhieu = nhanhieu;
}
public int getNamsx() {return namsx;}
public void setNamsx(int namsx) {this.namsx = namsx;}

public String getTenhang() { return tenhang;}


public void setTenhang(String tenhang) {
this.tenhang = tenhang;
}
//Constructor
public PhuongTien() {
this.nhanhieu = "";
this.namsx = 0;
this.tenhang = "";
}
public PhuongTien(String nhanhieu, int namsx, String tenhang) {
this.nhanhieu = nhanhieu;
this.namsx = namsx;
this.tenhang = tenhang;
}
//Abstract Method
abstract void nhap();
abstract void xuat();
}
Lớp Xê Hơi
package baiMau02;

import java.util.Scanner;

public class XeHoi extends PhuongTien{


private int socho;
private String dungtich;
//Getter và Setter
public int getSocho() {return socho;}
public void setSocho(int socho) {this.socho = socho;}

public String getDungtich() { return dungtich;}


public void setDungtich(String dungtich) {
this.dungtich = dungtich;
}
//Constructor
public XeHoi() {
super();
this.socho = 0;
this.dungtich = "";
}
public XeHoi(String nhanhieu, int namsx, String tenhang, int
socho, String dungtich) {
super(nhanhieu, namsx, tenhang);
this.socho = socho;
this.dungtich = dungtich;
}
//Các phương thức
public void nhap(){
Scanner sc = new Scanner(System.in);
System.out.println("Nhãn Hiệu:");
String nh = sc.nextLine();
super.setNhanhieu(nh);
System.out.println("Năm sản xuất:");
int nm = sc.nextInt();
super.setNamsx(nm);
System.out.println("Tên Hãng:");
String th = sc.nextLine();
super.setTenhang(th);
System.out.print("Số chổ ngồi");
socho = sc.nextInt();
System.out.print("Dung Tích:");
dungtich = sc.nextLine();
}
public void xuat(){
System.out.println("Nhãn hiệu :" + super.nhanhieu);
System.out.println("Năm sản xuất :" + super.namsx);
System.out.println("tên hãng :" + super.tenhang);
System.out.println("số chổ ngồi: " + socho);
System.out.println("Dung Tích : " + dungtich);
}
}
Lớp Xe Má y
package baiMau03;

import java.util.Scanner;

public class XeMay extends PhuongTien{


private String phankhoi;
//getter setter
public String getPhankhoi() {
return phankhoi;
}
public void setPhankhoi(String phankhoi) {
this.phankhoi = phankhoi;
}
//Constructor
public XeMay() {
super();
this.phankhoi = "";
}
public XeMay(String nhanhieu, int namsx, String tenhang, String
phankhoi) {
super(nhanhieu, namsx, tenhang);
this.phankhoi = phankhoi;
}
// các phương thức
public void nhap(){
Scanner sc = new Scanner(System.in);
System.out.println("Nhãn Hiệu:");
String nh = sc.nextLine();
super.setNhanhieu(nh);
System.out.println("Năm sản xuất:");
int nm = sc.nextInt();
super.setNamsx(nm);
System.out.println("Tên Hãng:");
String th = sc.nextLine();
super.setTenhang(th);
System.out.print("Phân khối:");
phankhoi = sc.nextLine();
}
public void xuat(){
System.out.println("Nhãn hiệu :" + super.nhanhieu);
System.out.println("Năm sản xuất :" + super.namsx);
System.out.println("tên hãng :" + super.tenhang);
System.out.println("Phân Khối : " + phankhoi);
}
}
Lớp Thi Hành
package baiMau03;
import java.util.Scanner;
public class ThucHien {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PhuongTien dspt[];
System.out.println("Bao nhiêu phương tiện:");
int n = sc.nextInt();
dspt = new PhuongTien[n];
//Nhâp phương tiện
for(int i=0; i<dspt.length; i++) {
System.out.println("1. Xe hơi - 2. Xe máy");
int chon = sc.nextInt();
switch(chon) {
case 1: dspt[i] = new XeHoi(); break;
case 2: dspt[i] = new XeMay(); break;
default: break;
}
dspt[i].nhap();
}
//Xuất phương tiện
for(PhuongTien pt : dspt) {
pt.xuat();
}
}
}

You might also like