jasonpogi1669

Using Interface for Time Comparison (Machine Problem 7) - Java

Feb 7th, 2021 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /**
  8.  *
  9.  * @author Jason
  10.  */
  11. import java.time.LocalTime;
  12.  
  13. interface Comparison {
  14.     public boolean isGreater(LocalTime obj1, LocalTime obj2);
  15.     public boolean isLess(LocalTime time1, LocalTime time2);
  16.     public boolean isEqual(LocalTime time1, LocalTime time2);
  17. }
  18.  
  19. class Compare implements Comparison {
  20.     public boolean isGreater(LocalTime time1, LocalTime time2) {
  21.         return (time1.compareTo(time2) > 0);
  22.     }
  23.     public boolean isLess(LocalTime time1, LocalTime time2) {
  24.         return (time1.compareTo(time2) < 0);
  25.     }
  26.     public boolean isEqual(LocalTime time1, LocalTime time2) {
  27.         return (time1.compareTo(time2) == 0);
  28.     }
  29. }
  30.  
  31. public class Main {
  32.     public static void main(String[] args) {
  33.         //  Time Object 1
  34.         LocalTime time_object1 = LocalTime.of(3, 30, 10);
  35.         // Time Object 2
  36.         LocalTime time_object2 = LocalTime.of(3, 30, 10);
  37.         Compare cmp = new Compare();
  38.         if (cmp.isGreater(time_object1, time_object2)) {
  39.             System.out.println(time_object1 + " is greater than " + time_object2);
  40.         } else if (cmp.isLess(time_object1, time_object2)) {
  41.             System.out.println(time_object1 + " is less than " + time_object2);
  42.         } else if (cmp.isEqual(time_object1, time_object2)) {
  43.             System.out.println(time_object1 + " is equal to " + time_object2);
  44.         }
  45.     }
  46. }
  47.  
Add Comment
Please, Sign In to add comment