Created
October 27, 2020 11:40
-
-
Save srifqi/93fd70b0b23e6f304a066a9860e6d9a2 to your computer and use it in GitHub Desktop.
Program untuk menghitung lama/durasi antara dua waktu dalam satu hari
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// srifqi, MIT License | |
import java.util.Scanner; | |
public class DurasiSehari { | |
public static void main(String[] args) { | |
System.out.println("Program Penghitung Lama Waktu/Durasi"); | |
Scanner input = new Scanner(System.in); | |
Waktu mulai = new Waktu(); | |
System.out.print("Waktu mulai -> jam: "); | |
mulai.jam = input.nextInt(); | |
System.out.print(" menit: "); | |
mulai.menit = input.nextInt(); | |
System.out.print(" detik: "); | |
mulai.detik = input.nextInt(); | |
Waktu selesai = new Waktu(); | |
System.out.print("Waktu selesai -> jam: "); | |
selesai.jam = input.nextInt(); | |
System.out.print(" menit: "); | |
selesai.menit = input.nextInt(); | |
System.out.print(" detik: "); | |
selesai.detik = input.nextInt(); | |
if (keluar.jam < masuk.jam) { | |
System.out.println("Program ini hanya bekerja pada hari yang sama. " + | |
"Silakan input waktu selesai dengan benar."); | |
} else { | |
Waktu lama = masuk.durasike(keluar); | |
System.out.println("Lama waktu/durasi: " + | |
lama.jam + " jam " + | |
lama.menit + " menit " + | |
lama.detik + " detik"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// srifqi, MIT License | |
public class Waktu { | |
public int jam = 0; | |
public int menit = 0; | |
public int detik = 0; | |
public Waktu durasike(Waktu masadepan) { | |
Waktu lama = new Waktu(); | |
int lamadetik = (masadepan.jam - this.jam) * 3600 + | |
(masadepan.menit - this.menit) * 60 + | |
(masadepan.detik - this.detik); | |
lama.jam = lamadetik / 3600; | |
lamadetik = lamadetik % 3600; | |
lama.menit = lamadetik / 60; | |
lamadetik = lamadetik % 60; | |
lama.detik = lamadetik; | |
return lama; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment