0% found this document useful (0 votes)
3 views28 pages

Java

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views28 pages

Java

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Bài 1:Quản lý nhân viên – Tính kế thừa

import [Link].*;

abstract class Employee {


protected String id;
protected String name;

public Employee(String id, String name) {


[Link] = id;
[Link] = name;
}

public abstract double calculateSalary();


public abstract String getType();

public void displayInfo() {


[Link]("%s - %s - %s - %.1f%n", id, name, getType(),
calculateSalary());
}
}

class FullTimeEmployee extends Employee {


private double salary;

public FullTimeEmployee(String id, String name, double salary) {


super(id, name);
[Link] = salary;
}

@Override
public double calculateSalary() {
return salary;
}

@Override
public String getType() {
return "FullTime";
}
}

class PartTimeEmployee extends Employee {


private double hourlyRate;
private int workingHours;

public PartTimeEmployee(String id, String name, double hourlyRate, int


workingHours) {
super(id, name);
[Link] = hourlyRate;
[Link] = workingHours;
}

@Override
public double calculateSalary() {
return hourlyRate * workingHours;
}

@Override
public String getType() {
return "PartTime";
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]([Link]());
List<Employee> employees = new ArrayList<>();

for (int i = 0; i < n; i++) {


String line = [Link]();
String[] parts = [Link](" ");
String type = parts[0];
String id = parts[1];

if ([Link]("F")) {
StringBuilder fullName = new StringBuilder();
for (int j = 2; j < [Link] - 1; j++) {
[Link](parts[j]).append(" ");
}
String name = [Link]().trim();
String lastName = [Link]([Link](" ") + 1);
double salary = [Link](parts[[Link] - 1]);
[Link](new FullTimeEmployee(id, lastName, salary));
} else if ([Link]("P")) {
StringBuilder fullName = new StringBuilder();
for (int j = 2; j < [Link] - 2; j++) {
[Link](parts[j]).append(" ");
}
String name = [Link]().trim();
String lastName = [Link]([Link](" ") + 1);
double hourlyRate = [Link](parts[[Link] - 2]);
int hours = [Link](parts[[Link] - 1]);
[Link](new PartTimeEmployee(id, lastName, hourlyRate,
hours));
}
}

for (Employee e : employees) {


[Link]();
}

[Link]();
}
}

Bài 2:Ct tính hh với Interface lớp triển khai


import [Link];

interface IShape {
float PI = 3.1416f;
void showInfo();
float getArea();
float getPerimeter();
}

class Circle implements IShape {


private float radius;

public Circle(float radius) {


[Link] = radius;
}

@Override
public float getArea() {
return PI * radius * radius;
}

@Override
public float getPerimeter() {
return 2 * PI * radius;
}

@Override
public void showInfo() {
[Link]("Hình tròn: diện tích %.2f, chu vi %.2f%n", getArea(),
getPerimeter());
}
}

class Rectangle implements IShape {


private double width;
private double length;

public Rectangle(double width, double length) {


[Link] = width;
[Link] = length;
}

@Override
public float getArea() {
return (float)(width * length);
}

@Override
public float getPerimeter() {
return (float)(2 * (width + length));
}

@Override
public void showInfo() {
[Link]("Hình chữ nhật: diện tích %.2f, chu vi %.2f%n",
getArea(), getPerimeter());
}
}

public class Main {


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

float r = [Link]();
double w = [Link]();
double l = [Link]();

Circle circle = new Circle(r);


Rectangle rectangle = new Rectangle(w, l);

[Link]();
[Link]();
}
}
Bài 3: Hệ thống quản lý Bảo trì và Tb phần cứng
import [Link];

// a. Interface Maintainable
interface Maintainable {
void maintain();
void showStaffInfo();
}

// b. Abstract class Device


abstract class Device {
protected String deviceID;
protected double price;

public Device(String deviceID, double price) {


[Link] = deviceID;
[Link] = price;
}

public abstract void showDeviceInfo();


}

// c. Lớp ServerDevice kế thừa Device


class ServerDevice extends Device {
private int ramCapacity;

public ServerDevice(String deviceID, double price, int ramCapacity) {


super(deviceID, price);
[Link] = ramCapacity;
}

@Override
public void showDeviceInfo() {
[Link]("=== Thông tin Thiết bị Server ===");
[Link]("Mã thiết bị: " + deviceID);
[Link]("Giá: %.2f\n", price);
[Link]("Dung lượng RAM: " + ramCapacity + " GB");
}
}

// d. Lớp Technician implements Maintainable


class Technician implements Maintainable {
private String staffName;
private String specialization;

public Technician(String staffName, String specialization) {


[Link] = staffName;
[Link] = specialization;
}

@Override
public void maintain() {
[Link]("Nhân viên " + staffName + " đang tiến hành bảo trì
hệ thống...");
}

@Override
public void showStaffInfo() {
[Link]("=== Thông tin Nhân viên Kỹ thuật ===");
[Link]("Họ tên: " + staffName);
[Link]("Chuyên môn: " + specialization);
}
}

// e. Hàm main
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Nhập thông tin nhân viên


String staffName = [Link]().trim();
String specialization = [Link]().trim();

// Nhập thông tin thiết bị


String deviceID = [Link]().trim();
double price = [Link]();
int ramCapacity = [Link]();

// Ràng buộc đầu vào


if ([Link]() || [Link]() ||
[Link]()) {
[Link]("Thông tin không được để trống!");
return;
}
if (price <= 0) {
[Link]("Giá thành phải lớn hơn 0!");
return;
}
if (ramCapacity < 4) {
[Link]("Dung lượng RAM phải ≥ 4 GB!");
return;
}
// Tạo đối tượng
Technician technician = new Technician(staffName, specialization);
ServerDevice server = new ServerDevice(deviceID, price, ramCapacity);

// Gọi phương thức


[Link]();
[Link]();
[Link]();
}
}
Bài 4 : Quản lý đơn hàng giao đồ ăn
import [Link];
import [Link];
import [Link];

abstract class DeliveryOrder {


protected String orderId;
protected double distance;

public DeliveryOrder(String orderId, double distance) {


[Link] = orderId;
[Link] = distance;
}

public abstract double calculateDeliveryFee();

public String getOrderId() {


return orderId;
}
}

class BikeDelivery extends DeliveryOrder {


public BikeDelivery(String orderId, double distance) {
super(orderId, distance);
}

@Override
public double calculateDeliveryFee() {
return distance * 5000;
}
}

class CarDelivery extends DeliveryOrder {


public CarDelivery(String orderId, double distance) {
super(orderId, distance);
}

@Override
public double calculateDeliveryFee() {
return distance * 8000;
}
}
class ExpressDelivery extends DeliveryOrder {
public ExpressDelivery(String orderId, double distance) {
super(orderId, distance);
}

@Override
public double calculateDeliveryFee() {
return distance * 12000 + 20000;
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
List<DeliveryOrder> orders = new ArrayList<>();

int n = [Link](); // số lượng đơn hàng

for (int i = 0; i < n; i++) {


String type = [Link](); // loại giao hàng (BIKE/CAR/EXPRESS)
String orderId = [Link](); // mã đơn hàng
double distance = [Link](); // khoảng cách

DeliveryOrder order;
switch ([Link]()) {
case "bike":
order = new BikeDelivery(orderId, distance);
break;
case "car":
order = new CarDelivery(orderId, distance);
break;
case "express":
order = new ExpressDelivery(orderId, distance);
break;
default:
[Link]("Loai giao hang khong hop le!");
continue;
}
[Link](order);
}

// In kết quả theo định dạng yêu cầu


for (DeliveryOrder order : orders) {
[Link]("%s %.2f%n", [Link](),
[Link]());
}

[Link]();
}
}
Bài 5: Quản lý thiết bị điện tử
import [Link];
import [Link];

class ElectronicDevice {
private String deviceId;
private String deviceName;
private double price;
private int warrantyMonths;

public void setDeviceId(String deviceId) { [Link] = deviceId; }


public void setDeviceName(String deviceName) { [Link] = deviceName;
}
public void setPrice(double price) {
if (price > 0) [Link] = price;
}
public void setWarrantyMonths(int warrantyMonths) {
if (warrantyMonths >= 0 && warrantyMonths <= 60) [Link] =
warrantyMonths;
}

public void displayInfo() {


DecimalFormat df = new DecimalFormat("0.00"); // luôn hiển thị 2 chữ số
thập phân
[Link]("Ma thiet bi: " + deviceId);
[Link]("Ten thiet bi: " + deviceName);
[Link]("Gia: " + [Link](price) + " VND");
[Link]("Bao hanh: " + warrantyMonths + " thang");
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
ElectronicDevice device = new ElectronicDevice();

[Link]([Link]());
[Link]([Link]());
[Link]([Link]([Link]()));
[Link]([Link]([Link]()));

[Link]();
[Link]();
}
}
Bài 6: Quản lý thuế thu nhập cá nhân
import [Link].*;
import [Link];

class NguoiNopThue {
private String hoTen;
private String maSoThue;
private double thuNhap;
private int soNguoiPhuThuoc;

public NguoiNopThue(String hoTen, String maSoThue, double thuNhap, int


soNguoiPhuThuoc) {
[Link] = hoTen;
[Link] = maSoThue;
[Link] = thuNhap;
[Link] = soNguoiPhuThuoc;
}

public double tinhThue() {


double giamTruBanThan = 11.0;
double giamTruPhuThuoc = 4.4 * soNguoiPhuThuoc;
double thuNhapChiuThue = thuNhap - giamTruBanThan - giamTruPhuThuoc;

if (thuNhapChiuThue <= 0) return 0;

if (thuNhapChiuThue <= 5) {
return thuNhapChiuThue * 0.05;
} else {
return thuNhapChiuThue * 0.10;
}
}

public void hienThi() {


DecimalFormat df = new DecimalFormat("0.00");
[Link](hoTen + " | " + maSoThue + " | " +
[Link](tinhThue()));
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]([Link]());
List<NguoiNopThue> ds = new ArrayList<>();
for (int i = 0; i < n; i++) {
String hoTen = [Link]();
String maSoThue = [Link]();
double thuNhap = [Link]([Link]());
int soNguoiPhuThuoc = [Link]([Link]());
[Link](new NguoiNopThue(hoTen, maSoThue, thuNhap, soNguoiPhuThuoc));
}
for (NguoiNopThue nnt : ds) {
[Link]();
}
[Link]();
}
}
Bài 7: Quản lý phương tiện giao thông
import [Link].*;

interface ITaxable {
double calcTax();
}

abstract class Vehicle {


private String brand;
protected String plateNumber;
protected double basePrice;

public Vehicle(String brand, String plateNumber, double basePrice) {


[Link] = brand;
[Link] = plateNumber;
[Link] = basePrice;
}

public String getBrand() {


return brand;
}

public abstract void showInfo();


}

class Car extends Vehicle implements ITaxable {


private int seatCount;

public Car(String brand, String plateNumber, double basePrice, int


seatCount) {
super(brand, plateNumber, basePrice);
[Link] = seatCount;
}

@Override
public double calcTax() {
if (seatCount <= 5) {
return basePrice * 0.10;
} else {
return basePrice * 0.12;
}
}

@Override
public void showInfo() {
double tax = calcTax();
double total = basePrice + tax;
[Link]("=== Car ===");
[Link]("Hãng: %s%n", getBrand());
[Link]("Biển số: %s%n", plateNumber);
[Link]("Giá cơ bản: %.2f%n", basePrice);
[Link]("Số ghế: %d%n", seatCount);
[Link]("Thuế: %.2f%n", tax);
[Link]("Tổng tiền: %.2f%n", total);
[Link]();
}
}

class Motorbike extends Vehicle implements ITaxable {


private int engineCC;

public Motorbike(String brand, String plateNumber, double basePrice, int


engineCC) {
super(brand, plateNumber, basePrice);
[Link] = engineCC;
}

@Override
public double calcTax() {
if (engineCC < 150) {
return basePrice * 0.05;
} else {
return basePrice * 0.08;
}
}

@Override
public void showInfo() {
double tax = calcTax();
double total = basePrice + tax;
[Link]("=== Motorbike ===");
[Link]("Hãng: %s%n", getBrand());
[Link]("Biển số: %s%n", plateNumber);
[Link]("Giá cơ bản: %.2f%n", basePrice);
[Link]("Dung tích: %d%n", engineCC);
[Link]("Thuế: %.2f%n", tax);
[Link]("Tổng tiền: %.2f%n", total);
[Link]();
}
}

public class Main {


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

// Nhập Car
String carBrand = [Link]().trim();
String carPlate = [Link]().trim();
double carPrice = [Link]([Link]().trim());
int seatCount = [Link]([Link]().trim());
Car car = new Car(carBrand, carPlate, carPrice, seatCount);

// Nhập Motorbike
String bikeBrand = [Link]().trim();
String bikePlate = [Link]().trim();
double bikePrice = [Link]([Link]().trim());
int engineCC = [Link]([Link]().trim());
Motorbike bike = new Motorbike(bikeBrand, bikePlate, bikePrice,
engineCC);

// Hiển thị thông tin


[Link]();
[Link]();

[Link]();
}
}

Bài 8: Hệ thống quản lý khóa học và giảng viên


import [Link];

interface ITeacher {
void teach();
void showTeacherInfo();
}

abstract class Course {


protected String courseName;
protected int duration;

public Course(String courseName, int duration) {


[Link] = courseName;
[Link] = duration;
}

public abstract void showCourseInfo();


}

class OnlineCourse extends Course {


private String platform;

public OnlineCourse(String courseName, int duration, String platform) {


super(courseName, duration);
[Link] = platform;
}

@Override
public void showCourseInfo() {
[Link]("=== Thông tin Khóa học Online ===");
[Link]("Tên khóa học: " + courseName);
[Link]("Số buổi: " + duration);
[Link]("Nền tảng: " + platform);
}
}

class Lecturer implements ITeacher {


private String name;
private String level;
public Lecturer(String name, String level) {
[Link] = name;
[Link] = level;
}

@Override
public void teach() {
[Link]( name + " đang bắt đầu giảng dạy...");
}

@Override
public void showTeacherInfo() {
[Link]("=== Thông tin Giảng viên ===");
[Link]("Họ tên: " + name);
[Link]("Trình độ: " + level);
}
}

public class Main {


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

String lecName = [Link]().trim();


String level = [Link]().trim();
String courseName = [Link]().trim();
int duration = [Link]([Link]().trim());
String platform = [Link]().trim();

// Ràng buộc cơ bản


if ([Link]() || [Link]() || [Link]() ||
[Link]() || duration < 1) {
[Link]("Dữ liệu nhập không hợp lệ!");
[Link]();
return;
}

Lecturer lecturer = new Lecturer(lecName, level);


OnlineCourse course = new OnlineCourse(courseName, duration, platform);

[Link]();
[Link]();
[Link]();

[Link]();
}
}

Bài 9: Quản lý tài khoản ngân hàng với lớp trừu


import [Link];

abstract class BankAccount {


private String ownerName;
protected double balance;

public BankAccount(String ownerName, double balance) {


[Link] = ownerName;
[Link] = balance;
}

public String getOwnerName() {


return ownerName;
}

public double getBalance() {


return balance;
}

public abstract double calcInterest();


public abstract void showInfo();
}

class SavingAccount extends BankAccount {


private double interestRate;

public SavingAccount(String ownerName, double balance, double interestRate)


{
super(ownerName, balance);
[Link] = interestRate;
}

@Override
public double calcInterest() {
return balance * (interestRate / 100);
}

@Override
public void showInfo() {
[Link]("=== Tài khoản Tiết kiệm ===");
[Link]("Chủ tài khoản: %s%n", getOwnerName());
[Link]("Số dư: %.2f%n", getBalance());
[Link]("Lãi: %.2f%n", calcInterest());
}
}

class CheckingAccount extends BankAccount {


private double fee;

public CheckingAccount(String ownerName, double balance, double fee) {


super(ownerName, balance);
[Link] = fee;
}

@Override
public double calcInterest() {
return 0; // không có lãi
}

@Override
public void showInfo() {
[Link]("=== Tài khoản Thanh toán ===");
[Link]("Chủ tài khoản: %s%n", getOwnerName());
[Link]("Số dư: %.2f%n", getBalance());
[Link]("Phí duy trì: %.2f%n", fee);
}
}

public class Main {


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

// Nhập SavingAccount
String name1 = [Link]();
double bal1 = [Link]();
double rate = [Link]();
[Link]();

SavingAccount sa = new SavingAccount(name1, bal1, rate);

// Nhập CheckingAccount
String name2 = [Link]();
double bal2 = [Link]();
double fee = [Link]();

CheckingAccount ca = new CheckingAccount(name2, bal2, fee);

// Hiển thị thông tin


[Link]();
[Link]();

[Link]();
}
}

Bài 10: Ct thực hiện các phép toán số học cb


import [Link];

// Interface IMathOperation
interface IMathOperation {
double PI = 3.1416;
void calculate();
void showInfo();
}

// Addition class
class Addition implements IMathOperation {
private float operand1;
private float operand2;
private float result;

public Addition(float operand1, float operand2) {


this.operand1 = operand1;
this.operand2 = operand2;
}

@Override
public void calculate() {
result = operand1 + operand2;
}

@Override
public void showInfo() {
calculate();
[Link]("Lớp: Addition");
[Link]("%.2f + %.2f = %.2f%n", operand1, operand2, result);
}
}

// Subtraction class
class Subtraction implements IMathOperation {
private float operand1;
private float operand2;
private float result;

public Subtraction(float operand1, float operand2) {


this.operand1 = operand1;
this.operand2 = operand2;
}

@Override
public void calculate() {
result = operand1 - operand2;
}

@Override
public void showInfo() {
calculate();
[Link]("Lớp: Subtraction");
[Link]("%.2f - %.2f = %.2f%n", operand1, operand2, result);
}
}

// Multiplication class
class Multiplication implements IMathOperation {
private float operand1;
private float operand2;
private float result;

public Multiplication(float operand1, float operand2) {


this.operand1 = operand1;
this.operand2 = operand2;
}

@Override
public void calculate() {
result = operand1 * operand2;
}

@Override
public void showInfo() {
calculate();
[Link]("Lớp: Multiplication");
[Link]("%.2f * %.2f = %.2f%n", operand1, operand2, result);
}
}

// Main class
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Addition input
float a1 = [Link]();
float a2 = [Link]();

// Subtraction input
float s1 = [Link]();
float s2 = [Link]();

// Multiplication input
float m1 = [Link]();
float m2 = [Link]();

Addition addition = new Addition(a1, a2);


Subtraction subtraction = new Subtraction(s1, s2);
Multiplication multiplication = new Multiplication(m1, m2);

[Link]();
[Link]();
[Link]();

[Link](); }}
Bài 11: Quản lý sinh viên NEU
import [Link];

// Interface IStaff
interface IStaff {
void work();
}

// Interface IStudent
interface IStudent {
void study();
}
// Abstract class Person
abstract class Person {
String name; // default
private int age; // private
protected String ID; // protected
public String birthDate; // public

public Person(String name, int age, String ID, String birthDate) {


[Link] = name;
[Link] = age;
[Link] = ID;
[Link] = birthDate;
}

public int getAge() {


return age;
}

public void setAge(int age) {


if (age > 0) {
[Link] = age;
}
}

public abstract void showInfo();


}

// Class NEUStudent
class NEUStudent extends Person implements IStaff, IStudent {
protected String studentID;

public NEUStudent(String name, int age, String ID, String birthDate, String
studentID) {
super(name, age, ID, birthDate);
[Link] = studentID;
}

@Override
public void showInfo() {
[Link]("----- Thông tin sinh viên -----");
[Link]("Tên: " + name);
[Link]("Tuổi: " + getAge());
[Link]("ID: " + ID);
[Link]("Ngày sinh: " + birthDate);
[Link]("Mã sinh viên: " + studentID);
}

@Override
public void work() {
[Link](name + " đang làm việc tại khoa hoặc CLB...");
}

@Override
public void study() {
[Link](name + " đang học tập chăm chỉ tại NEU...");
}
}

// Main class
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Input student 1
String name1 = [Link]();
int age1 = [Link]();
[Link]();
String id1 = [Link]();
String birth1 = [Link]();
String stuID1 = [Link]();

// Input student 2
String name2 = [Link]();
int age2 = [Link]();
[Link]();
String id2 = [Link]();
String birth2 = [Link]();
String stuID2 = [Link]();

NEUStudent s1 = new NEUStudent(name1, age1, id1, birth1, stuID1);


NEUStudent s2 = new NEUStudent(name2, age2, id2, birth2, stuID2);

[Link]();
[Link]();
[Link]();

[Link]();

[Link]();
[Link]();
[Link]();

[Link](); }}

Bài 12: Quản lý sản phẩm trong siêu thị mini


import [Link];
import [Link];

// Lớp trừu tượng Product


abstract class Product {
private String name;
protected float price;
private String description;
protected int quantity;

public Product(String name, float price, String description, int quantity) {


[Link] = name;
[Link] = price;
[Link] = description;
[Link] = quantity;
}

public String getName() {


return name;
}
public void setName(String name) {
if (![Link]()) {
[Link] = name;
}
}

public String getDescription() {


return description;
}
public void setDescription(String description) {
[Link] = description;
}

public abstract void showInfo();


}

// Lớp Milk kế thừa Product


class Milk extends Product {
private LocalDate expirationDate;

public Milk(String name, float price, String description, int quantity,


LocalDate expirationDate) {
super(name, price, description, quantity);
[Link] = expirationDate;
}

@Override
public void showInfo() {

[Link]("Tên sản phẩm: " + getName());


[Link]("Mô tả: " + getDescription());
[Link]("Giá: %.2f%n", price);
[Link]("Số lượng: " + quantity);
[Link]("Ngày hết hạn: " + expirationDate);
}

public void checkExpired() {


LocalDate today = [Link]();
if ([Link](today)) {
[Link]("Sản phẩm đã hết hạn.");
} else {
[Link]("Sản phẩm vẫn còn hạn sử dụng.");
}
}
}

// Main class
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

String name = [Link]().trim();


float price = [Link]([Link]().trim());
String desc = [Link]().trim();
int quantity = [Link]([Link]().trim());
String dateStr = [Link]().trim();
LocalDate expDate = [Link](dateStr);

// Ràng buộc
if (price < 0 || quantity < 0 || [Link]()) {
[Link]("Dữ liệu nhập không hợp lệ!");
[Link]();
return;
}

Milk milk = new Milk(name, price, desc, quantity, expDate);

[Link]();
[Link]();

[Link]();
}
}

Bài 13: Quản lý động vật trong trang trại


import [Link];

// Interface Animal
interface Animal {
void eat();
void showInfo();
}

// Interface Bird extends Animal


interface Bird extends Animal {
void fly();
}

// Interface Horse extends Animal


interface Horse extends Animal {
void run();
}

// Class Pegasus implements Bird và Horse


class Pegasus implements Bird, Horse {
public String name;
private int age;
public Pegasus(String name, int age) {
[Link] = name;
[Link] = age;
}

// Getter/Setter cho age


public int getAge() {
return age;
}

public void setAge(int age) {


if (age > 0) {
[Link] = age;
}
}

@Override
public void showInfo() {
[Link]("Tên: " + name);
[Link]("Tuổi: " + age);
}

@Override
public void eat() {
[Link](name + " đang ăn cỏ...");
}

@Override
public void fly() {
[Link](name + " đang bay trên bầu trời...");
}

@Override
public void run() {
[Link](name + " đang chạy rất nhanh...");
}
}

// Main class
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

String name = [Link]().trim();


int age = [Link]();

if ([Link]() || age <= 0) {


[Link]("Dữ liệu nhập không hợp lệ!");
[Link]();
return;
}

Pegasus p = new Pegasus(name, age);


[Link]();
[Link]();
[Link]();
[Link]();

[Link]();
}
}

Bài 14:Ct quản lý nhân sự lớp trừu tượng & kế thừa


import [Link];
import [Link];

// Lớp trừu tượng Employee


abstract class Employee {
private String name;
private LocalDate started;

public Employee(String name, LocalDate started) {


[Link] = name;
[Link] = started;
}

public String getName() {


return name;
}

public void setName(String name) {


if (![Link]()) {
[Link] = name;
}
}

public LocalDate getStarted() {


return started;
}

public void setStarted(LocalDate started) {


[Link] = started;
}

public abstract void showInfo();


public abstract double calcSalary();
}

// FullTimeEmployee
class FullTimeEmployee extends Employee {
private double monthlySalary;
private double bonus;
public FullTimeEmployee(String name, LocalDate started, double
monthlySalary, double bonus) {
super(name, started);
[Link] = monthlySalary;
[Link] = bonus;
}

@Override
public double calcSalary() {
return monthlySalary + bonus;
}

@Override
public void showInfo() {
[Link]("=== Nhân viên Full-Time ===");
[Link]("Tên: " + getName());
[Link]("Ngày bắt đầu: " + getStarted());
[Link]("Lương cơ bản: %.2f%n", monthlySalary);
[Link]("Thưởng: %.2f%n", bonus);
[Link]("Lương thực nhận: %.2f%n", calcSalary());
}
}

// PartTimeEmployee
class PartTimeEmployee extends Employee {
private int workingHour;
private double rate;

public PartTimeEmployee(String name, LocalDate started, int workingHour,


double rate) {
super(name, started);
[Link] = workingHour;
[Link] = rate;
}

@Override
public double calcSalary() {
return workingHour * rate;
}

@Override
public void showInfo() {
[Link]("=== Nhân viên Part-Time ===");
[Link]("Tên: " + getName());
[Link]("Ngày bắt đầu: " + getStarted());
[Link]("Số giờ làm: " + workingHour);
[Link]("Đơn giá/giờ: %.2f%n", rate);
[Link]("Lương thực nhận: %.2f%n", calcSalary());
}
}

// Main
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Full-time employee
String ftName = [Link]().trim();
LocalDate ftStarted = [Link]([Link]().trim());
double ftSalary = [Link]([Link]().trim());
double ftBonus = [Link]([Link]().trim());

FullTimeEmployee fte = new FullTimeEmployee(ftName, ftStarted, ftSalary,


ftBonus);

// Part-time employee
String ptName = [Link]().trim();
LocalDate ptStarted = [Link]([Link]().trim());
int ptHour = [Link]([Link]().trim());
double ptRate = [Link]([Link]().trim());

PartTimeEmployee pte = new PartTimeEmployee(ptName, ptStarted, ptHour,


ptRate);

// Hiển thị thông tin


[Link]();
[Link]();
[Link]();

[Link]();
}
}

Bài 15:Ql đv sở thú bằng kế thừa & trừu tượng


import [Link];

abstract class Animal {


protected int age;
protected String gender;

public Animal(int age, String gender) {


[Link] = age;
[Link] = gender;
}

public boolean isMammal() {


return false;
}

public abstract void showInfo();


}

class Duck extends Animal {


public String color;

public Duck(int age, String gender, String color) {


super(age, gender);
[Link] = color;
}

@Override
public void showInfo() {
[Link]("=== Thông Tin Vịt ===");
[Link]("Tuổi: " + age);
[Link]("Giới tính: " + gender);
[Link]("Màu sắc: " + color);
[Link]("Có phải động vật có vú? " + isMammal());
}

public void swim() {


[Link]("Vịt đang bơi...");
}

public void quack() {


[Link]("Vịt kêu: Quack Quack!");
}
}

class Fish extends Animal {


private int size;
private boolean canEat;

public Fish(int age, String gender, int size, boolean canEat) {


super(age, gender);
[Link] = size;
[Link] = canEat;
}

@Override
public void showInfo() {
[Link]("=== Thông Tin Cá ===");
[Link]("Tuổi: " + age);
[Link]("Giới tính: " + gender);
[Link]("Kích thước: " + size);
[Link]("Có thể ăn động vật khác? " + canEat);
[Link]("Có phải động vật có vú? " + isMammal());
}

public void swim() {


[Link]("Cá đang bơi...");
}
}

class Horse extends Animal {


private boolean isWild;

public Horse(int age, String gender, boolean isWild) {


super(age, gender);
[Link] = isWild;
}
@Override
public boolean isMammal() {
return true;
}

@Override
public void showInfo() {
[Link]("=== Thông Tin Ngựa ===");
[Link]("Tuổi: " + age);
[Link]("Giới tính: " + gender);
[Link]("Hoang dã: " + isWild);
[Link]("Có phải động vật có vú? " + isMammal());
}

public void run() {


[Link]("Ngựa đang chạy...");
}
}

public class Main {


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

// Nhập thông tin Vịt


int ageDuck = [Link]();
String genderDuck = [Link]();
String color = [Link]();

// Nhập thông tin Cá


int ageFish = [Link]();
String genderFish = [Link]();
int size = [Link]();
boolean canEat = [Link]();

// Nhập thông tin Ngựa


int ageHorse = [Link]();
String genderHorse = [Link]();
boolean isWild = [Link]();

Duck duck = new Duck(ageDuck, genderDuck, color);


Fish fish = new Fish(ageFish, genderFish, size, canEat);
Horse horse = new Horse(ageHorse, genderHorse, isWild);

[Link]();
[Link]();
[Link]();

[Link]();

[Link]();
[Link]();
[Link]();

[Link]();
[Link]();

[Link]();
}
}

You might also like