Cài đặt lớp time định nghĩa toán tử năm 2024

CodeLearn is an online platform that helps users to learn, practice coding skills and join the online coding contests.

Links

Learning

Training

Fights

Information

About Us

Terms of Use

Help

Help

Discussion

Powered by CodeLearn © 2024. All Rights Reserved. rev 2/24/2024 9:44:06 AM

Đề bài: Viết chương trình sử dụng ngôn ngữ lập trình Java tạo lớp Time biểu diễn thời gian gồm 3 thuộc tính giây, phút, giờ (second, minute, hour) và các phương thức: Khởi tạo, lấy và thiết lập giá trị cho các thuộc tính, tăng giờ, phút, giây lên 1 đơn vị. Tại hàm main, tạo đối tượng lớp Time có giá trị giờ, phút, giây là 7, 0, 0 sau đó dùng vòng lặp while vô tận để biểu diễn đồng hồ. Yêu cầu kiến thức:

  • Xác định đối tượng chương trình từ đối tượng thực tế
  • Phân chia các thành phần khi xây dựng lớp
  • Xác định kiểu dữ liệu đúng cho các biến và hàm

Cấu trúc thư mục: src |——BuildClass |——Time.java |——UseClass |——MainUse.java Code tham khảo dưới đây được viết trên JDK ver 8.x: File Time.java:

  
package BuildClass;
public class Time {  
    // Thuoc tinh  
    private int second; // tu 0 den 59  
    private int minute; // tu 0 den 59  
    private int hour; // tu 0 den 23
    // Phuong thuc  
    // Ham khoi tao khong doi so  
    public Time() {  
        this.second = 0;  
        this.minute = 0;  
        this.hour = 0;  
    }
    // Ham khoi tao co doi so  
    public Time(int second, int minute, int hour) {  
        this.second = second;  
        this.minute = minute;  
        this.hour = hour;  
    }
    // Ham getter va setter cho cac thuoc tinh  
    public int getSecond() {  
        return second;  
    }
    public void setSecond(int second) {  
        // Neu secord > 60 thi reset second = 0  
        if (second >= 0 && second < 60) {  
            this.second = second;  
        } else {  
            this.second = 0;  
        }  
    }
    public int getMinute() {  
        return minute;  
    }
    public void setMinute(int minute) {  
        // Neu minute > 60 thi reset minute = 0  
        if (minute >= 0 && minute < 60) {  
            this.minute = minute;  
        } else {  
            this.minute = 0;  
        }  
    }
    public int getHour() {  
        return hour;  
    }
    public void setHour(int hour) {  
        // Neu hour > 23 thi reset hour =0  
        if (hour >= 0 && hour < 24) {  
            this.hour = hour;  
        } else {  
            this.hour = 0;  
        }  
    }
    // Hien thi thoi gian  
    @Override  
    public String toString() {  
        String str = "";  
        if (hour == 12 || hour == 0) {  
            str += 12;  
        } else {  
            str += (hour % 12);  
        }  
        str += ":";  
        if (minute < 10) {  
            str += "0";  
        } else {  
            str += "";  
        }  
        str += minute + ":";  
        if (second < 10) {  
            str += "0";  
        } else {  
            str += "";  
        }  
        str += second;  
        if (hour < 12) {  
            str += " AM";  
        } else {  
            str += " PM";  
        }  
        return str;  
    }
    // Tang giay len 1 don vi  
    public void tangGiay() {  
        this.setSecond(this.second + 1);  
        // Neu giay tang len 60 thi giay reset ve 0, va tang phut len 1  
        if (this.second == 0) {  
            tangPhut();  
        }  
    }
    // Tang phut len 1 don vi  
    public void tangPhut() {  
        this.setMinute(this.minute + 1);  
        // Neu phut tang len 60 thi phut reset ve 0, va tang gio len 1  
        if (this.minute == 0) {  
            tangGio();  
        }  
    }
    // Tang gio len 1 don vi, neu gio len 24 thi gio reset ve 0  
    public void tangGio() {  
        this.setHour(this.hour + 1);  
    }  
}

File MainUse.java:

  
package UseClass;
import BuildClass.Time;
public class MainUse {  
    public static void main(String[] args) {  
        // Khai bao doi tuong  
        Time O = new Time(0, 0, 7);  
        System.out.println("Thoi gian da thiet lap: " + O.toString());  
        while (true) {  
            // Hien thi thoi gian hien tai  
            System.out.println(O.toString());  
            try {  
                Thread.sleep(1000);  
                O.tangGiay();  
            } catch (Exception ex) {
            }  
        }  
    }  
}

Kết luận:

  • Bạn có thể tham khảo thêm khóa học lập trình C từ cơ bản đến nâng cao. Xem tại đây
  • Bạn có thể tham khảo thêm khóa học Thành thạo lập trình C#. Xem tại đây