xinyu_Tiffany 2014-07-21 16:38 采纳率: 0%
浏览 2506

如何创建Time class,求解一道题

(The Time class) Design a class named Time. The class contains:
■ Data fields hour, minute, and second that represent a time.
■ A no-arg constructor that creates a Time object for the current time. (The values
of the data fields will represent the current time.)
■ A constructor that constructs a Time object with a specified elapsed time since
midnight, Jan 1, 1970, in milliseconds. (The values of the data fields will represent
this time.)
■ A constructor that constructs a Time object with the specified hour, minute, and
second.
■ Three get methods for the data fields hour, minute, and second, respectively.
■ A method named setTime(long elapseTime) that sets a new time for the
object using the elapsed time.
Implement the class. Write a test program
that creates two Time objects (using new Time() and new Time(555550000))
and display their hour, minute, and second.
(Hint: The first two constructors will extract hour, minute, and second from the
elapsed time. For example, if the elapsed time is 555550 seconds, the hour is 10,
the minute is 19, and the second is 9. For the no-arg constructor, the current time
can be obtained using System.currentTimeMills(), as shown in Listing 2.6,
ShowCurrentTime.java.)

  • 写回答

1条回答 默认 最新

  • 宰猪好手 2018-05-08 09:14
    关注

    weilin

    以下是解答
    import java.util.Date;
    import java.util.Calendar;
    public class Time {

    long hour,minute,second;
    Date date;
    Calendar calendar=Calendar.getInstance();
    public Time(){
        this.date=new Date();   
    }
    public Time(long milliseconds){
        this.date=new Date(milliseconds);   
    }
    public Time(int hour,int minute,int second){
        long sum=0;
        sum=sum+hour*60*60*1000+minute*60*1000+second*1000;
        this.date=new Date(sum);
    }
    public long getHour() {
        hour=date.getTime()/3600000;
        return hour;
    }
    public long getMinute(){
         minute = (date.getTime() % 3600000) / 60000;
    return minute;
    
    }
    public long getSecond() {
         second= (date.getTime() % 3600000) % 60000/1000;
        return second;  
    }
    public void setTime(long elapseTime) {
        Date newdate=new Date(elapseTime);
        date=newdate;   
    }   
    

    }

    测试

    public class Test {
    public static void main(String args[]) {
    Time time1=new Time();
    Time time2=new Time(555550000);

        //System.out.println(time1.date);
        //System.out.println(time2.date);
        System.out.println("new Time()时分秒为:"+time1.getHour()+"时"+time1.getMinute()+"分"+time1.getSecond()+"秒");
        System.out.println("new Time(555550000)时分秒为:"+time2.getHour()+"时"+time2.getMinute()+"分"+time2.getSecond()+"秒");
    }   
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog