Bài tập lập trình hướng đối tượng – composition

Trong loạt bài tập về composition sẽ giúp chúng ta ôn lại các kiến về lập trình hướng đối tượng như: 

Note

  • Dấu (-) tương ứng với private access modifier
  • Dấu (+) tương ứng với public access modifier
  • Dấu (#) tương ứng với protected access modifier

Bài 1: Triển khai class Book và Author theo diagram

exercise-oop-composition-book-author

Chúng ta có 2 class Author và Book. Một author sẽ có các thông tin cơ bản như name, email, genderm age. Một cuốn sách ngoài các thông tin cơ bản như name, price etc thì còn có một Author cho nó.

Ở đây chúng ta thấy class Book sẽ có một thuộc tính là Author, như vậy ta nói Book has – a Author.

// File Author.java
public class Author {
    private String name;
    private String email;
    private String gender;
    private int age;

    public Author(String name, String email, String gender, int age) {
        this.name = name;
        this.email = email;
        this.gender = gender;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public String getGender() {
        return gender;
    }

    public int getAge() {
        return age;
    }

    public String toString() {
        return "Author{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }
}
// File Book.java
public class Book {
    private String name;
    private Author author;
    private double price;
    private int count;

    public Book(String name, Author author, double price, int count) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.count = count;
    }

    public String getName() {
        return name;
    }

    public Author getAuthor() {
        return author;
    }

    public double getPrice() {
        return price;
    }

    public int count() {
        return count;
    }

    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author=" + author.getName() +
                ", price=" + price +
                ", count=" + count +
                '}';
    }
}
// File Main.java
public class Main {
    public static void main(String[] args) {
        Author author = new Author("shareprogramming.net", "[email protected]", "male", 23);
        Book book = new Book("Java OOP", author, 10, 100);
        System.out.println(book.toString());
    }
}

Output

Book{name=’Java OOP’, author=shareprogramming.net, price=10.0, count=100}

Link source code tham khảo

Bài 2: Triển khai class Student và Address

exercise-oop-composition-student-address

Gợi ý: 

getRating(): Trả về good nếu score >=8, medium nếu score thuộc [5, 8), bad nếu score < 5

// File Address.java
public class Address {
    private String country;
    private String city;
    private String district;
    private String street;

    public Address(String country, String city, String district, String street) {
        this.country = country;
        this.city = city;
        this.district = district;
        this.street = street;
    }

    public String getCountry() {
        return country;
    }

    public String getCity() {
        return city;
    }

    public String getDistrict() {
        return district;
    }

    public String getStreet() {
        return street;
    }

    public String toString() {
        return "Address{" +
                "country='" + country + '\'' +
                ", city='" + city + '\'' +
                ", district='" + district + '\'' +
                ", street='" + street + '\'' +
                '}';
    }
}
// File Student.java
public class Student {
    private String name;
    private int age;
    private double score;
    private Address address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getRating() {
        if (this.score < 5.0) {
            return "bad";
        } else if (this.score >= 5.0 && this.score < 8.0) {
            return "medium";
        } else {
            return "good";
        }
    }

    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                ", address=" + address.toString() +
                '}';
    }
}
// File Main.java
public class Main {

    public static void main(String[] args) {
        Address address = new Address("VN", "HCM", "Q7", "428 LE VAN LUONG");
        Student student = new Student();
        student.setName("HGA");
        student.setAddress(address);
        student.setAge(23);
        student.setScore(5);
        System.out.println(student.toString());
        System.out.println("Xep loai: " + student.getRating());
    }
}

Output: 

Student{name=’HGA’, age=23, score=5.0, address=Address{country=’VN’, city=’HCM’, district=’Q7′, street=’428 LE VAN LUONG’}}
Xep loai: medium

Link source code tham khảo

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x