Tugas kedua kelas PBO-C Class dan Object serta membuat biodata
TUGAS PBO Pertemuan 2 : Biodata & Membuat Time1 dari buku Deitel
Pada pertemuan kedua kelas PBO-C, saya diberi tugas untuk membuat Biodata melalui BlueJ dan juga mengerjakan latihan program 8.1-8.6. berikut hasil Biodata saya :
Terminal Window Biodataku:
SourceCode Biodataku :
Terminal Window Time1 dan Time1Test :
Source Code Time1 :
public class Time1
{
private int jam,menit,detik;
public void setTime(int j, int m, int d)
{
if ((j >= 0 && j < 24) && (m >= 0 && m < 60) && (d >= 0 && d<60))
{
jam = j;
menit = m;
detik = d;
}
else
throw new IllegalArgumentException("jam, menit dan/atau detik di luar rentang");
} // akhir dari setTime
public String toUniversalString()
{
return String.format("%02d:%02d:%02d", jam, menit, detik);
} //akhir format String
public String toString()
{
return String.format("%d:%02d:%02d %s",
((jam == 0 || jam == 12) ? 12 : jam % 12),
menit, detik, (jam < 12 ? "AM" : "PM"));
}
}
SourceCode Time1test :
public class Time1Test
{
public static void main (String[] args)
{
Time1 time = new Time1();
System.out.print("The initial universal time is : ");
System.out.println(time.toUniversalString() );
System.out.print("The initial standart time is : ");
System.out.println(time.toString() );
System.out.println();
time.setTime(13, 27, 6);
System.out.print("Universal time after setTime is : ");
System.out.println(time.toUniversalString() );
System.out.print("Standard time after setTime is: " );
System.out.println(time.toString() );
System.out.println();
try
{
time.setTime(99, 89, 79);
}
catch (IllegalArgumentException e)
{
System.out.printf("Exception : %s\n\n", e.getMessage() );
}
System.out.println("After attempting invalid settings : ");
System.out.print("Universal time : ");
System.out.println(time.toUniversalString() );
System.out.print("Standard time : ");
System.out.println(time.toString() );
}
}
Sekian Terima kasih.
Komentar
Posting Komentar