Bài tập java hướng đối tượng co loi giai pdf năm 2024

Nhằm giúp các bạn rèn luyện kiến thức, dưới đây là danh sách 10 bài tập kế thừa trong Java từ cấp độ cơ bản cho người mới bắt đầu và trung cấp mang tính thử thách hơn! Hãy cùng Rikkei Academy luyện tập ngay thôi!

Bài tập kế thừa trong Java cấp độ cơ bản

Dưới đây là 5 bài tập về kế thừa trong Java cấp độ cơ bản

Bài 1: Tạo hai lớp Animal và Dog. Lớp Animal có phương thức makeSound(). Lớp Dog kế thừa từ lớp Animal và ghi đè phương thức makeSound().

Bài 2: Tạo lớp Rectangle với các thuộc tính length và width. Tạo lớp Square kế thừa từ lớp Rectangle và sử dụng phương thức khởi tạo (constructor) để thiết lập độ dài và độ rộng bằng nhau.

Bài 3: Tạo lớp Person với các thuộc tính name và age. Sau đó, tạo lớp Student kế thừa từ lớp Person và thêm thuộc tính course.

Bài 4: Tạo lớp Shape với phương thức area(). Tạo hai lớp kế thừa từ lớp Shape: lớp Circle và lớp Triangle. Ghi đè phương thức area() trong cả hai lớp kế thừa.

Bài 5: Tạo lớp Vehicle với các thuộc tính brand và speed. Tạo lớp Car và lớp Bike kế thừa từ lớp Vehicle. Thêm thuộc tính numOfWheels vào lớp Car và lớp Bike.

Bài tập kế thừa trong Java cấp độ trung cấp

Dưới đây là 5 bài ập về kế thừa Java cấp độ trung cấp

Bài 6: Tạo lớp BankAccount với các thuộc tính accountNumber và balance. Tạo lớp SavingsAccount kế thừa từ lớp BankAccount và thêm thuộc tính interestRate.

Bài 7: Tạo lớp Publication với các thuộc tính title và price. Tạo lớp Book và lớp Magazine kế thừa từ lớp Publication. Thêm thuộc tính author vào lớp Book và thuộc tính issue vào lớp Magazine.

Bài 8: Tạo lớp Employee với các thuộc tính name, id và salary. Tạo lớp Manager kế thừa từ lớp Employee và thêm thuộc tính subordinates (một danh sách các đối tượng Employee).

Bài 9: Tạo lớp Instrument với phương thức play(). Tạo các lớp Guitar, Piano và Drums kế thừa từ lớp Instrument. Ghi đè phương thức play() trong mỗi lớp kế thừa.

Bài 10: Tạo lớp Furniture với các thuộc tính material và price. Tạo lớp Chair và lớp Table kế thừa từ lớp Furniture. Thêm thuộc tính numOfLegs vào lớp Chair và thuộc tính shape vào lớp Table.

Lời giải bài tập kế thừa trong Java cấp độ cơ bản

Hãy thử so sánh lời giải bạn viết so với lời giải của Rikkei Academy nhé!

Bài 1:

// Bước 1: Tạo lớp Animal với phương thức makeSound()

class Animal {

void makeSound() {

System.out.println(“The animal makes a sound”);

}

}

// Bước 2: Tạo lớp Dog kế thừa từ lớp Animal và ghi đè phương thức makeSound()

class Dog extends Animal {

@Override

void makeSound() {

System.out.println(“The dog barks”);

}

}

// Bước 3: Tạo đối tượng Dog và gọi phương thức makeSound()

public class Main {

public static void main(String[] args) {

Dog dog = new Dog();

dog.makeSound();

}

}

Bài 2:

// Bước 1: Tạo lớp Rectangle với các thuộc tính length và width

class Rectangle {

int length;

int width;

Rectangle(int length, int width) {

this.length = length;

this.width = width;

}

}

// Bước 2: Tạo lớp Square kế thừa từ lớp Rectangle

class Square extends Rectangle {

Square(int side) {

super(side, side);

}

}

// Bước 3: Tạo đối tượng Square và kiểm tra thuộc tính length và width

public class Main {

public static void main(String[] args) {

Square square = new Square(4);

System.out.println(“Length: ” + square.length);

System.out.println(“Width: ” + square.width);

}

}

Bài 3:

// Bước 1: Tạo lớp Person với các thuộc tính name và age

class Person {

String name;

int age;

Person(String name, int age) {

this.name = name;

this.age = age;

}

}

// Bước 2: Tạo lớp Student kế thừa từ lớp Person và thêm thuộc tính course

class Student extends Person {

String course;

Student(String name, int age, String course) {

super(name, age);

this.course = course;

}

}

// Bước 3: Tạo đối tượng Student và kiểm tra các thuộc tính

public class Main {

public static void main(String[] args) {

Student student = new Student(“John”, 20, “Computer Science”);

System.out.println(“Name: ” + student.name);

System.out.println(“Age: ” + student.age);

System.out.println(“Course: ” + student.course);

}

}

Bài 4:

// Bước 1: Tạo lớp Shape với phương thức area()

class Shape {

double area() {

return 0;

}

}

// Bước 2: Tạo lớp Circle kế thừa từ lớp Shape và ghi đè phương thức area()

class Circle extends Shape {

int radius;

Circle(int radius) {

this.radius = radius;

}

@Override

double area() {

return Math.PI * radius * radius;

}

}

// Bước 3: Tạo lớp Trianglekế thừa từ lớp Shape và ghi đè phương thức area()

class Triangle extends Shape {

int base;

int height;

Triangle(int base, int height) {

this.base = base;

this.height = height;

}

@Override

double area() {

return 0.5 * base * height;

}

}

// Bước 4: Tạo đối tượng Circle và Triangle, sau đó gọi phương thức area()

public class Main {

public static void main(String[] args) {

Circle circle = new Circle(5);

System.out.println(“Circle area: ” + circle.area());

Triangle triangle = new Triangle(4, 7);

System.out.println(“Triangle area: ” + triangle.area());

}

}

Bài 5:

// Bước 1: Tạo lớp Vehicle với thuộc tính speed

class Vehicle {

int speed;

Vehicle(int speed) {

this.speed = speed;

}

}

// Bước 2: Tạo lớp Car kế thừa từ lớp Vehicle và thêm phương thức accelerate()

class Car extends Vehicle {

Car(int speed) {

super(speed);

}

void accelerate() {

speed += 10;

}

}

// Bước 3: Tạo lớp Bike kế thừa từ lớp Vehicle và thêm phương thức accelerate()

class Bike extends Vehicle {

Bike(int speed) {

super(speed);

}

void accelerate() {

speed += 5;

}

}

// Bước 4: Tạo đối tượng Car và Bike, sau đó gọi phương thức accelerate()

public class Main {

public static void main(String[] args) {

Car car = new Car(50);

car.accelerate();

System.out.println(“Car speed: ” + car.speed);

Bike bike = new Bike(20);

bike.accelerate();

System.out.println(“Bike speed: ” + bike.speed);

}

}

Đáp án bài tập kế thừa trong Java cấp độ trung cấp

Dưới đây là đáp án các bài tập kế thừa trong Java trung cấp:

Bài 6:

// Bước 1: Tạo lớp BankAccount với các thuộc tính accountNumber và balance

class BankAccount {

int accountNumber;

double balance;

BankAccount(int accountNumber, double balance) {

this.accountNumber = accountNumber;

this.balance = balance;

}

// Bước 2: Tạo phương thức deposit() để nạp tiền vào tài khoản

void deposit(double amount) {

balance += amount;

}

// Bước 3: Tạo phương thức withdraw() để rút tiền từ tài khoản

boolean withdraw(double amount) {

if (balance >= amount) {

balance -= amount;

return true;

}

return false;

}

// Bước 4: Tạo phương thức getBalance() để lấy số dư hiện tại của tài khoản

double getBalance() {

return balance;

}

}

// Bước 5: Tạo đối tượng BankAccount và thử nghiệm các phương thức

public class Main {

public static void main(String[] args) {

BankAccount account = new BankAccount(123456, 1000);

account.deposit(500);

System.out.println(“Balance after deposit: ” + account.getBalance());

account.withdraw(800);

System.out.println(“Balance after withdraw: ” + account.getBalance());

}

}

Bài 7:

// Bước 1: Tạo lớp Employee với các thuộc tính name và salary

class Employee {

String name;

double salary;

Employee(String name, double salary) {

this.name = name;

this.salary = salary;

}

// Bước 2: Tạo phương thức raiseSalary() để tăng lương

void raiseSalary(double percent) {

salary += salary * percent / 100;

}

}

// Bước 3: Tạo đối tượng Employee và thử nghiệm phương thức raiseSalary()

public class Main {

public static void main(String[] args) {

Employee employee = new Employee(“John”, 3000);

System.out.println(“Initial salary: ” + employee.salary);

employee.raiseSalary(10);

System.out.println(“Salary after raise: ” + employee.salary);

}

}

Bài 8:

// Bước 1: Tạo lớp ComplexNumber với các thuộc tính real và imaginary

class ComplexNumber {

double real;

double imaginary;

ComplexNumber(double real, double imaginary) {

this.real = real;

this.imaginary = imaginary;

}

// Bước 2: Tạo phương thức add() để cộng hai số phức

ComplexNumber add(ComplexNumber other) {

return new ComplexNumber(real + other.real, imaginary + other.imaginary);

}

// Bước 3: Tạo phương thức subtract() để trừ hai số phức

ComplexNumber subtract(ComplexNumber other) {

return new ComplexNumber(real – other.real, imaginary – other.imaginary);

}

// Bước 4: Tạo phương thức multiply() để nhân hai số phức

ComplexNumber multiply(ComplexNumber other) {

double newReal = real * other.real – imaginary * other.imaginary;

double newImaginary = real * other.imaginary + imaginary * other.real;

return new ComplexNumber(newReal, newImaginary);

}

}

// Bước 5: Tạo các đối tượng ComplexNumber và thử nghiệm các phương thức

public class Main {

public static void main(String[] args) {

ComplexNumber num1 = new ComplexNumber(3, 2);

ComplexNumber num2 = new ComplexNumber(1, 7);

ComplexNumber result = num1.add(num2);

System.out.println(“Addition: ” + result.real + ” + ” + result.imaginary + “i”);

result = num1.subtract(num2);

System.out.println(“Subtraction: ” + result.real + ” + ” + result.imaginary + “i”);

result = num1.multiply(num2);

System.out.println(“Multiplication: ” + result.real + ” + ” + result.imaginary + “i”);

}

}

Bài 9:

// Bước 1: Tạo lớp Fraction với các thuộc tính numerator và denominator

class Fraction {

int numerator;

int denominator;

Fraction(int numerator, int denominator) {

this.numerator = numerator;

this.denominator = denominator;

}

// Bước 2: Tạo phương thức gcd() để tìm ước chung lớn nhất

int gcd(int a, int b) {

if (b == 0) {

return a;

}

return gcd(b, a % b);

}

// Bước 3: Tạo phương thức simplify() để rút gọn phân số

void simplify() {

int gcd = gcd(numerator, denominator);

numerator /= gcd;

denominator /= gcd;

}

// Bước 4: Tạo phương thức add() để cộng hai phân số

Fraction add(Fraction other) {

int newNumerator = numerator * other.denominator + other.numerator * denominator;

int newDenominator = denominator * other.denominator;

Fraction result = new Fraction(newNumerator, newDenominator);

result.simplify();

return result;

}

// Bước 5: Tạo phương thức subtract() để trừ hai phân số

Fraction subtract(Fraction other) {

int newNumerator = numerator * other.denominator – other.numerator * denominator;

int newDenominator = denominator * other.denominator;

Fraction result = new Fraction(newNumerator, newDenominator);

result.simplify();

return result;

}

// Bước 6: Tạo phương thức multiply() để nhân hai phân số

Fraction multiply(Fraction other) {

int newNumerator = numerator * other.numerator;

int newDenominator = denominator * other.denominator;

Fraction result = new Fraction(newNumerator, newDenominator);

result.simplify();

return result;

}

// Bước 7: Tạo phương thức divide() để chia hai phân số

Fraction divide(Fraction other) {

int newNumerator = numerator * other.denominator;

int newDenominator = denominator * other.numerator;

Fraction result = new Fraction(newNumerator, newDenominator);

result.simplify();

return result;

}

}

// Bước 8: Tạo các đối tượng Fraction và thử nghiệm các phương thức

public class Main {

public static void main(String[] args) {

Fraction frac1 = new Fraction(3, 4);

Fraction frac2 = new Fraction(1, 2);

Fraction result = frac1.add(frac2);

System.out.println(“Addition: ” + result.numerator + “/” + result.denominator);

result = frac1.subtract(frac2);

System.out.println(“Subtraction: ” + result.numerator + “/” + result.denominator);

result = frac1.multiply(frac2);

System.out.println(“Multiplication: ” + result.numerator + “/” + result.denominator);

result = frac1.divide(frac2);

System.out.println(“Division: ” + result.numerator + “/” + result.denominator);

}

}

Bài 10:

// Bước 1: Tạo lớp Matrix với các thuộc tính rows, cols và data

class Matrix {

int rows;

int cols;

int[][] data;

Matrix(int rows, int cols) {

this.rows = rows;

this.cols = cols;

data = new int[rows][cols];

}

// Bước 2: Tạo phương thức add() để cộng hai ma trận

Matrix add(Matrix other) {

if (rows != other.rows || cols != other.cols) {

throw new IllegalArgumentException(“Matrix dimensions do not match.”);

}

Matrix result = new Matrix(rows, cols);

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

for (int j = 0; j < cols; j++) {

result.data[i][j] = data[i][j] + other.data[i][j];

}

}

return result;

}

// Bước 3: Tạo phương thức subtract() để trừ hai ma trận

Matrix subtract(Matrix other) {

if (rows != other.rows || cols != other.cols) {

throw new IllegalArgumentException(“Matrix dimensions do not match.”);

}

Matrix result = new Matrix(rows, cols);

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

for (int j = 0; j < cols; j++) {

result.data[i][j] = data[i][j] – other.data[i][j];

}

}

return result;

}

// Bước 4: Tạo phương thức multiply() để nhân hai ma trận

Matrix multiply(Matrix other) {

if (cols != other.rows) {

throw new IllegalArgumentException(“Matrix dimensions do not match.”);

}

Matrix result = new Matrix(rows, other.cols);

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

for (int j = 0; j < other.cols; j++) {

int sum = 0;

for (int k = 0; k < cols; k++) {

sum += data[i][k] * other.data[k][j];

}

result.data[i][j] = sum;

}

}

return result;

}

}

// Bước 5: Tạo các đối tượng Matrix và thử nghiệm các phương thức

public class Main {

public static void main(String[] args) {

Matrix mat1 = new Matrix(2, 2);

mat1.data = new int[][]{{1, 2}, {3, 4}};

Matrix mat2 = new Matrix(2, 2);

mat2.data = new int[][]{{5, 6}, {7, 8}};

Matrix result = mat1.add(mat2);

System.out.println(“Addition:”);

for (int i = 0; i < result.rows; i++) {

for (int j = 0; j < result.cols; j++) {

System.out.print(result.data[i][j] + ” “);

}

System.out.println();

}

result = mat1.subtract(mat2);

System.out.println(“Subtraction:”);

for (int i = 0; i < result.rows; i++) {

for (int j = 0; j < result.cols; j++) {

System.out.print(result.data[i][j] + ” “);

}

System.out.println();

}

result = mat1.multiply(mat2);

System.out.println(“Multiplication:”);

for (int i = 0; i < result.rows; i++) {

for (int j = 0; j < result.cols; j++) {

System.out.print(result.data[i][j] + ” “);

}

System.out.println();

}

}

}

Kết luận

Trên đây là 10 bài tập kế thừa trong Java và lời giải. Bạn hãy tự thực hành trước khi xem lời giải để có thể thuần thục kiến thức về kế thừa nhé!