Si scusami, errore mio.
class progT {
public static void main (String[] args) {
T t1,t2;
t1=new T();
t2=new T(12,30,10);
System.out.println("t1="+t1+"-"+"t2="+t2);
t1.setT(23,63,59);
System.out.println("t1="+t1+"-"+"t2="+t2);
t1.setT(19,21,40);
System.out.println("t1="+t1+"-"+"t2="+t2);
t1.addT(t2);
System.out.println("t1="+t1+"-"+"t2="+t2);
t2.subT(t1);
System.out.println("t1="+t1+"-"+"t2="+t2);
} //fine main
} //fine classe progT
class T {
private int ore;
private int min;
private int sec;
public T() {this(0,0,0);}
public T(int o, int m, int s) {
if (isT(o,m,s)) {ore = o; min = m;sec = s;}
else { System.out.println("Errore");
ore = 0; min = 0; sec = 0;
}
}
public boolean isT(int o, int m, int s) {
return (o>=0 && o<=24 && m>=0 && m<=60 && s>=0 && s<=60);
}
public void setT(int o, int m, int s) {
if (isT(o,m,s)) {ore = o;min = m;sec = s;}
else System.out.println("Errore");
}
public void addT(T t) {
sec=sec+t.getSec();
if(sec > 59){sec=sec-60;min++;}
min=min+t.getMin();
if(min > 59){min=min-60;ore++;}
ore=ore+t.getOre();
if(ore > 23)ore=ore-24;
}
public void subT(T t) {
if(sec < t.getSec()){min--;sec=sec+60-getSec();}
else sec=sec-t.getSec();
if(min < t.getMin()){ore--;min=min+60-getMin();}
else min=min-t.getMin();
ore=Math.abs(ore-t.getOre());
}
Questa impostazione può essere giusta?