daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.IOException;
  2.  
  3. public class Time
  4. {
  5.  
  6.     private int hour;
  7.    
  8.     private int minute;
  9.    
  10.     public Time()
  11.     {
  12.         hour = 0;
  13.         minute = 0;
  14.     }
  15.  
  16.  
  17.     public Time(int h, int m)
  18.     {
  19.         if ( h >= 1 && h <= 23)
  20.             hour =h;
  21.         else
  22.             hour = 0;
  23.         if ( m >= 0 && m <= 59)
  24.             minute = m;
  25.         else
  26.             minute = 0;
  27.     }
  28.  
  29.  
  30.  
  31.     public String toString()
  32.     {
  33.         String s = "";
  34.        
  35.         if ( hour < 10 && minute < 10)
  36.             s = "0" + hour + "0" + minute;
  37.        
  38.         else if ( hour < 10 && minute > 10)
  39.             s = "0" + hour + minute;
  40.        
  41.         else if ( hour > 10 && minute < 10)
  42.             s = hour + "0" + minute;
  43.        
  44.         else if ( hour > 10 && minute > 10)
  45.             s = hour +""+ minute;
  46.        
  47.         else if ( hour == 0)
  48.             s = "0" + hour + minute;
  49.        
  50.         else if ( minute == 0)
  51.             s = hour + "0" + minute;
  52.  
  53.         return s;
  54.  
  55.     }
  56.  
  57.     public String convert()
  58.     {
  59.         String c = "";
  60.  
  61.         if (hour >11)
  62.         {
  63.             if (hour == 12)
  64.             {
  65.                 c = hour +":"+ minute + " PM";
  66.             }
  67.             if (hour > 12)
  68.             {
  69.                 c = (hour - 12) +":"+ minute + " PM";
  70.             }
  71.         }
  72.         else if (hour == 0)
  73.         {
  74.             c = (hour+12) +":"+ minute + " AM";
  75.         }
  76.  
  77.         else
  78.              c = hour +":"+ minute + " AM";
  79.  
  80.         if(minute < 10)
  81.         {
  82.             c= c.substring(0, c.length()-4)+"0"+minute+" "+c.substring(c.length()-2, c.length());
  83.         }
  84.         return c;
  85.     }
  86.    
  87.     public void increment()
  88.     {
  89.         if (hour == 23 && minute == 59)
  90.         {
  91.             hour = 0;
  92.             minute = 0;
  93.         }
  94.         else
  95.         {
  96.         minute++;
  97.         if (minute == 60)
  98.         {
  99.             hour++;
  100.             minute = 0;
  101.         }
  102.         else if ( hour == 24)
  103.             hour = 0;
  104.         }
  105.     }
  106.  
  107.      public static void main(String str[]) throws IOException
  108.      {
  109.  
  110.          Time time1 = new Time(14, 56);
  111.           System.out.println("time1: " + time1);
  112.           System.out.println("convert time1 to standard time: " + time1.convert());
  113.           System.out.println("time1: " + time1);
  114.           System.out.print("increment time1 five times: ");
  115.           time1.increment();
  116.           time1.increment();
  117.           time1.increment();
  118.           time1.increment();
  119.           time1.increment();
  120.           System.out.println(time1 + "\n");
  121.  
  122.           Time time2 = new Time(-7, 12);
  123.           System.out.println("time2: " + time2);
  124.           System.out.print("increment time2 67 times: ");
  125.           for (int i = 0; i < 67; i++)
  126.            time2.increment();
  127.           System.out.println(time2);
  128.           System.out.println("convert to time2 standard time: " + time2.convert());
  129.           System.out.println("time2: " + time2 + "\n");
  130.  
  131.  
  132.           Time time3 = new Time(5, 1);
  133.           System.out.println("time3: " + time3);
  134.           System.out.print("convert time3: ");
  135.           System.out.println(time3.convert());
  136.  
  137.           Time time4 = new Time(12, 15);
  138.           System.out.println("\ntime4: " + time4);
  139.           System.out.println("convert time4: " + time4.convert());
  140.  
  141.           Time time5 = new Time(0, 15);
  142.           System.out.println("\ntime5: " + time5);
  143.           System.out.println("convert time5: " + time5.convert());
  144.  
  145.           Time time6 = new Time(24, 15);
  146.           System.out.println("\ntime6: " + time6);
  147.           System.out.println("convert time6: " + time6.convert());
  148.  
  149.           Time time7 = new Time(23,59);
  150.           System.out.println("\ntime7: " + time7);
  151.           System.out.println("convert time7: " + time7.convert());
  152.           time7.increment();
  153.           System.out.println("increment time7: " + time7);
  154.           System.out.println("convert time7: " + time7.convert());
  155.  
  156.      }
  157.  
  158. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top