tugas 2 pbo



Time1 class declaration maintains the time in 24-hour format

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



Time1 object used in an application



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



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


  1. public class ThisTest
  2. {
  3.     public static void main (String[] args)
  4.     {
  5.         SimpleTime time = new SimpleTime(151515);
  6.         System.out.println(time.buildString());
  7.     }
  8. }
  9.  
  10. class SimpleTime
  11. {
  12.     private int hour;
  13.     private int minute;
  14.     private int second;
  15.    
  16.     public SimpleTime(int hour, int minute, int second)
  17.     {
  18.         this.hour=hour;
  19.         this.minute=minute;
  20.         this.second=second;
  21.     }
  22.    
  23.     public String buildString()
  24.     {
  25.         return String.format("%24s: %s\n%24s: %s",
  26.             "this.toUniversalString()"this.toUniversalString(),
  27.             "toUniversalString()", toUniversalString());
  28.     }
  29.    
  30.     public String toUniversalString()
  31.     {
  32.         return String.format( "%02d:%02d:%02d",
  33.             this.hourthis.minutethis.second);
  34.     }
  35. }

Comments

Popular posts from this blog

Buffon Itu Masih Ada

Neymar’s Father Rubbishes Report of Barca-Santos Agreement