Tugas 2 - Biodata dan Time Class Case Study
Pada pertemuan kedua kali ini, kami diberi tugas untuk membuat Biodata dan juga Time Class Study yang merujuk pada buku Java - How to Program 9th edition oleh Deitel pada bab 8.
Berikut merupakan dokumentasi dari tugas yang diberikan,
Biodata
Berikut merupakan source code dari tugas biodata
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
/** | |
* Class Biodata menampung data mahasiswa, nama, alamat, umur, jurusan. | |
* | |
* @author Fidhia Ainun Khofifah | |
* @version 0.2 8 Oktober 2020 | |
*/ | |
public class Biodata | |
{ | |
public Biodata() | |
{ | |
System.out.println("Data Mahasiswa "); | |
System.out.println("======================="); | |
System.out.println("Nama Mahasiswa : Fidhia Ainun Khofifah "); | |
System.out.println("Alamat Mahasiswa : Jl. Cengkeh no. 10 Gresik"); | |
System.out.println("Umur Mahasiswa : 19 tahun"); | |
System.out.println("Jurusan Mahasiswa : Informatika"); | |
System.out.println("Nomor Telepon/WA Mahasiswa : 082257713666"); | |
} | |
} |
dan screenshot dari run code
Time Class Case Study
Fig. 8.1-8.2
Terdapat dua kelas, yaitu Time1 (Fig. 8.1) dan Time1Test (Fig. 8.2). Class Time1 merepresentasikan waktu hari ini. Untuk Class Time1Test adalah sebuah aplikasi class yang merupakan main yang membuat sebuah objek dari class Time1 dan memanggilnya. Class berikut harus di deklarasikan pada file terpisah karena class tersebut merupakan public classes.
Berikut merupakan source code dari program
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
//Fig. 8.1: Time1.java | |
//Time1 class declaration maintains the time in 24-hour format. | |
//@author: Fidhia Ainun Khofifah | |
public class Time1 | |
{ | |
private int hour; | |
private int minute; | |
private int second; | |
public void setTime(int h, int m, int s) | |
{ | |
if((h>=0 && h<24) && (m>=0 && m<60) && (s>=0 && s<60)) | |
{ | |
hour = h; | |
minute = m; | |
second = s; | |
} | |
else | |
{ | |
throw new IllegalArgumentException("hour, minute and/or second was out of range"); | |
} | |
} | |
public String toUniversalString() | |
{ | |
return String.format("%02d:%02d:%02d", hour, minute, second); | |
} | |
public String toString() | |
{ | |
return String.format("%d:%02d:%02d %s", ((hour==0 || hour==12) ? 12 : hour%12), minute, second, | |
(hour<12 ? "AM" : "PM")); | |
} | |
} |
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
//Fig. 8.2: Time1Test.java | |
//Time1 object used in an application | |
//@author: Fidhia Ainun Khofifah | |
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 standard 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, 99, 99); | |
} | |
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()); | |
} | |
} |
Berikut merupakan hasil run code program
Fig. 8.3 Controlling Access to Members
Pada 8.3 ditunjukkan bahwa member private class tidak dapat diakses di luar kelas. Line ke 9-11 mencoba untuk mengakses private class, seperti variabel hour, minute dan juga second dari Time1 object time. Ketika program di compile akan terjadi error karena member private class tidak dapat diakses.
Berikut merupakan source code dari program
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
//Fig. 8.3: MemberAccessTest.java | |
//Private members of class Time1 are not accessible. | |
//@author: Fidhia Ainun Khofifah | |
public class MemberAccessTest | |
{ | |
public static void main(String[] args) | |
{ | |
Time1 time = new Time1(); | |
time.hour = 7; | |
time.minute = 15; | |
time.second = 30; | |
} | |
} |
Fig. 8.4 Reffering to the Current Object's Members with the 'this' Reference
Berikut merupakan source code dari program
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
//Fig. 8.4: ThisTest.java | |
//this used implicity and explicity to refer to members of an object. | |
//@author: Fidhia Ainun Khofifah | |
public class ThisTest | |
{ | |
public static void main(String[] args) | |
{ | |
SimpleTime time = new SimpleTime(15, 30, 19); | |
System.out.println(time.buildString()); | |
} | |
} | |
class SimpleTime | |
{ | |
private int hour; | |
private int minute; | |
private int second; | |
public SimpleTime(int hour, int minute, int second) | |
{ | |
this.hour = hour; | |
this.minute = minute; | |
this.second = second; | |
} | |
public String buildString() | |
{ | |
return String.format("%24s: %s\n%24s: %s", "this.toUniversalString()", | |
this.toUniversalString(), "toUniversalString()", toUniversalString()); | |
} | |
public String toUniversalString() | |
{ | |
return String.format("%02d:%02d:%02d", this.hour, this.minute, this.second); | |
} | |
} |
Berikut merupakan hasil run code program
Fig. 8.5-8.6 Time Class Case Study: Overloaded Constructors
Berikut merupakan source code dari program
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
//Fig. 8.5: Time2.java | |
//Time2 class declaration wirth overloaded constructors. | |
//@author: Fidhia Ainun Khofifah | |
public class Time2 | |
{ | |
private int hour; | |
private int minute; | |
private int second; | |
public Time2() | |
{ | |
this(0, 0, 0); | |
} | |
public Time2(int h) | |
{ | |
this(h, 0, 0); | |
} | |
public Time2(int h, int m) | |
{ | |
this(h, m, 0); | |
} | |
public Time2(int h, int m, int s) | |
{ | |
setTime(h, m, s); | |
} | |
public Time2(Time2 time) | |
{ | |
this(time.getHour(), time.getMinute(), time.getSecond()); | |
} | |
public void setTime(int h, int m, int s) | |
{ | |
setHour(h); | |
setMinute(m); | |
setSecond(s); | |
} | |
public void setHour(int h) | |
{ | |
if(h>=0 && h<24) | |
{ | |
hour=h; | |
} | |
else | |
{ | |
throw new IllegalArgumentException("hour must be 0-23"); | |
} | |
} | |
public void setMinute(int m) | |
{ | |
if(m>=0 && m<60) | |
minute=m; | |
else | |
throw new IllegalArgumentException("minute must be 0-59"); | |
} | |
public void setSecond(int s) | |
{ | |
if(s>=0 && s<60) | |
second = ((s>=0 && s<60) ? s : 0); | |
else | |
throw new IllegalArgumentException("second must be 0-59"); | |
} | |
public int getHour() | |
{ | |
return hour; | |
} | |
public int getMinute() | |
{ | |
return minute; | |
} | |
public int getSecond() | |
{ | |
return second; | |
} | |
public String toUniversalString() | |
{ | |
return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond()); | |
} | |
public String toString() | |
{ | |
return String.format("%d:%02d:%02d %s",((getHour()==0 || getHour()==12) ? 12 : getHour()%12), | |
getMinute(), getSecond(), (getHour()<12 ? "AM" : "PM")); | |
} | |
} |
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
//Fig. 8.6: Time2Test.java | |
//Overload constructors used to initialize Time2 objects. | |
//@author: Fidhia Ainun Khofifah | |
public class Time2Test | |
{ | |
public static void main(String[] args) | |
{ | |
Time2 t1 = new Time2(); | |
Time2 t2 = new Time2(2); | |
Time2 t3 = new Time2(21, 34); | |
Time2 t4 = new Time2(12, 25, 42); | |
Time2 t5 = new Time2(t4); | |
System.out.println("Constructed with:"); | |
System.out.println("t1: all arguments defaulted"); | |
System.out.printf(" %s\n", t1.toUniversalString()); | |
System.out.printf(" %s\n", t1.toString()); | |
System.out.println("t2: hour specified; minute and second defaulted"); | |
System.out.printf(" %s\n", t2.toUniversalString()); | |
System.out.printf(" %s\n", t2.toString()); | |
System.out.println("t3: hour and minute specified; second defaulted"); | |
System.out.printf(" %s\n", t3.toUniversalString()); | |
System.out.printf(" %s\n", t3.toString()); | |
System.out.println("t4: hour, minute and second specified"); | |
System.out.printf(" &s\n", t4.toUniversalString()); | |
System.out.printf(" &s\n", t4.toString()); | |
System.out.println("t5: Time2 object t4 specified"); | |
System.out.printf(" %s\n", t5.toUniversalString()); | |
System.out.printf(" %s\n", t5.toString()); | |
try | |
{ | |
Time2 t6 = new Time2(27, 74, 99); | |
} | |
catch(IllegalArgumentException e) | |
{ | |
System.out.printf("\nException while initializing t6: %s\n", e.getMessage()); | |
} | |
} | |
} |
Berikut merupakan hasil run code program
Sekian dari dokumentasi Tugas 2 PBO ini.
Comments
Post a Comment