novemberstorsm09

Time2Test

Jul 6th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. public class Time2Test
  2. {
  3.    public static void main(String[] args)
  4.    {
  5.       Time2 t1 = new Time2(); // 00:00:00
  6.       Time2 t2 = new Time2(2); // 02:00:00
  7.       Time2 t3 = new Time2(21, 34); // 21:34:00
  8.       Time2 t4 = new Time2(12, 25, 42); // 12:25:42
  9.       Time2 t5 = new Time2(t4); // 12:25:42
  10.  
  11.      
  12.       System.out.println("Constructed with:");
  13.       displayTime("t1: all default arguments", t1);
  14.       displayTime("t2: hour specified; default minute and second", t2);
  15.       displayTime("t3: hour and minute specified; default second", t3);
  16.       displayTime("t4: hour, minute and second specified", t4);
  17.       displayTime("t5: Time2 object t4 specified", t5);
  18.  
  19.       // attempt to initialize t6 with invalid values
  20.       try
  21.       {
  22.          Time2 t6 = new Time2(27, 74, 99); // invalid values
  23.       }
  24.       catch (IllegalArgumentException e)
  25.       {
  26.          System.out.printf("%nException while initializing t6: %s%n",
  27.             e.getMessage());
  28.       }
  29.    }
  30.  
  31.    // displays a Time2 object in 24-hour and 12-hour formats
  32.    private static void displayTime(String header, Time2 t)
  33.    {
  34.       System.out.printf("%s%n   %s%n   %s%n",
  35.          header, t.toUniversalString(), t.toString());
  36.    }
  37. } // end class Time2Test
Add Comment
Please, Sign In to add comment