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 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑