Bài tập Java OOP: tạo lớp Time để biểu diễn thời gian theo yêu cầu | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create Time class in Time.java file with the following information:

Explanation:

  • Time class stores the information about hours, minutes and seconds.
  • hour, minute, second are the attributes that describe the hours, minutes and seconds of Time class.
  • Time(hour: int, minute: int, second: int) constructor parameter is used to initialize values to attributes.
  • setTime(hour: int, minute: int, second: int) method changes the values of the following attributes hour, minute, second.
  • nextSecond() method increases the time of objective by 1 second. For instance:
    • Values of attributes after and before calling nextSecond() method: hour = 7, minute = 7, second = 40 and hour = 7, minute = 7, second = 41.
    • Values of attributes after and before calling nextSecond() method: hour = 23, minute = 59, second = 59 and hour = 0, minute = 0, second = 0.
    • Values of attributes after and before calling nextSecond() method: hour = 7, minute = 59, second = 59 and hour = 8, minute = 0, second = 0.
  • previousSecond() method decreases the time of object by 1 second. For instance:
    • Values of attributes after and before calling previousSecond() method: hour = 7, minute = 7, second = 7 and hour = 7, minute = 7, second = 6.
    • Values of attributes after and before calling previousSecond() method: hour = 0, minute = 0, second = 0 and hour = 23, minute = 59, second = 59.
  • display() method displays the time information of objective in the format "HH:mm:ss". For example:
    • For hour = 1, minute = 8, second = 45, after calling display() method, the text "01:08:45" will display on the screen.
    • For hour = 23, minute = 5, second = 7, after calling display() method, the text "23:05:07" will display on the screen.
  • The other methods are setter and getter.

A program to test Time class:

class Entry {
	public static void main(String[] args) {
		Time t = new Time(12, 1, 1);
		t.display();
		t.setTime(23, 59, 59);
		t.display();
		t.nextSecond();
		t.display();
		t.nextSecond();
		t.display();
		t.setTime(7, 0, 0);
		t.display();
		t.previousSecond();
		t.display();
	}
}

When the above code is compiled and executed, it produces the following result:

12:01:01
23:59:59
00:00:00
00:00:01
07:00:00
06:59:59

Instruction

You have to create a Time class as below:

class Time {
	private int hour;
	private int minute;
	private int second;

	public Time(int hour, int minute, int second) {
		this.hour = hour;
		this.minute = minute;
		this.second = second;
	}

	public void setTime(int hour, int minute, int second) {
		this.hour = hour;
		this.minute = minute;
		this.second = second;
	}

	public int getHour() {
		return hour;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public int getMinute() {
		return minute;
	}

	public void setMinute(int minute) {
		this.minute = minute;
	}

	public int getSecond() {
		return second;
	}

	public void setSecond(int second) {
		this.second = second;
	}

	public void previousSecond() {
		if (hour == 0 && minute == 0 && second == 0) {
			hour = 23;
			minute = 59;
			second = 59;
			return;
		}
		if (minute == 0 && second == 0) {
			hour--;
			minute = 59;
			second = 59;
			return;
		}
		if (second == 0) {
			minute--;
			second = 59;
			return;
		}
		second--;
	}

	public void nextSecond() {
		if (hour == 23 && minute == 59 && second == 59) {
			hour = 0;
			minute = 0;
			second = 0;
			return;
		}
		if (minute == 59 && second == 59) {
			hour++;
			minute = 0;
			second = 0;
			return;
		}
		if (second == 59) {
			minute++;
			second = 0;
			return;
		}
		second++;
	}

	void display() {
		String hour = this.hour + "";
		String minute = this.minute + "";
		String second = this.second + "";
		if (hour.length() == 1)
			hour = "0" + hour;
		if (minute.length() == 1)
			minute = "0" + minute;
		if (second.length() == 1)
			second = "0" + second;
		System.out.println(hour + ":" + minute + ":" + second);
	}
}