Ejercicio Java
import java.util.*;
class tiempo {
static public void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Deberá ingresar dos instantes de tiempo en elformato HHMMSS");
int instante1;
int instante2;
int mayor;
int menor;
int suma;
int resta;
inthr;
int min;
int seg;
System.out.println("Ingrese un instante con el formato HHMMSS:");
System.out.println("Ingrese el instante 1:");
instante1 =s.nextInt();
System.out.println("Ingrese el instante 2:");
instante2 = s.nextInt();
mayor = Math.max(instante1, instante2);
System.out.println("Mayor: " + f(mayor));
menor =Math.min(instante1, instante2);
System.out.println("Menor: " + f(menor));
suma = totalsegundos(instante1) + totalsegundos(instante2);
hr =(suma/3600);
min = (suma % 3600)/60;
seg = ((suma %3600) % 60);
suma = hr*10000 + min*100 + seg;
System.out.println("Suma: " + f(suma));
resta = totalsegundos(mayor) - totalsegundos(menor);
hr =(resta/3600);
min = (resta %3600)/60;
seg = ((resta % 3600) % 60);
resta = hr*10000 + min*100 + seg;
System.out.println("Resta: " + f(resta));
}
public static int horas(int x) {
return x/10000;
}
publicstatic int minutos(int x) {
int y = x-horas(x)*10000;
return y/100;
}
public static int segundos(int x) {
int y = x-horas(x)*10000 - minutos(x)*100;
return y;
}
public staticint totalsegundos(int x) {
return 3600*horas(x) + 60*minutos(x)+ segundos(x);
}
static public String f(int x){
String horas=horas(x) < 10 ? "0" + tiempo.horas(x) : "" + horas(x);
Stringminutos=minutos(x) < 10 ? "0" + minutos(x) : "" + minutos(x);
String segundos=segundos(x) < 10 ? "0" + segundos(x) : "" + segundos(x);
return horas + ":" + minutos + ":" + segundos;
}
}
Regístrate para leer el documento completo.