I am trying to solve an exercise in Java and I have come up blank:
Implement the play method, which will play the media object seconds. The implementation of this method will only write to the console the sequence of elapsed seconds until it completes, based on the duration. For example, if it lasts 70 seconds (1 minute 10 seconds), it would write: 00:00:01—00:00:02—00:00:03—00:00:04 --...-00:00:59 —00:01:00 ... 00:01:10 [End]
static void reproducir(int duracion) {
int horas = 00;
int minutos = 00;
int segundos = 00;
final int N = 59;
int i;
for (i = 0; i <= duracion; i++) {
if (segundos == N) {
segundos = 0;
minutos++;
}
if (minutos == N) {
minutos = 0;
horas++;
}
segundos++;
System.out.printf("%02d:%02d:%02d - ", horas, minutos, segundos);
}
System.out.print("Fin de la reproducción");
}
Method working after review.
I understand that it is a simulation, it does not really count the passage of time.
What you need is to calculate minutes and hours depending on the time that has passed. This would be your function:
Seconds are from 0 to 59, so
segundos = totalSegundos % 60
.Every 60 seconds there is a minute, so
minutos = totalSegundos / 60
. Also, minutes are from 0 to 59, so we do modulo 60 with respect to the minutes that come out.Every 3600 seconds (60 * 60) there is an hour, so
horas = totalSegundos / 3600