第十章第一题(Time类)(Time class)

第十章第一题(Time类)(Time class)

  • *10.1(Time类)设计一个名为Time的类。这个类包含:

    • 表示时间的数据域hour、minute和second。
    • 一个以当前时间创建Time对象的无参构造方法(数据域的值表示当前时间)。
    • 一个构造Time对象的构造方法,,以一个指定的流逝时间值来构造Time对象,这个值是从1970年1月1日午夜开始到现在流逝的以毫秒表示的值(数据域的值表示这个时间)。
    • 以指定的小时、分钟和秒来构造Time对象的构造方法。
    • 三个数据域hour、minute和second各自的获取方法。
    • 一个名为setTime(long elapseTime)的方法使用流逝的时间给对象设置一个新时间。例如,如果流逝的时间为555550000毫秒,则转换为10小时、10分钟、10秒。

    画出该类的UML图并实现这个类。编写一个测试程序,创建两个Time对象(使用newTime()和new Time(55550000)),然后显示他们的小时、分钟和秒。
    提示:前两个构造方法可以从流逝的时间中提取出小时、分钟和秒。对于无参构造方法,可以使用System.currentTimeMills()获取当前时间,如程序清单2-7所示。假设时间使用GMT。
    *10.1(Time class)Design a class called time. This class contains:

    • The data fields for time are hour, minute, and second.
    • A nonparametric construction method that creates a time object at the current time (the value of the data field represents the current time).
    • A method for constructing a time object. The time object is constructed with a specified elapsed time value. This value is the value expressed in milliseconds since midnight on January 1, 1970 (the value in the data field represents the time).
    • Constructs the constructor of the time object in the specified hours, minutes, and seconds.
    • The three data fields hour, minute and second are obtained respectively.
    • A method called setTime (long elapsetime) uses the elapsed time to set a new time for the object. For example, if the elapsed time is 55555000 milliseconds, it is converted to 10 hours, 10 minutes, 10 seconds.

    Draw the UML diagram of the class and implement the class. Write a test program, create two time objects (using newtime() and new time (5550000)), and then display their hours, minutes and seconds.
    Tip: the first two construction methods can extract hours, minutes and seconds from the elapsed time. For nonparametric construction methods, you can use the System.currentTimeMills () gets the current time, as shown in listing 2-7. Suppose time uses GMT.

  • 参考代码:

package chapter10;

public class Code_01 {
    
    
    public static void main(String[] args) {
    
    
        Time t1 = new Time();
        System.out.println(t1.getHour() + " " + t1.getMinute() + " " + t1.getSecond());
        Time t2 = new Time(555550000);
        System.out.println(t2.getHour() + " " + t2.getMinute() + " " + t2.getSecond());
    }
}
class Time{
    
    
    private long second;
    private long minute;
    private long hour;

    public Time(){
    
    
        long t = System.currentTimeMillis()/1000;
        second = t % 60;
        t /= 60;
        minute = t % 60;
        t /= 60;
        hour = t % 24;
    }

    public Time(long t){
    
    
        t /= 1000;
        second = t % 60;
        t /= 60;
        minute = t % 60;
        t /= 60;
        hour = t;
    }

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

    public long getSecond(){
    
    
        return second;
    }

    public long getMinute(){
    
    
        return minute;
    }

    public long getHour(){
    
    
        return hour;
    }

    public void setTime(long elapseTime){
    
    
        elapseTime /= 1000;
        this.second = elapseTime%60;
        elapseTime /= 60;
        this.minute = elapseTime%60;
        elapseTime /= 60;
        this.hour = elapseTime;
    }
}

  • 结果显示:
9 42 46
154 19 10

Process finished with exit code 0

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/jxh1025_/article/details/109294713