Senin, 27 Februari 2017

Classes and Objects


Program Chapter 8 Classes and Objects: A Deeper Look (8.1 - 8.6)



8.1 Time1 class declaration maintains the time in 24-hour format 


  1. /**  
  2.   * Program Kelas Time1  
  3.   * Deklarasi kelas Time1 mengelola waktu dalam format 24 jam  
  4.   * Nama file : Time1.java  
  5.   */
  6. public class Time
  7. {
  8.     private int hour;
  9.     private int minute;
  10.     private int second;
  11.    
  12.     public void setTime (int h, int m, int s)
  13.     {
  14.         if((h>=0 && h<24) && (m>=0 && m<60) && (s>=0 && s<60))
  15.         {
  16.             hour=h;
  17.             minute=s;
  18.             second=s;
  19.         }
  20.         else
  21.             throw new IllegalArgumentException("hour, minute and/or second was out of range");
  22.     }
  23.    
  24.     public String toUniversalString()
  25.     {
  26.         return String.format("%02d:%02d:%02", hour, minute, second);
  27.     }
  28.    
  29.     public String toString()
  30.     {
  31.         return String.format("%d:%02d:%02d %s",
  32.         ((hour==0 || hour==12)?12:hour%12),
  33.         minute, second, (hour<12? "AM" : "PM"));
  34.     }
  35. }

8.2 Time1 object used in an application 


  1. /**  
  2.   * Program Kelas Time1Test  
  3.   * Objek Time1 digunakan dalam aplikasi  
  4.   * nama file : Time1Test.java  
  5.   */  
  6. public class Time1Test  
  7.  {  
  8.    public static void main( String[] args )  
  9.    {  
  10.      Time1 time = new Time1();  
  11.      System.out.print( "The initial universal time is: ");  
  12.      System.out.println(time.toUniversalString() );  
  13.      System.out.print( "The initial standard time is: ");  
  14.      System.out.println(time.toString() );  
  15.      System.out.println();  
  16.      time.setTime( 13, 27, 6);  
  17.      System.out.print( "Universal time after setTime is: ");  
  18.      System.out.println(time.toUniversalString() );  
  19.      System.out.print("Standard time after setTime is: ");  
  20.      System.out.println( time.toString() );  
  21.      System.out.println();  
  22.      try  
  23.      {  
  24.        time.setTime( 99, 99 , 99 );  
  25.      }  
  26.      catch ( IllegalArgumentException e )  
  27.      {  
  28.        System.out.printf( "Exception: %s\n\n", e.getMessage() );  
  29.      }  
  30.      System.out.println( "After attempting invalid settings:");  
  31.      System.out.print( "Universal time: ");  
  32.      System.out.println(time.toUniversalString() );  
  33.      System.out.print("Standard time: ");  
  34.      System.out.println( time.toString() );  
  35.    }  
  36.  }

Output:



8.3 Controlling Access to Members 


  1. /**  
  2.  * Program Kelas MemberAccessTest  
  3.  * Anggota kelas private Time1 tidak dapat diakses  
  4.  * Nama file : MemberAccessTest.java  
  5.  */  
  6.    
  7. public class MemberAccessTest
  8. {
  9.    public static void main(String[] args)
  10.    {
  11.        Time1 time = new Time1();
  12.        
  13.        time.hour = 7; //error: has private access
  14.        time.minute = 15; //error: has private access
  15.        time.second = 30; //error: has private access
  16.    }
  17. }


8.4 Referring to the Current Object's Members with the this Reference 


  1. /**  
  2.  * Program Kelas ThisTest  
  3.  * Program ini digunakan secara implisit dan eksplisit untuk merujuk kepada anggota dari sebuah objek  
  4.  * Nama file : ThisTest.java  
  5.  */  
  6. public class ThisTest
  7. {
  8.     public static void main (String[] args)
  9.     {
  10.         SimpleTime time = new SimpleTime(15, 30, 19);
  11.         System.out.println(time.buildString());
  12.     }
  13. }
  14. class SimpleTime
  15. {
  16.     private int hour;
  17.     private int minute;
  18.     private int second;
  19.    
  20.     public SimpleTime(int hour, int minute, int second)
  21.     {
  22.         this.hour=hour;
  23.         this.minute=minute;
  24.         this.second=second;
  25.     }
  26.    
  27.     public String buildString()
  28.     {
  29.         return String.format("%24s: %s\n%24s: %s",
  30.             "this.toUniversalString()", this.toUniversalString(),
  31.             "toUniversalString()", toUniversalString());
  32.     }
  33.    
  34.     public String toUniversalString()
  35.     {
  36.         return String.format( "%02d:%02d:%02d",
  37.             this.hour, this.minute, this.second);
  38.     }
  39. }

Output:




8.5 Time2 class with overloaded constructors


  1. /**  
  2.   * Program Kelas Time2  
  3.   * Deklarasi kelas Time2 dengan konstuktor overload  
  4.   * Nama file : Time2.java  
  5.   */
  6. public class Time2
  7. {
  8.     private int hour;
  9.     private int minute;
  10.     private int second;
  11.    
  12.     public Time2()
  13.     {
  14.         this( 0, 0, 0 );
  15.     }
  16.    
  17.     public Time2( int h )
  18.     {
  19.         this( h, 0, 0 );
  20.     }
  21.    
  22.     public Time2( int h, int m )
  23.     {
  24.         this( h, m, 0 );
  25.     }
  26.    
  27.     public Time2( int h, int m, int s )
  28.     {
  29.         setTime( h, m, s );
  30.     }
  31.    
  32.     public Time2( Time2 time )
  33.     {
  34.         this( time.getHour(), time.getMinute(), time.getSecond() );
  35.     }
  36.    
  37.     public void setTime( int h, int m, int s )
  38.     {
  39.         setHour( h );
  40.         setMinute( m );
  41.         setSecond( s );
  42.     }
  43.     public void setHour( int h )
  44.     {
  45.         if ( h >= 0 && h < 24 )
  46.             hour = h;
  47.         else
  48.             throw new IllegalArgumentException( "hour must be 0-23" );
  49.     }
  50.     public void setMinute( int m )
  51.     {
  52.         if ( m >= 0 && m < 60 )
  53.             minute = m;
  54.         else
  55.             throw new IllegalArgumentException( "minute must be 0-59" );
  56.     }
  57.     public void setSecond( int s )
  58.     {
  59.         if ( s >= 0 && s < 60 )
  60.             second = ( ( s >= 0 && s < 60 ) ? s : 0 );
  61.         else
  62.             throw new IllegalArgumentException( "second must be 0-59" );
  63.     }
  64.     public int getHour()
  65.     {
  66.         return hour;
  67.     }
  68.     public int getMinute()
  69.     {
  70.         return minute;
  71.     }
  72.    
  73.     public int getSecond()
  74.     {
  75.         return second;
  76.     }
  77.     public String toUniversalString()
  78.     {
  79.         return String.format(
  80.             "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
  81.     }
  82.     public String toString()
  83.     {
  84.         return String.format( "%d:%02d:%02d %s",
  85.             ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
  86.                 getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
  87.     }
  88. }

8.6 Overloaded constructors used to initialize Time2 objects 


  1. /**  
  2.   * Program Kelas Time2Test  
  3.   * Konstruktor overload digunakan untuk menginisialisasi objek Time2  
  4.   * Nama file : Time2Test.java  
  5.   */  
  6. public class Time2Test
  7. {
  8.     public static void main(String[] args)
  9.     {
  10.         Time2 t1 = new Time2();
  11.         Time2 t2 = new Time2( 2 );
  12.         Time2 t3 = new Time2( 21, 34 );
  13.         Time2 t4 = new Time2( 12, 25, 42 );
  14.         Time2 t5 = new Time2( t4 );
  15.        
  16.         System.out.println( "Constructed with:" );
  17.         System.out.println( "t1: all arguments defaulted" );
  18.         System.out.printf( " %s\n", t1.toUniversalString() );
  19.         System.out.printf( " %s\n", t1.toString() );
  20.         System.out.println(
  21.             "t2: hour specified; minute and second defaulted" );
  22.         System.out.printf( " %s\n", t2.toUniversalString() );
  23.         System.out.printf( " %s\n", t2.toString() );
  24.         System.out.println(
  25.             "t3: hour and minute specified; second defaulted" );
  26.         System.out.printf( " %s\n", t3.toUniversalString() );
  27.         System.out.printf( " %s\n", t3.toString() );
  28.         System.out.println( "t4: hour, minute and second specified" );
  29.         System.out.printf( " %s\n", t4.toUniversalString() );
  30.         System.out.printf( " %s\n", t4.toString() );
  31.         System.out.println( "t5: Time2 object t4 specified" );
  32.         System.out.printf( " %s\n", t5.toUniversalString() );
  33.         System.out.printf( " %s\n", t5.toString() );
  34.         try
  35.         {
  36.             Time2 t6 = new Time2( 27, 74, 99 );
  37.         }
  38.         catch ( IllegalArgumentException e )
  39.         {
  40.             System.out.printf( "\nException while initializing t6: %s\n",
  41.             e.getMessage() );
  42.         }
  43.     }
  44.  }

Output:

Rangkuman Buku System Analysis and Design

BAB 3: Requirements Determination -           Fase Analisis Fase analisis menentukan garis besar tujuan bisnis untuk sistem, menentukan...