Create a new Java class called: MilitaryTime public class MilitaryTime Write a J
ID: 3881130 • Letter: C
Question
Create a new Java class called: MilitaryTime
public class MilitaryTime
Write a Java application program that reads two times in military format (0900 and 1730 for example) from the keyboard. The program should then calculate and display the number of hours and minutes between the two times.
The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's values are shown below in italics (also note the extra line break before the final output):
Please enter the first time: 0900
Please enter the second time: 1730
The difference in the two times is 8 hour(s) and 30 minute(s).
Here is another example run of the program:
Please enter the first time: 1245
Please enter the second time: 1330
The difference in the two times is 0 hour(s) and 45 minute(s).
For simplicity of the assignment, you may assume that the first time entered will always be less than the second time. Also, as you see in the above example, you don't have to worry about whether the words hour and minute are plural or not if you simply put an 's' in parentheses behind the words.
Calculation Hint: When dividing integers, JAVA can give you both the quotient and the remainder from performing long division; however, these must be computed using two separate calculations. The forward slash mark ( / ) is the division operator and the percent sign ( % ) is the modulus operator, which returns the remainder.
For example: If you divide 5 by 3, you get a quotient of 1, with a remainder of 2.
So if you if perform the calculation: 5/3 you will get the answer 1 (which is the quotient)
and if you if perform the calculation: 5%3 you will get the answer 2 (which is the remainder)
Explanation / Answer
MilataryTime.java
import java.util.Scanner;
public class MilataryTime {
public static void main(String[] args) {
//Declaring variables
int first, second, dhrs, dmins, fhrs, fmins, shrs, smins;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Please enter the first time:");
first = sc.nextInt();
System.out.print("Please enter the second time:");
second = sc.nextInt();
fhrs = first / 100;
fmins = first % 100;
shrs = second / 100;
smins = second % 100;
//Calculating the difference between timings
dhrs = shrs - fhrs;
dmins = smins - fmins;
if (dmins < 0) {
dhrs -= 1;
dmins = 60 + dmins;
}
//Displaying the output
System.out.println("The difference in the two times is " + dhrs + " hour(s) and " + dmins + " minute(s).");
}
}
________________
Output#1:
Please enter the first time:0900
Please enter the second time:1730
The difference in the two times is 8 hour(s) and 30 minute(s).
______________
Output#2:
Please enter the first time:1245
1Please enter the second time:330
The difference in the two times is 0 hour(s) and 45 minute(s).
_______________Could you plz rate me well.Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.