Mục Lục
Mục Lục
Hướng Dẫn Cài Đặt Môi Trường Video - Hướng dẫn cài đặt môi trường Video - Hướng dẫn tạo dự án Hello World Video Hướng dẫn phát hành dự án java - Lập trình Java căn bản Java Basic- Tạo dự án đầu tiên & phát hành dự án 01. Hướng dẫn cài đặt môi trường lập trình Java- lập trình Java 02. Hướng dẫn tạo dự án Java đầu tiên - lập trình Java căn bản Khai Báo Biến & Mệnh Đề Điều Kiện & Vòng Lặp In chuỗi Fibonaci Java Basic- In chuỗi ngược String in java Giải phương trình bậc nhất & Phương trình bậc 2 - Java 🌟 Bộ Bài Tập Tổng Hợp Java Core: Biến – Toán Tử – If/Else – Vòng Lặp – Bài Toán Nâng Cao Array Video Hướng dẫn Collections Java | List & ArrayList Java | Vector Java | Map & Hashmap Java | String Java Java Basic - Tìm dãy số nguyên tố trong mảng Video Test - Tìm kiếm chuỗi trong chuỗi OOP - Tích Chất Đóng Gói & Kế Thừa Video OOP - Tích chất bao đóng - Kế thừa trong Java - Lập trình Java căn bản Video OOP - Tính Chất Đa Hình - Tính Chất Trừu Tượng - Lập Trình Java Căn Bản Quản Lí Cán Bộ Video Quản Lí Cán Bộ OOP - Interface Video Java Basic- OOP - Tổng hợp - Quản lý sở thú TEST- Chương trình quản lý rạp chiếu film bằng java Video Hướng dẫn cú pháp Lambda Java | Tạo anonymous class Java | Interface Java & Abstract Java Video Hướng dẫn tìm hiểu Singleton Design Pattern Java | Singleton Object Java | OOP Java cho người mới Examination & Test Exam - Kiểm tra 60 phút lập trình OOP - Quản lý thiết bị máy tính - Lập trình OOP Video Exam - Kiểm tra 60 phút lập trình OOP - Quản lý thiết bị máy tính - Lập trình OOP [Examination] Xây dựng Hệ thống Phân cấp Đối tượng Hình học - Khoá Java Cơ Bản Java Swing Video Hướng dẫn học Java Swing | Ví du quản lý sinh viên Java | Java Swing cho người mới bắt đầu Ôn Tập Tổng Quát Java Basic Java basic- Assignment - Quản lý sách Java Basic - Quản lý ATM - Quản lý tài khoản ngân hàng Video Java Basic - Quản lý ATM - Quản lý tài khoản ngân hàng
Java Basic

Java basic- Overview - Viết chương trình quản lý xe car bằng java

1. Create an interface named ICar

Define an interface called ICar that declares the following methods:

  • calculateTax() → returns a float:
    Used to calculate the tax of the car.

  • calculatePrice() → returns a float:
    Used to calculate the total cost (total price) of the car.

  • getInfo() → returns void:
    Used to display the information of the car.

2. Create a class Car that implements the ICar interface

The Car class should implement all methods from ICar.
Declare the following private fields:

private String name; private String producer; private int year; private int seat; private float rootPrice;

Requirements:

  • Create two constructors:

    • A default (no-argument) constructor.

    • A parameterized constructor that initializes all attributes.

  • Generate getters and setters for all fields.

Implement methods:

  • calculateTax()
    Calculate the tax based on the number of seats:

    • If the car has fewer than 7 seats, then
      tax = rootPrice * 0.6

    • Otherwise,
      tax = rootPrice * 0.7

  • calculatePrice()
    Calculate the total price as:
    totalPrice = rootPrice + tax

  • getInfo()
    Display the car’s details in the following format:

    <car name> car produced by <producer> in <year> has <seat> seats with the total price of <totalPrice> $.

    Example:
    Ford car produced by Ford in 1997 has 4 seats with the total price of 20000 $.

3. Create a class LuxuryCar that extends the Car class

Add one new private attribute:

private float specialRate;

Requirements:

  • Create two constructors:

    • A default constructor.

    • A parameterized constructor (including inherited attributes and specialRate).

  • Create getters and setters for specialRate.

Override and overload methods:

  • Override calculatePrice()
    Calculate the total price as:
    totalPrice = rootPrice + tax + (rootPrice * specialRate)

  • Overload calculatePrice(float transportCost)
    Add one parameter named transportCost (type float), and calculate:
    totalPrice = rootPrice + tax + (rootPrice * specialRate) + transportCost

4. Create a Test class

In the main() method:

  1. Declare and initialize an instance of LuxuryCar named myLuxuryCar.

  2. Input data from the keyboard:
    Read values for name, producer, year, seat, and rootPrice.

    • Use exception handling (try-catch) to catch invalid inputs.

  3. Display the car’s information using the getInfo() method.

  4. Calculate the total price when the transportCost is $20,000 using the overloaded calculatePrice(float transportCost) method.

  5. Display the final total price.

Summary:
This exercise helps you practice:

  • Defining and implementing interfaces in Java.

  • Using inheritance and method overriding/overloading.

  • Working with constructors, encapsulation (private fields, getters/setters).

  • Handling user input and exceptions.

  • Calculating and displaying object data clearly and logically.



Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó

×