java程序设计 定义一个时钟类,要求如下1,创建新对象时默认0时0分0秒。 2,设置时钟为指定的时间。3,编写 5
使时钟前进1秒的方法incsecond()4,以"时:分:秒:"的形式输出时钟的时间5.存储时钟的时HOUR(0~23)、分minute(0~59)、秒second(0~...
使时钟前进1秒的方法incsecond() 4,以"时:分:秒:"的形式输出时钟的时间 5.存储时钟的时HOUR(0~23)、分minute(0~59)、秒second(0~59)
展开
1个回答
展开全部
import java.util.Calendar;
public class Clock {
private int hour, mintue, second;
public Clock(){
this(0, 0, 0);
}
public Clock(int hour, int minute, int second){
this.hour = hour;
this.mintue = minute;
this.second = second;
}
public void incsecond(){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, hour);
cal.set(Calendar.MINUTE, mintue);
cal.set(Calendar.SECOND, second);
cal.add(Calendar.SECOND, 1);
this.hour = cal.get(Calendar.HOUR);
this.mintue = cal.get(Calendar.MINUTE);
this.second = cal.get(Calendar.SECOND);
}
public String toString(){
return hour + ":" +mintue + ":" + second;
}
public void setHour(int hour) {
this.hour = hour;
}
public void setMintue(int mintue) {
this.mintue = mintue;
}
public void setSecond(int second) {
this.second = second;
}
public static void main(String[] args) {
}
}
public class Clock {
private int hour, mintue, second;
public Clock(){
this(0, 0, 0);
}
public Clock(int hour, int minute, int second){
this.hour = hour;
this.mintue = minute;
this.second = second;
}
public void incsecond(){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, hour);
cal.set(Calendar.MINUTE, mintue);
cal.set(Calendar.SECOND, second);
cal.add(Calendar.SECOND, 1);
this.hour = cal.get(Calendar.HOUR);
this.mintue = cal.get(Calendar.MINUTE);
this.second = cal.get(Calendar.SECOND);
}
public String toString(){
return hour + ":" +mintue + ":" + second;
}
public void setHour(int hour) {
this.hour = hour;
}
public void setMintue(int mintue) {
this.mintue = mintue;
}
public void setSecond(int second) {
this.second = second;
}
public static void main(String[] args) {
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询