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

如何创建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 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发