Advertisement
GabrielHr00

08. On Time for the Exam

Mar 24th, 2024
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. package _03_ConditionalStatementsAdvanced;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class OnTimeForTheExam {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int examHour = Integer.parseInt(scanner.nextLine());
  9.         int examMinutes = Integer.parseInt(scanner.nextLine());
  10.         int arrivalHour = Integer.parseInt(scanner.nextLine());
  11.         int arrivalMinutes = Integer.parseInt(scanner.nextLine());
  12.  
  13.         // calc exam time in minutes => (easier when working only with minutes)
  14.         int timeForExam = (examHour * 60) + examMinutes;
  15.         int timeForArrival = (arrivalHour * 60) + arrivalMinutes;
  16.  
  17.         // get difference between arrival time and exam time
  18.         int diff = Math.abs(timeForArrival - timeForExam);
  19.  
  20.         // check if student is late (when timeForArrival > timeForExam)
  21.         if(timeForArrival > timeForExam) {
  22.             System.out.println("Late");
  23.  
  24.             // more than 1 hour late for the exam
  25.             if(diff >= 60) {
  26.                 // convert back into hours/minutes
  27.                 int hour = diff / 60;
  28.                 int minutes = diff % 60;
  29.                 // %02d -> check if minutes are < 10, then insert 0 infront, if not - don't do anything
  30.                 System.out.printf("%d:%02d hours after the start", hour, minutes);
  31.             } else{
  32.                 System.out.printf("%d minutes after the start", diff);
  33.             }
  34.         }
  35.         // check for arrival on time or earlier, but not more than 30 mins
  36.         else if(timeForExam == timeForArrival || diff <= 30) {
  37.             System.out.println("On Time");
  38.  
  39.             // check for minutes before the start
  40.             if(diff >= 1 && diff <= 30) {
  41.                 System.out.printf("%d minutes before the start", diff);
  42.             }
  43.         }
  44.         else {
  45.             System.out.println("Early");
  46.             // more than 1 hour early for the exam
  47.             if(diff >= 60) {
  48.                 // convert back into hours/minutes
  49.                 int hour = diff / 60;
  50.                 int minutes = diff % 60;
  51.                 // %02d -> check if minutes are < 10, then insert 0 infront, if not - don't do anything
  52.                 System.out.printf("%d:%02d hours before the start", hour, minutes);
  53.             } else{
  54.                 System.out.printf("%d minutes before the start", diff);
  55.             }
  56.         }
  57.  
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement