永发信息网

求解Java作业 急

答案:2  悬赏:80  手机版
解决时间 2021-07-16 13:30
Question 2.
We wish to model a digital clock, which records the time as 0:0:0 to 23:59:59. The clock,
which will be modelled by class DigitalClock, should have the following attributes:
hours (0 to 23)
minutes (0 to 59)
seconds (0 to 59)
which should all be declared as private, and the following methods:
DigitalClock() - creates a object and sets the time to 0:0:0
void setHours( int hrs ) – if hrs is not a valid value, sets the hours to 0
void setMinutes( int min ) – similar
void setSeconds( int sec ) – similar
void tick() – advances the clock one second
void displayTime( ) – to display the time.
String toString () – to display the time
Your jobs:
(1). Create the class DigitalClock.
(2). Create another class to test it. Design your test carefully to cover as many situations
as possible. Suggested testing:
a) set your clock to 23:59:50.
b) write a loop that calls the tick method 10 or more times printing the
current time after each tick. Check that the clock changes at midnight to
00:00:00
Note that we can’t really simulate a clock unless we use the Timer class to call the tick
method at intervals of one second – this is well beyond our current stage of learning.
最佳答案

DigitalClock 类


public class DigitalClock {

private int hours ;

private int minutes ;

private int seconds ;



public DigitalClock()//构造函数
{
this.hours=0;//初始化小时为0
this.minutes=0;//初始化分钟为0
this.seconds=0;//初始化秒针为0
}


// public int getHours() {
// return hours;
//}

public void setHours(int hours) {//判断小时大于等于0切小于等于23进行赋值否则设置为0
if(hours>=0&&hours<=23)
{
this.hours = hours;
}
else
{
this.hours =0;
}

}


// public int getMinutes() {
// return minutes;
// }


public void setMinutes(int minutes) {//判断分大于等于0切小于等于59进行赋值否则设置为0
if(minutes>=0&&minutes<=59)
{
this.minutes = minutes;
}
else
{
this.minutes =0;
}

}


// public int getSeconds() {
// return seconds;
// }


public void setSeconds(int seconds) {//判断秒大于等于0切小于等于59进行赋值否则设置为0
if(seconds>=0&&seconds<=59)
{
this.seconds = seconds;
}
else
{
this.seconds = 0;
}

}


public void tick()//让时间增加
{
this.seconds++;

if(this.seconds<=59)//当秒小于等于59秒是直接设置
{
this.setSeconds(this.seconds);
}
else//否则 分加一
{
this.minutes++;
this.setSeconds(this.seconds);
if(this.minutes<=59)//同上一样
{
this.setMinutes(this.minutes);
}
else//否则时 加一
{

this.hours++;
this.setMinutes(this.minutes);
this.setHours(this.hours);
}
}


}

public void displayTime()//没有返回类型的显示时间方法
{
System.out.println(""+this.hours+"-"+this.minutes+"-"+this.seconds);
}


public String toString()//返回字符串类型的方法
{
return ""+this.hours+"-"+this.minutes+"-"+this.seconds;
}
}



测试类


public class ceshi {



public static void main(String[] args) {
// TODO Auto-generated method stub

DigitalClock dis = new DigitalClock();
dis.setHours(23);
dis.setMinutes(59);
dis.setSeconds(50);

System.out.println("对时间为:23-59-50进行十次秒进结果是:");
for(int i=0;i<10;i++)
{
dis.tick();//推动时间增加
dis.displayTime();
}


}


}


测试结果


全部回答

public DigitalClock{ private hours=0; private minutes=0; private seconds=0; public void setHours(int hrs){

this.hours=hrs; } public void setMinutes(int min){

this.minutes=min;

} public void setSeconds(int sec){

this.seconds=sec; } public void tick() { seconds++; if(60==seconds){ seconds=0; minutes++; if(60==minutes){ minutes=0; hours++; if(60==hours){ hours=0;

}

} } } public void displayTime(){ System.out.println(toString(seconds)+toString(minutes)+toString(hours)); }

public String toString (int i)

return i+""; }

public static void main(string[] s){ DigitalClock dc=new DigitalClock(); dc.setHours(23); dc.setMinutes(59) dc.setSeconds(50);

while(1=1) { dc.tick(); dc.displayTime(); }

}

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
我把一个好友删了,是先拉黑再从黑名单里删掉
一年之计在于春,(   )。(  ),海阔
关于诚信班会主持稿
请说出《封神演义》中姜子牙的封地在今天的哪
姚明这赛季还有复出希望没?
腾讯控股的股盘有多大?
为什么吃过药后病情反而加重了?
2xmy与5x的平方乘m-3y的同类项,求9m-28的101
数码相机只拍了几十张照片,然后放了两天没有
【急】求适合女孩子配音的动画电影片段
dnf为什么总掉线?A
分析公车改革的政治意义
中国全有什么网球大赛?
问道80级仙魔路要请人杀几次?做完后得什么?
如果QQ好友,当时没分组,现在该怎么分啊?
推荐资讯
两个月的婴儿可以使用强生爽身份吗?
上个礼拜《篮球公园》放詹姆斯在纽约时的那段
什么样的单位是求职的第一选择?这个问题要怎
吃生鸡蛋有什么好处~除细菌多还有什么坏处?
急需2份绿宝石386通关记录!!!!!!!!!
为什么从新下载了FLASH最新版,优化大师也装
带着耳机睡觉会不会对耳朵有影响?
一道初一解方程数学题!!
农历90年12月5号,是阳历什么时间!什么星座
谁能点丝路英雄15QB
DNF阿德拉战绩就26集吗?还有没有
我这是甲流症状吗?
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?