Advertisement
SensaBG

On TIME FOR THE EXAM - EXPLAINED

Jul 14th, 2024 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.33 KB | None | 0 0
  1. package PersonalTasks;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class OnTimeForTheExamSlavi {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         // 1.
  10.         // will be reading hours and minutes
  11.  
  12.         // Pointless to create 10 variables especially when
  13.         // the logic is always the same for reading hours and minutes from the console
  14.  
  15.         // so why creating 10 variables when you can create abstract reusable variables?
  16.  
  17.         // It doesn't matter if you read
  18.         // hours and minutes for the Exam start hour
  19.         // or hours and minutes for the arrival hour
  20.  
  21.         // only define that they will exist in the code
  22.         // consider them like 2 tools that we put in our suitcase ( program )
  23.  
  24.         int hours;
  25.         int minutes;
  26.  
  27.         // 2.
  28.         // Read the exam start Hour FROM THE CONSOLE
  29.  
  30.         // In this case the hours and minutes variables read 2 numbers from the console
  31.  
  32.         // use the defined values that in this case are instruments that we use
  33.         // in order to gain info saved from the console
  34.  
  35.         // hours variable read a new line from the input filled in the console
  36.         // minutes variable read another new line from the input filled in the console
  37.  
  38.         // convert logic from hours Into minutes
  39.  
  40.         hours = Integer.parseInt(scanner.nextLine());
  41.         minutes = Integer.parseInt(scanner.nextLine());
  42.         int examMinutes = hours * 60 + minutes;
  43.  
  44.  
  45.         // 3.
  46.         // Read arrival Hour from the console the same way
  47.  
  48.         // use the same variables in order to read the hours and the minute
  49.         // by reading from the console again using the same variables
  50.         // we overwrite the initial values read from the console
  51.  
  52.         // however we already saved the result  in the examMinutes variable
  53.         // so its safe to reuse them in order to calculate the minutes for the arrivalMinutes
  54.  
  55.         hours = Integer.parseInt(scanner.nextLine());
  56.         minutes = Integer.parseInt(scanner.nextLine());
  57.         int arrivalMinutes = hours * 60 + minutes;
  58.  
  59.         // 4.
  60.         // Time difference calculation
  61.         // diff between arrival hour and required hour for arrival
  62.  
  63.         // if the diff is positive it means that we are either on time or earlier than expected
  64.  
  65.         // if diff is negative it means that we are late
  66.  
  67.         // this happens because the arrival hours should be LESS THAN or EQUALS to the exam hour
  68.  
  69.         // we do not use Math Abs here
  70.         // that's because later on in the code we will benefit from the info from the calculation
  71.         // if the product is negative or 0, we are not late for sure.
  72.  
  73.         int timeDifference = arrivalMinutes - examMinutes;
  74.  
  75.         // 5.
  76.         // hours difference
  77.         // minutes difference
  78.  
  79.         // We do this in order to create a variable "time"
  80.         // the variable will have different behaviour depending on the time diff
  81.  
  82.         // is the time difference an hour more or just a few minutes?
  83.  
  84.         // Convert the difference in normal format - Hour and Minutes
  85.  
  86.         // for the calculation of the hours and minutes we need absolute values
  87.         // we get the time diff between the arrival and the exam hour
  88.  
  89.         // we need this since if the diff is not 0 we have different cases
  90.         // Earlier and Later
  91.  
  92.         // Reuse the variables again as this is why we crreated them
  93.         // why create another variables?
  94.  
  95.         // use the tools from the suitcase, that's why we bring them with us
  96.         // they did their job previously but now they have other responsibilites
  97.  
  98.         // Kato si kupish kleshti edva li kato gi pozlvash edin put i si kupuvash novi ?
  99.         // hours and minutes sa nashte kleshti i chuk
  100.         // veche gi polzvahme po rano no sgea imat druga otgovornost
  101.  
  102.         hours = Math.abs(timeDifference / 60);
  103.         minutes = Math.abs(timeDifference % 60);
  104.  
  105.         // 6.
  106.         // defining time
  107.  
  108.         // is the difference a few hours or just a few minutes?
  109.  
  110.         // variable acts differently depending on the time difference
  111.         // so that we can print different things based on the time difference
  112.  
  113.         // define that time will exist in our program ( another isntrument )
  114.  
  115.         // aide vzimame i edna otverka na stroeja
  116.  
  117.         String time;
  118.  
  119.         if (hours == 0) {
  120.  
  121.             // if  the hours are 0, the diff is less than an hour obviously
  122.             // so we force the variable to show only the minutes without the hours since they are 0
  123.  
  124.             // this is required by the Task
  125.  
  126.             // tuka otverkata q polzvam da otviq edno bolt4e
  127.  
  128.             time = String.format("%01d minutes", minutes);
  129.         } else {
  130.  
  131.             // IF the hours are not 0
  132.             // the diff is either an hour or more
  133.  
  134.             // so we properly adjust the variable to display the hours as well
  135.             // since there is a values that we can display
  136.  
  137.             // %02d -> %d int placeholder for fomratted printing
  138.  
  139.             //02 -> we force the minutes to always display 2 digits for the minutes
  140.  
  141.             // the 0 specifies that if we have minutes less than 10 we will always have a leading 0
  142.             // this means that we will print the minutes from 0 to 9 with 2 digits
  143.             // simple example - 1 hour and 7 minutues will be printed like that
  144.             // 1:07
  145.  
  146.             //the 2 from ( %02d ) specifies that we always want 2 digits when we format the int in the printing
  147.  
  148.             // KAZAH TI CHE IMA SHABLON ( %02d )
  149.  
  150.             // tuka polzvame otverkata da otvurtim drug vid bolt4e
  151.  
  152.             time = String.format("%d:%02d hours", hours, minutes);
  153.         }
  154.  
  155.         // 7.
  156.         // if the difference is positive or equals to 0, this means that we are either right on time or earlier
  157.  
  158.         if (timeDifference <= 0) {
  159.  
  160.             //If we reached this point it means that the number is positive
  161.             // if the user arrives no more than 30 minutes earlier , it's on time
  162.  
  163.             // we use math abs here because we already established that the number is positive
  164.             // considering that we passed through the above if statement
  165.  
  166.             // this means that we are not late for sure
  167.             // so we need to use the timeDifference as a positive value instead
  168.  
  169.             // in our case every outcome between 1 - 30 should be printed
  170.  
  171.             // If we do not use math abs?
  172.             // the time difference will be calculated incorrectly
  173.  
  174.             if (Math.abs(timeDifference) <= 30) {
  175.                 System.out.println("On time");
  176.  
  177.                 //if the difference is 0 it means that we are right on time
  178.                 // !! WILL PRINT ONLY ON TIME
  179.  
  180.                 // if we arrive earlier than expected BUT no more than 30 minutes earlier
  181.                 // we are still on time BUT print the minutes difference of hour arrival earlier
  182.  
  183.                 if (timeDifference != 0) {
  184.  
  185.                     //if difference is anything but 0 and at the same time less than 30 we are still on time
  186.                     // However we need to print how many minutes before the exam we arrived.
  187.  
  188.                     System.out.printf("%s before the start", time);
  189.                 }
  190.  
  191.             } else {
  192.  
  193.                 // 8.
  194.                 // if we arrive more than 30 minutes before the exam - we print that its Early
  195.  
  196.                 System.out.printf("Early %s before the start", time);
  197.             }
  198.  
  199.         } else {
  200.  
  201.             // 9.
  202.             // if the difference is negative it means that we are late
  203.  
  204.             System.out.printf("Late %s after the start", time);
  205.         }
  206.     }
  207. }
  208.  
  209. //        int hours;
  210. //        int minutes;
  211. //
  212. //        hours = Integer.parseInt(scanner.nextLine());
  213. //        minutes = Integer.parseInt(scanner.nextLine());
  214. //
  215. //        int examMinutes = hours * 60 + minutes;
  216. //
  217. //        hours = Integer.parseInt(scanner.nextLine());
  218. //        minutes = Integer.parseInt(scanner.nextLine());
  219. //
  220. //        int arrivalMinutes = hours * 60 + minutes;
  221. //
  222. //        int timeDifference = arrivalMinutes - examMinutes;
  223. //
  224. //        hours = Math.abs(timeDifference / 60);
  225. //        minutes = Math.abs(timeDifference % 60);
  226. //
  227. //        String time;
  228. //        if (hours == 0) {
  229. //            time = String.format("%01d minutes", minutes);
  230. //        } else {
  231. //            time = String.format("%d:%02d hours", hours, minutes);
  232. //        }
  233. //
  234. //        if (timeDifference <= 0) {
  235. //            if (Math.abs(timeDifference) <= 30) {
  236. //                System.out.println("On time");
  237. //                if (timeDifference != 0) {
  238. //                    System.out.printf("%s before the start", time);
  239. //                }
  240. //
  241. //            } else {
  242. //                System.out.printf("Early %s before the start", time);
  243. //            }
  244. //
  245. //        } else {
  246. //            System.out.printf("Late %s after the start", time);
  247. //        }
  248. //    }
  249. //}
  250.  
  251. // bonus -> https://prnt.sc/cfNXai8Xbo2E -> extract the method
  252.  
  253. //    private static int parseTimeIntoMinutes(int hours, int minutes) {
  254. //        return hours * 60 + minutes;
  255. //    }
  256.  // int examMinutes = parseTimeIntoMinutes(hours, minutes);  --> BONUS / skip for now
  257. // int arrivalMinutes = parseTimeIntoMinutes(hours, minutes); --> BONUS / skip for now
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement