Created
February 14, 2020 03:51
-
-
Save minor/1854571b2d72132b23ccdc24d0b7d019 to your computer and use it in GitHub Desktop.
Professor Estrada: Part 3 Homework Assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.saurishsrivastava; | |
/** part 3: this program is used to take two time intervals and compare them with a series of calculations. */ | |
/** import used packages */ | |
import java.util.Scanner; | |
public class Intervals { | |
/** Main method */ | |
public static void main(String[] args) { | |
/** declare variables and initialize Scanner */ | |
Scanner input = new Scanner(System.in); // initialize the scanner | |
int early_start; // variable for user early start | |
int early_end; // variable for user early end | |
int late_start; // variable for user late start | |
int late_end; // variable for user late end | |
int final_early_end; // variable for calculated early end in minutes | |
int final_early_start; // variable for calculated early start in minutes | |
int final_late_end; // variable for calculated late end in minutes | |
int final_late_start; // variable for calculated late start in minutes | |
int early_difference_minutes; // variable for calculated difference in early end and start in minutes | |
int late_difference_minutes; // variable for calculated difference in late end and start in minutes | |
/** Prompt user input */ | |
System.out.print("Enter earlier start and end time as two 24-hour format times: "); // ask for earlier start and end times | |
early_start = input.nextInt(); // assign the first time to "early_start" variable | |
early_end = input.nextInt(); // assign the second time to "early_end" variable | |
System.out.print("Enter later start and end time as two 24-hour format times: "); // ask for later start and end times | |
late_start = input.nextInt(); // assign the second time to "late_start" variable | |
late_end = input.nextInt(); // assign the second time to "late_end" variable | |
/** Calculations of Early Start and End (minutes) */ | |
final_early_end = (((early_end / 100) * 60) + early_end % 100); // calculates the number of minutes in the early end time | |
final_early_start = (((early_start / 100) * 60) + early_start % 100); // calculates the number of minutes in the early start time | |
early_difference_minutes = final_early_end - final_early_start; // takes the early difference in minutes - subtraction of bigger time and smaller time | |
/** Calculations of Late Start and End (minutes) */ | |
final_late_end = (((late_end / 100) * 60) + late_end % 100); // calculates the number of minutes in the later end time | |
final_late_start = (((late_start / 100) * 60) + late_start % 100); // calculates the number of minutes in the later start time | |
late_difference_minutes = final_late_end - final_late_start; // takes the later difference in minutes - subtraction of bigger time and smaller time | |
/** ~Boolean methods~ */ | |
boolean isOverlap = (early_end > late_start || early_end > late_end); // used to determine if the intervals overlap | |
boolean isLonger = (early_difference_minutes > late_difference_minutes); // used to determine which interval is longer | |
/** ~Print intervals (minutes)~ */ | |
System.out.println("The earlier interval is " + early_difference_minutes + " minutes long."); // prints results | |
System.out.println("The later interval is " + late_difference_minutes + " minutes long."); // prints results | |
/** Print which interval is longer or equal */ | |
if (isLonger) { | |
System.out.println("The earlier interval is longer."); | |
} else if (!isLonger && early_difference_minutes == late_difference_minutes) { | |
System.out.println("The intervals are equally long."); // the boolean needs to be false and both the minutes need to be the same for this to be true | |
} | |
else { | |
System.out.println("The later interval is longer."); | |
} | |
/** Print if interval overlap */ | |
if (!isOverlap) { | |
System.out.println("These intervals do not overlap."); | |
} | |
else { | |
System.out.println("These intervals overlap."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment