class MyTime{
private int day;
private int hour;
private int minute;
public MyTime(){
this.hour=this.minute=this.minute=0;
}
public MyTime(int hour, int minute) {
this.hour = hour;
this.minute = minute;
}
//能计算两个时间的和
public MyTime timeAdd(MyTime s) {
MyTime result = new MyTime();
int d,h,m;
m = s.minute + this.minute;
h = s.hour + this.hour + m / 60;
d = s.day + this.day + h / 24;
result.day = d;
result.hour = h %24;
result.minute = m % 60;
return result;
}
//比较两个时间是否相同
public boolean eguals(MyTime s){
return this.day==s.day&&this.hour==s.hour&&this.minute==s.minute;
}
public String toCNString() {//按中文形式转化时间
if(day>0)
return day+"天"+hour+"小时"+minute+"分钟";
else
return hour+"小时"+minute+"分钟";
}
@Override
public String toString() {
if(day>0)
return "+" + day+"天 "+hour+":"+minute;
else
return hour+":"+minute;
}
}
public class Solution {
public static void main(String[]args){
/*
在main方法中创建至少三组起始时间和运行时间,计算到达时间。
输出效果例如:
起始时间 运行时间 到达时间
20:12 35小时10分钟 +2天07:22
*/
MyTime m1, m2,m3;
m1 = new MyTime(20,12);
m2 = new MyTime(35, 10);
m3 = m1.timeAdd(m2);
System.out.println("起始时间\t运行时间\t到达时间");
System.out.println(m1+" "+m2.toCNString()+" "+m3);
m1 = new MyTime(20,12);
m2 = new MyTime(50, 10);
m3 = m1.timeAdd(m2);
System.out.println("起始时间\t运行时间\t到达时间");
System.out.println(m1+" "+m2.toCNString()+" "+m3);
m1 = new MyTime(20,12);
m2 = new MyTime(2, 10);
m3 = m1.timeAdd(m2);
System.out.println("起始时间\t运行时间\t到达时间");
System.out.println(m1+" "+m2.toCNString()+" "+m3);
}
}