1. Program untuk mencetak Biodata
Screenshot:
2. Program untuk menunjukkan waktu dg format 24 jam atau 12 jam AM/PM
Program Untuk Waktu
Program Untuk Eksekusi
Screenshot:
public class Biodata
{
public Biodata(){
System.out.println("==============Biodata==============");
System.out.println("\n");
System.out.println("Nama : Abdi Dewa Maha Rupawan");
System.out.println("NRP : 05111840000003");
System.out.println("Kelas : PBO - D");
System.out.println("TTL : 30 Juli 2001");
}
}
Screenshot:
2. Program untuk menunjukkan waktu dg format 24 jam atau 12 jam AM/PM
Program Untuk Waktu
public class Time
{
private int hour,minute,second;
public Time(){
hour = 12;
minute = 30;
second = 0;
}
public Time(int hour, int minute, int second){
if( ( hour>=0 && hour<24 ) && ( minute>=0 && minute<60 ) && ( second>=0 && second<60 ) ){
this.hour = hour;
this.minute = minute;
this.second = second;
}
else{
throw new IllegalArgumentException(
"Format jam, menit, dan/atau detik tidak valid (jam 0-23, menit 0-59, detik 0-59)"
);
}
}
public void setTime(int hour, int minute, int second){
if( ( hour>=0 && hour<24 ) && ( minute>=0 && minute<60 ) && ( second>=0 && second<60 ) ){
this.hour = hour;
this.minute = minute;
this.second = second;
}
else{
throw new IllegalArgumentException(
"Format jam, menit, dan/atau detik tidak valid (jam 0-23, menit 0-59, detik 0-59)"
);
}
}
public String Format24h(){
return String.format("%02d:%02d:%02d\n",hour,minute,second);
}
public String Format12h(){
String mode;
int fhour;
if(hour>=12){
mode = "PM";
}
else{
mode = "AM";
}
if(hour!=12){
fhour = hour%12;
}
else{
fhour = hour;
}
return String.format("%02d:%02d:%02d %s\n",fhour,minute,second,mode);
}
}
Program Untuk Eksekusi
public class Timetest
{
public static void main(String[] args){
Time time = new Time();
System.out.println("Waktu Mula2 (Format 24 jam)");
System.out.println( time.Format24h() );
System.out.println("Waktu Mula2 (Format AM/PM)");
System.out.println( time.Format12h() );
System.out.println();
time.setTime(12, 37, 38);
System.out.println("Waktu Sekarang (Format 24 jam)");
System.out.println( time.Format24h() );
System.out.println("Waktu Sekarang (Format AM/PM)");
System.out.println( time.Format12h() );
System.out.println();
try{
time.setTime(99, 99, 99);
}
catch (IllegalArgumentException e){
String pesan = String.format( "Exception: %s\n\n", e.getMessage());
System.out.println(pesan);
}
System.out.println("Setelah percobaan pernyataan yang salah:");
System.out.println("Waktu Sekarang (Format 24 jam)");
System.out.println( time.Format24h() );
System.out.println("Waktu Sekarang (Format AM/PM)");
System.out.println( time.Format12h() );
System.out.println();
}
}
Screenshot:
Comments
Post a Comment