Created
March 3, 2014 11:01
-
-
Save bitwiser/9322786 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.util.*; | |
public class TimeComparer | |
{ | |
public static void main(String[] args) | |
{ | |
Scanner in = new Scanner(System.in); | |
System.out.println("Please enter the first time in military time (hour minute): "); | |
int hour1 = in.nextInt(); | |
int minute1 = in.nextInt(); | |
System.out.println("Please enter the second time in military time (hour minute): "); | |
int hour2 = in.nextInt(); | |
int minute2 = in.nextInt(); | |
Time time1 = new Time(hour1, minute1); | |
Time time2 = new Time(hour2, minute2); | |
String comp = time1.compareWith(time2); | |
System.out.println("The first time is " + comp + " the second time."); | |
} | |
} | |
/** | |
This class compares two military times and | |
determines which comes first. | |
*/ | |
class Time | |
{ | |
private int hour; | |
private int minute; | |
public Time(int anHour, int aMinute) | |
{ | |
this.hour = anHour; | |
this.minute = aMinute; | |
} | |
public int getHour(){ | |
return this.hour; | |
} | |
public int getMin(){ | |
return this.minute; | |
} | |
/** | |
Compares this time against another time. | |
@param time2 the time to compare with | |
@return "before" if this time comes before time2, | |
"" if the times are the same, and "after" otherwise | |
*/ | |
public String compareWith(Time time2) | |
{ | |
String str = ""; | |
if(this.hour<time2.getHour()){ | |
str = "before"; | |
}else if(this.hour==time2.getHour()){ | |
if(this.minute<time2.getMin()){ | |
str = "before"; | |
}else if(this.minute==time2.getMin()){ | |
str = ""; | |
}else{ | |
str = "after"; | |
} | |
}else | |
str = "after"; | |
return str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment