TUGAS 3 PBO A : JAVA TIME PRACTICE


  1. **
  2.  * Time1.java
  3.  * Time1 class declaration maintains the time in 24hour format.
  4.  *
  5.  * @Benito Danneswara
  6.  * @26 February 2017
  7.  */
  8. public class Time1
  9. {
  10.     private int hour; // 0 – 23
  11.     private int minute; // 0 – 59
  12.     private int second; // 0 – 59
  13.     // set a new time value using universal time; throw an
  14.     // exception if the hour, minute or second is invalid.
  15.     public void setTime (int h, int m, int s)
  16.     {
  17.         // validate hour, minute and second
  18.         if ((>= 0 && h < 24) && ( m >= 0 && m < 60) && (>= 0 && s < 60))
  19.         {
  20.             hour = h;
  21.             minute = m;
  22.             second = s;
  23.         } // end if
  24.         else
  25.         {
  26.             throw new IllegalArgumentException
  27.             (“hour, minute and/or second was out of range”);
  28.         }
  29.     } // end method setTime
  30.     // convert to String in universal-time format (HH:MM:SS)
  31.     public String toUniversalString()
  32.     {
  33.         return String.format (“%02d:%02d:%02d”, hour, minute, second);
  34.     } // end method toUniversalString
  35.     // convert to String in standard-time format (H:MM:SS AM or PM)
  36.     public String toString()
  37.     {
  38.         return String.format (“%d:%02d:%02d %s”,
  39.             (( hour == 0 || hour == 12 ) ? 12 : hour % 12),
  40.             minute, second, (hour < 12 ? “AM” : “PM”));
  41.     } // end method toString
  42. } // end class Time1
DATA HOSTED WITH ♥ BY PASTEBIN.COM – DOWNLOAD RAW – SEE ORIGINAL
  1. /**
  2.  * Time1Test.java
  3.  *
  4.  * Benito Dannes
  5.  * 26 February 2017
  6.  */
  7. public class Time1Test
  8. {
  9.     public static void main (String[] args)
  10.     {
  11.         // create and initialize a Time1 object
  12.         Time1 time = new Time1(); // invokes Time1 constructor
  13.         // output string representations of the time
  14.         System.out.print (“The initial universal time is: “);
  15.         System.out.println (time.toUniversalString());
  16.         System.out.print (“The initial standard time is: “);
  17.         System.out.println (time.toString());
  18.         System.out.println(); // output a blank line
  19.         // change time and output updated time
  20.         time.setTime (13276);
  21.         System.out.print (“Universal time after setTime is: “);
  22.         System.out.println (time.toUniversalString());
  23.         System.out.print (“Standard time after setTime is: “);
  24.         System.out.println (time.toString());
  25.         System.out.println(); // output a blank line
  26.         // attempt to set time with invalid values
  27.         try
  28.         {
  29.             time.setTime (999999); // all values out of range
  30.         } // end try
  31.         catch (IllegalArgumentException e)
  32.         {
  33.             System.out.printf (“Exception: %s\n\n, e.getMessage());
  34.         } // end catch
  35.         // display time after attempt to set invalid values
  36.         System.out.println (“After attempting invalid settings:” );
  37.         System.out.print (“Universal time: “);
  38.         System.out.println (time.toUniversalString());
  39.         System.out.print (“Standard time: “);
  40.         System.out.println (time.toString());
  41.     } // end main
  42. } // end class Time1Test

Output yang diberikan adalah seperti berikut:

DATA HOSTED WITH ♥ BY PASTEBIN.COM – DOWNLOAD RAW – SEE ORIGINAL
  1. /**
  2.  * ThisTest.java
  3.  * this used implicitly and explicitly to refer to members of an object.
  4.  *
  5.  * Benito Dannes
  6.  * 26 February 2017
  7.  */
  8. public class ThisTest
  9. {
  10.     public static void main (String[] args)
  11.     {
  12.      SimpleTime time = new SimpleTime (153019);
  13.      System.out.println (time.buildString());
  14.     } // end main
  15. } // end class ThisTest
  16. // class SimpleTime demonstrates the “this” reference
  17. class SimpleTime
  18. {
  19.     private int hour; // 0 – 23
  20.     private int minute; // 0 – 59
  21.     private int second; // 0 – 59
  22.     // if the constructor uses parameter names identical to
  23.     // instance variable names the “this” reference is
  24.     // required to distinguish between the names
  25.     public SimpleTime (int hour, int minute, int second)
  26.     {
  27.         this.hour = hour; // set “this” object’s hour
  28.         this.minute = minute; // set “this” object’s minute
  29.         this.second = second; // set “this” object’s second
  30.     } // end SimpleTime constructor
  31.     // use explicit and implicit “this” to call toUniversalString
  32.     public String buildString()
  33.     {
  34.         return String.format (“%24s: %s\n%24s: %s”,
  35.             “this.toUniversalString()”this.toUniversalString(),
  36.             “toUniversalString()”, toUniversalString());
  37.     } // end method buildString
  38.     // convert to String in universal-time format (HH:MM:SS)
  39.     public String toUniversalString()
  40.     {
  41.         // “this” is not required here to access instance variables,
  42.         // because method does not have local variables with same
  43.         // names as instance variables
  44.         return String.format (“%02d:%02d:%02d”,
  45.             this.hourthis.minutethis.second);
  46.     } // end method toUniversalString
  47. } // end class SimpleTime

Outputnya:

DATA HOSTED WITH ♥ BY PASTEBIN.COM – DOWNLOAD RAW – SEE ORIGINAL
  1. /**
  2.  * Time2.java
  3.  * Time2 class declaration with overloaded constructors.
  4.  *
  5.  * Benito Danneswara
  6.  * 26 February 2017
  7.  */
  8. public class Time2
  9. {
  10.     private int hour; // 0 – 23
  11.     private int minute; // 0 – 59
  12.     private int second; // 0 – 59
  13.     // Time2 no-argument constructor:
  14.     // initializes each instance variable to zer
  15.     public Time2()
  16.     {
  17.         this (000); // invoke Time2 constructor with three arguments
  18.     } // end Time2 no-argument constructor
  19.     // Time2 constructor: hour supplied, minute and second defaulted to 0
  20.     public Time2 (int h)
  21.     {
  22.         this (h, 00); // invoke Time2 constructor with three arguments
  23.     } // end Time2 one-argument constructor
  24.     // Time2 constructor: hour and minute supplied, second defaulted to 0
  25.     public Time2 (int h, int m)
  26.     {
  27.         this (h, m, 0); // invoke Time2 constructor with three arguments
  28.     } // end Time2 two-argument constructor
  29.     // Time2 constructor: hour, minute and second supplied
  30.     public Time2 (int h, int m, int s)
  31.     {
  32.         setTime (h, m, s); // invoke setTime to validate time
  33.     } // end Time2 three-argument constructor
  34.     // Time2 constructor: another Time2 object supplied
  35.     public Time2 (Time2  time)
  36.     {
  37.         // invoke Time2 three-argument constructor
  38.         this (time.getHour(), time.getMinute(), time.getSecond());
  39.     } // end Time2 constructor with a Time2 object argument
  40.     // Set Methods
  41.     // Set a new time value using universal time;
  42.     // validate the data
  43.     public void setTime (int h, int m, int s)
  44.     {
  45.         setHour (h); // set the hour
  46.         setMinute (m); // set the minute
  47.         setSecond (s); // set the second
  48.     } // end method setTime
  49.     // validate and set hour
  50.     public void setHour (int h)
  51.     {
  52.         if (>0 && h < 24)
  53.             hour = h;
  54.         else
  55.             throw new IllegalArgumentException (“hour must be 0 – 23”);
  56.     } // end method setHour
  57.     // validate and set minute
  58.     public void setMinute (int m)
  59.     {
  60.         if (>0 && m < 60)
  61.             minute = m;
  62.         else
  63.             throw new IllegalArgumentException (“minute must be 0 – 59”);
  64.     } // end method setMinute
  65.     // validate and set second
  66.     public void setSecond (int s)
  67.     {
  68.         if (>0 && s < 60)
  69.             second = ((>0 && s < 60) ? s : 0);
  70.         else
  71.             throw new IllegalArgumentException (“second must be 0 – 59”);
  72.     } // end method setSecond
  73.     // Get Methods
  74.     // get hour value
  75.     public int getHour()
  76.     {
  77.         return hour;
  78.     } // end method getHour
  79.     // get minute value
  80.     public int getMinute()
  81.     {
  82.         return minute;
  83.     } // end method getMinute
  84.     // get second value
  85.     public int getSecond()
  86.     {
  87.         return second;
  88.     } // end method getSecond
  89.     // convert to String in universal-time format (HH:MM:SS)
  90.     public String toUniversalString()
  91.     {
  92.         return String.format (“%02d:%02d:%02d”, getHour(), getMinute(), getSecond());
  93.     } // end method toUniversalString
  94.     // convert to String in standard-time format (H:MM:SS AM or PM)
  95.     public String toString()
  96.     {
  97.         return String.format (“%d:%02d:%02d %s”,
  98.             ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12),
  99.             getMinute(), getSecond()(getHour() < 12 ? “AM” : “PM”));
  100.     } // end method toString
  101. } // end class Time2
DATA HOSTED WITH ♥ BY PASTEBIN.COM – DOWNLOAD RAW – SEE ORIGINAL
  1. /**
  2.  * Time2Test.java
  3.  * Overloaded constructors used to initialize Time2 objects.
  4.  *
  5.  * Benito Danneswara
  6.  * 26 February 2017
  7.  */
  8. public class Time2Test
  9. {
  10.     public static void main (String[] args)
  11.     {
  12.         Time2 t1 = new Time2(); // 00:00:00
  13.         Time2 t2 = new Time2(2); // 02:00:00
  14.         Time2 t3 = new Time2(2134); // 21:34:00
  15.         Time2 t4 = new Time2(122542); // 12:25:42
  16.         Time2 t5 = new Time2(t4); // 12:25:42
  17.         System.out.println (“Constructed with:”);
  18.         System.out.println (“t1: all arguments defaulted”);
  19.         System.out.printf (”    %s\n, t1.toUniversalString());
  20.         System.out.printf (”    %s\n, t1.toString());
  21.         System.out.println (“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 (“t3: hour and minute specified; second defaulted”);
  25.         System.out.printf (”    %s\n, t3.toUniversalString());
  26.         System.out.printf (”    %s\n, t3.toString());
  27.         System.out.println (“t4: hour, minute and second specified”);
  28.         System.out.printf (”    %s\n, t4.toUniversalString());
  29.         System.out.printf (”    %s\n, t4.toString());
  30.         System.out.println (“t5: Time2 object t4 specified”);
  31.         System.out.printf (”    %s\n, t5.toUniversalString());
  32.         System.out.printf (”    %s\n, t5.toString());
  33.         // attempt to initialize t6 with invalid values
  34.         try
  35.         {
  36.             Time2 t6 = new Time2 (27,74,99); // invalid value
  37.         } // end try
  38.         catch (IllegalArgumentException e)
  39.         {
  40.             System.out.printf (\nException while initializing t6: %s\n, e.getMessage());
  41.         } // end catch
  42.     } // end main
  43. } // end class Time2Test

Berikut outputnya:


Tinggalkan komentar