Tugas 1 PBO

 Classes and Objects

Muhammad Izzuddin A (05111740000035)

Kelas B

A. Time class study (Fig 8.1 - 8.2)

Class yang dibuat: Time1 dan Time1Test

Overview class diagram:


Berikut source code dari kelas Time1:
 /**  
  * Program Kelas Time1 (Fig. 8.1)  
  * Deklarasi Kelas Time1, mengelola waktu dalam format 24h  
  *   
  * @author Muhammad Izzuddin A  
  */  
 public class Time1  
 {  
   // instance variables - replace the example below with your own  
   private int hour;  
   private int minute;  
   private int second;  
   /**  
    * Setter method  
    * @param h hour  
    * @param m minute  
    * @param s 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");  
     }  
   }  
   /**  
    * Return date in universal format (24h)  
    * @return  the sum of x and y  
    */  
   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"));  
   }  
 }  
Berikut source code dari class Time1Test:
 /**  
  * Program Kelas Time1Test (Fig. 8.2)  
  * Object Time1 yang digunakan dalam aplikasi  
  *  
  * @author Muhammad Izzuddin A  
  */  
 public class Time1Test  
 {  
   public static void main(String[] args){  
     // initialize new Time1 object  
     Time1 time = new Time1();  
     // print string representation of the time  
     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());  
     // set time and print updated time  
     time.setTime(13, 27, 6);  
     System.out.print("Universal time after setTime() is: ");  
     System.out.println(time.toUniversalString());  
     System.out.print("The standard time after setTime() is: ");  
     System.out.println(time.toString());  
     //try to set with invalid argument  
     try {  
       time.setTime( 99, 99, 99);  
     } catch (IllegalArgumentException e){  
       System.out.printf("Exception: %s\n\n", e.getMessage());  
     }  
     // display time after attempting to set with invalid values  
     System.out.print("After attempting invalid settings: \nUniversal time: ");  
     System.out.println(time.toUniversalString());  
     System.out.print("Standard time: ");  
     System.out.println(time.toString());  
   }  
 }  
 Dan berikut hasil screenshot window console:



B. Controlling Access to Members (Fig 8.1 - 8.2)

Menambahkan kelas MemberAccessTest unutk melihat apakah kelas dapat mengakses properties/varieabel dalam kelas Time1.

Source code MemberAccessTest.java :
 /**  
  * Program Kelas MemberAccessTest (Fig. 8.3)  
  *  
  * @author Muhammad Izzuddin A  
  */  
 // Private members of class Time1 are not accessible.  
 public class MemberAccessTest  
 {  
   public static void main( String[] args ){  
     Time1 time = new Time1();  
     time.hour = 7;  
     time.minute = 15;  
     time.second = 30;  
   }  
 }  
Saat dicompile muncul error sebagai berikut:






C. Referring to The Current Object's Members With The `this` Reference

Setiap objek bisa mengakses reference ke dirinya sendiri menggunakan keyword `this`.

Source code ThisTest.java : 
 /**  
  * Program Kelas ThisTest (Fig. 8.4)  
  * Object ThisTest yang digunakan dalam pengetesan reference  
  *  
  * @author Muhammad Izzuddin A  
  */  
 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);  
   }  
 }  

Jika source code diatas dijalankan maka hasilnya sebagai berikut:



D. Time Class Case Study: Overloaded Constructors

    Pada konsep Object Oriented Programming dikenal istilah overloading yaitu mendeklarasikan sebuah method berulang kali menggunakan argument yang berbeda. Pada contoh kali ini ditunjukan sebuah studi kasus overloading pada method constructor sehingga object dapat diinisialisasi dengan cara yang berbeda.

Source code Time2.java:
 /**  
  * Program Kelas Time2 (Fig. 8.5)  
  * Deklarasi Kelas Time2, dengan overloaded constructor  
  *   
  * @author Muhammad Izzuddin A  
  */  
 public class Time2  
 {  
   // instance variables - replace the example below with your own  
   private int hour;  
   private int minute;  
   private int second;  
   // first constructor  
   public Time2() {  
     this(0,0,0);  
   }  
   /**  
    * second constructor  
    * @param h hour  
    */  
   public Time2(int h) {  
     this(h, 0, 0);  
   }  
   /**  
    * third constructor  
    * @param h hour  
    * @param m minute  
    */  
   public Time2(int h, int m) {  
     this(h, m, 0);  
   }  
   /**  
    * fourth constructor (base, called on this(h,m,s))  
    * @param h hour  
    * @param m minute  
    * @param s second  
    */  
   public Time2(int h, int m, int s) {  
     setTime(h, m, s);  
   }  
   /**  
    * fifth constructor (base, called on this(h,m,s))  
    * @param time - a Time2 class object  
    */  
   public Time2(Time2 time) {  
     this(time.getHour(), time.getMinute(), time.getSecond());  
   }  
   /**  
    * Setter method  
    * @param h hour  
    * @param m minute  
    * @param s second  
    */  
   public void setTime(int h, int m, int s) {  
     setHour(h);  
     setMinute(m);  
     setSecond(s);  
   }  
   /**  
    * Setter method  
    * @param h hour  
    */   
   public void setHour(int h) {  
     if (h >= 0 && h < 24){  
       hour = h;  
     } else {  
       throw new IllegalArgumentException("Hour must be 0-24");  
     }  
   }  
   /**  
    * Setter method  
    * @param m minute  
    */   
   public void setMinute(int m) {  
     if (m>= 0 && m < 60){  
       minute = m;  
     } else {  
       throw new IllegalArgumentException("Minute must be 0-59");  
     }  
   }  
   /**  
    * Setter method  
    * @param s second  
    */   
   public void setSecond(int s) {  
     if (s>= 0 && s < 60){  
       second = s;  
     } else {  
       throw new IllegalArgumentException("Second must be 0-59");  
     }  
   }  
   /**  
    * Getter method  
    * Return hour  
    */  
   public int getHour(){  
     return hour;  
   }  
   /**  
    * Getter method  
    * Return minute  
    */  
   public int getMinute(){  
     return minute;  
   }  
   /**  
    * Getter method  
    * Return second  
    */  
   public int getSecond(){  
     return second;  
   }  
   /**  
    * Return date in universal format (24h)  
    */  
   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"));  
   }  
 }  

Berikut source code dari Time2Test.java yang digunakan untuk mengetes:  
 /**  
  * Program Kelas Time1Test (Fig. 8.2)  
  * Object Time1 yang digunakan dalam aplikasi  
  *  
  * @author Muhammad Izzuddin A  
  */  
 public class Time2Test  
 {  
   public static void main(String[] args){  
     // initialize new Time2 object  
     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 to set with invalid argument  
     try {  
       Time2 t6 = new Time2(27, 75, 99);  
     } catch (IllegalArgumentException e){  
       System.out.printf("\nException while initializing t6: %s\n\n", e.getMessage());  
     }  
   }  
 }  

Dan berikut hasil screenshot window console:








Comments

Popular posts from this blog

ETS PBO

Tugas 6 PBO - Game World of Zuul

Tugas 2 PBO - Aplikasi Traffic Light