根据如下描述编写一个描述时间的类MyTime。
(1)包含三个数据哉:小时(hour),分钟(minute)和秒(second)。注意进行合理封装。
(2)包含一个无参构造方法(3个数据均为0)和一个有3个形参的构造方法。
(3)包含一个将当前时间前进一秒的方法。
(4)包含一个计算当前时间对象与形参传入时间对象之间隔秒数的方法。
(5)包含一个以“HH-MM-SS”形式输出当前时间的方法。
根据以上描述编写类MyTime的源程序,并编写测试类TestMytime测试时间类。
请各位指点啦!!!

解决方案 »

  1.   

    public final class MyTime {
    private int hour;
    private int minute;
    private int second; public MyTime() {
    this.hour = 0;
    this.minute = 0;
    this.second = 0;
    } public MyTime(int hr, int mt, int se) throws Exception {
    this.setHour(hr);
    this.setMinute(mt);
    this.setSecond(se);
    } public void goPerSecond() {
    this.second++;
    if (this.second == 60) {
    this.second = 0;
    this.minute++;
    if (this.minute == 60) {
    this.minute = 0;
    this.hour++;
    if (this.hour == 24) {
    this.hour = 0;
    }
    }
    }
    } public int secondsBetween(MyTime atime) {
    return this.getSeconds(this) - this.getSeconds(atime);
    } public String toString() {
    StringBuffer retVal = new StringBuffer();
    String tmpStr = Integer.toString(this.hour);
    if (tmpStr.length() == 1) {
    retVal.append("0");
    }
    retVal.append(tmpStr);
    retVal.append("-");
    tmpStr = Integer.toString(this.minute);
    if (tmpStr.length() == 1) {
    retVal.append("0");
    }
    retVal.append(tmpStr);
    retVal.append("-");
    tmpStr = Integer.toString(this.second);
    if (tmpStr.length() == 1) {
    retVal.append("0");
    }
    retVal.append(tmpStr);
    return retVal.toString();
    } public int getHour() {
    return this.hour;
    } public void setHour(int hour) throws Exception {
    if (hour < 0 || hour > 23) {
    throw new Exception("hour < 0 || hour > 23");
    }
    this.hour = hour;
    } public int getMinute() {
    return this.minute;
    } public void setMinute(int minute) throws Exception {
    if (minute < 0 || minute > 59) {
    throw new Exception("minute < 0 || minute > 59");
    }
    this.minute = minute;
    } public int getSecond() {
    return this.second;
    } public void setSecond(int second) throws Exception {
    if (second < 0 || second > 59) {
    throw new Exception("second < 0 || second > 59");
    }
    this.second = second;
    } private int getSeconds(MyTime atime) {
    return atime.hour * 3600 + atime.minute * 60 + atime.second;
    }
    }public final class TestMytime {
    public static void main(String[] args) {
    try {
    MyTime myTime = new MyTime();
    myTime.setHour(12);
    myTime.setMinute(0);
    myTime.setSecond(0);
    System.out.println(myTime.toString());
    MyTime newTime = new MyTime(13, 59, 59);
    System.out.println(newTime.toString());
    newTime.goPerSecond();
    System.out.println(newTime.toString());
    System.out.println(newTime.secondsBetween(myTime));
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  2.   

    仅供参考,作业还是自己做比较好。呵呵。
    import java.util.Calendar;
    public class MyTime {

    private int hour, minute, second;
    private static final char SEPARATOR = '-';

    public MyTime() { }

    public MyTime(int h, int m, int s) {
    hour = h;
    minute = m;
    second = s;
    if (hour > 23 || minute > 59 || second > 59) adjust();
    }

    public void forwardOneSecond() {
    second++;
    if (second > 59) adjust();
    }

    private void adjust() {  // 如果时间格式不正确,做相应调整
    minute += second / 60;
    second %= 60;
    hour += minute / 60;
    minute %= 60;
    hour %= 24;
    }

    public long offsetByNow() {
    Calendar myTime = Calendar.getInstance();
    Calendar now = Calendar.getInstance();
    myTime.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE), hour, minute, second);
    return (myTime.getTimeInMillis() - now.getTimeInMillis()) / 1000;
    }

    public String toString() {
    return (hour<10?"0":"") + hour + SEPARATOR
    + (minute<10?"0":"") + minute + SEPARATOR
    + (second<10?"0":"") + second;
    } // 测试程序
    public static void main(String[] args) {
    MyTime myTime = new MyTime(13, 30, 8);
    System.out.println(myTime);
    myTime.forwardOneSecond();
    System.out.println("前进一秒后:" + myTime);
    System.out.println("与当前时间之差(秒):" + myTime.offsetByNow());
    }}
      

  3.   

    public final class MyTime {
        private int hour;
        private int minute;
        private int second;    public MyTime() {
            this.hour = 0;
            this.minute = 0;
            this.second = 0;
        }    public MyTime(int hr, int mt, int se) throws Exception {
            this.setHour(hr);
            this.setMinute(mt);
            this.setSecond(se);
        }    public void goPerSecond() {
            this.second++;
            if (this.second == 60) {
                this.second = 0;
                this.minute++;
                if (this.minute == 60) {
                    this.minute = 0;
                    this.hour++;
                    if (this.hour == 24) {
                        this.hour = 0;
                    }
                }
            }
        }    public int secondsBetween(MyTime atime) {
            return this.getSeconds(this) - this.getSeconds(atime);
        }    public String toString() {
            StringBuffer retVal = new StringBuffer();
            String tmpStr = Integer.toString(this.hour);
            if (tmpStr.length() == 1) {
                retVal.append("0");
            }
            retVal.append(tmpStr);
            retVal.append("-");
            tmpStr = Integer.toString(this.minute);
            if (tmpStr.length() == 1) {
                retVal.append("0");
            }
            retVal.append(tmpStr);
            retVal.append("-");
            tmpStr = Integer.toString(this.second);
            if (tmpStr.length() == 1) {
                retVal.append("0");
            }
            retVal.append(tmpStr);
            return retVal.toString();
        }    public int getHour() {
            return this.hour;
        }    public void setHour(int hour) throws Exception {
            if (hour < 0 || hour > 23) {
                throw new Exception("hour < 0 || hour > 23");
            }
            this.hour = hour;
        }    public int getMinute() {
            return this.minute;
        }    public void setMinute(int minute) throws Exception {
            if (minute < 0 || minute > 59) {
                throw new Exception("minute < 0 || minute > 59");
            }
            this.minute = minute;
        }    public int getSecond() {
            return this.second;
        }    public void setSecond(int second) throws Exception {
            if (second < 0 || second > 59) {
                throw new Exception("second < 0 || second > 59");
            }
            this.second = second;
        }    private int getSeconds(MyTime atime) {
            return atime.hour * 3600 + atime.minute * 60 + atime.second;
        }
    }public final class TestMytime {
        public static void main(String[] args) {
            try {
                MyTime myTime = new MyTime();
                myTime.setHour(12);
                myTime.setMinute(0);
                myTime.setSecond(0);
                System.out.println(myTime.toString());
                MyTime newTime = new MyTime(13, 59, 59);
                System.out.println(newTime.toString());
                newTime.goPerSecond();
                System.out.println(newTime.toString());
                System.out.println(newTime.secondsBetween(myTime));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }