首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 可以帮我做一下这道题吗? [已结贴,结贴人:gylxing]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-10 12:37:13 楼主
    根据如下描述编写一个描述时间的类MyTime。
    (1)包含三个数据哉:小时(hour),分钟(minute)和秒(second)。注意进行合理封装。
    (2)包含一个无参构造方法(3个数据均为0)和一个有3个形参的构造方法。
    (3)包含一个将当前时间前进一秒的方法。
    (4)包含一个计算当前时间对象与形参传入时间对象之间隔秒数的方法。
    (5)包含一个以“HH-MM-SS”形式输出当前时间的方法。
    根据以上描述编写类MyTime的源程序,并编写测试类TestMytime测试时间类。
    请各位指点啦!!!
    20  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-10 12:44:161楼 得分:0
    建议你先自己尝试写一下,遇到问题再来问,效果会比较好
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-10 13:04:222楼 得分:0
    应该不难啊,大一的题目
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-10 13:09:463楼 得分:0
    引用 2 楼 ErnestLu 的回复:
    应该不难啊,大一的题目

    晕死....
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-10 13:32:384楼 得分:7
    Java code
    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(); } } }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-10 13:34:135楼 得分:7
    仅供参考,作业还是自己做比较好。呵呵。
    Java code
    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()); } }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-10 15:57:516楼 得分:6
    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();
            }
        }
    }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-04-12 01:23:427楼 得分:0
    谢谢各位的帮助!!!!
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved