求解Java作业 急
- 提问者网友:戎马万世
- 2021-07-16 06:16
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.
- 五星知识达人网友:时间的尘埃
- 2021-07-16 07:43
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();
}
}
}
测试结果
- 1楼网友:末日狂欢
- 2021-07-16 07:50
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(); }
}