Here is what I need to do.
Write a program that reads two times in military format (0940, 1705) and prints the number of hours and minutes between the two times. Here is a sample run.
Please enter the first time: 0940
Please enter the second time: 1705
7 hours 25 minutes
Use a Time class with a constructor that takes a military time as a parameter and a method difference that takes a Time object as a parameter and returns a Time object.
Note: The times should be input in the order in which they occur. For example, for 0940 on Monday and 1705 on Monday, the first time is 0940, and the second time is 1705. For 1705 on Monday and 0940 on Tuesday, the first number is 1705, and the second number is 0940.
Hint: To account for negative time differences, as in the second instance described above, add one day (24 * 60 minutes) to the difference and then find the remainder that results from dividing the result by the number of minutes in a day. That is, timeDifference = (timeDifference + MINUTES_PER_DAY) % MINUTES_PER_DAY. Note that this calculation will not make a difference in a case of a positive time difference, but it will make the proper adjustment in a case of a negative time difference.
PSEUDO-CODE:
Create a class Time
Declare instance fields hours and minutes
Constructor:
takes in a time parameter as an integer (military time between 000 and 2400)
set hours instance field as time divided by 100
set minutes instance field as time modulus 100
Get hours method:
Return hours instance field
Get minutes method:
Return minutes instance field
Difference method (takes in a Time object which is end time and returns a Time object which contains diff hours and minutes):
Create a time object (diff), passing in 0
startMinutes is this object hours * 60 + minutes
endMinutes is end object hours * 60 + minutes
timeDiff is endMinutes - startMinutes
convert timeDiff per timeDifference formula provided in hints
diff object hours is timeDiff divided by 60
diff object minutes is timeDiff modulus 60
return the diff object
End Time class
Create a TimeDriver class:
In main:
Create a scanner object
Prompt for and capture the first time in military format (between 000 and 2400)
Do the same for military time2
Create a start time object and an end time object, passing in the time1 and time2 values respectively
Call the difference method on the start time object, passing in the end time object
Display the time difference as a call to get hours and get minutes on the diff object that was the return variable on the call to the difference method
End TimeDriver class
Example program runs:
Please enter the first time:
800
Please enter the second time:
1333
5 hours 33 minutes
Please enter the first time:
1333
Please enter the second time:
800
18 hours 27 minutes
This is what I have to do.
Write a program that reads two times in military format (0940, 1705) and prints the number of hours and minutes between the two times. Here is a sample run.
Please enter the first time: 0940
Please enter the second time: 1705
7 hours 25 minutes
Use a Time class with a constructor that takes a military time as a parameter and a method difference that takes a Time object as a parameter and returns a Time object.
Note: The times should be input in the order in which they occur. For example, for 0940 on Monday and 1705 on Monday, the first time is 0940, and the second time is 1705. For 1705 on Monday and 0940 on Tuesday, the first number is 1705, and the second number is 0940.
Hint: To account for negative time differences, as in the second instance described above, add one day (24 * 60 minutes) to the difference and then find the remainder that results from dividing the result by the number of minutes in a day. That is, timeDifference = (timeDifference + MINUTES_PER_DAY) % MINUTES_PER_DAY. Note that this calculation will not make a difference in a case of a positive time difference, but it will make the proper adjustment in a case of a negative time difference.
PSEUDO-CODE:
Create a class Time
Declare instance fields hours and minutes
Constructor:
takes in a time parameter as an integer (military time between 000 and 2400)
set hours instance field as time divided by 100
set minutes instance field as time modulus 100
Get hours method:
Return hours instance field
Get minutes method:
Return minutes instance field
Difference method (takes in a Time object which is end time and returns a Time object which contains diff hours and minutes):
Create a time object (diff), passing in 0
startMinutes is this object hours * 60 + minutes
endMinutes is end object hours * 60 + minutes
timeDiff is endMinutes - startMinutes
convert timeDiff per timeDifference formula provided in hints
diff object hours is timeDiff divided by 60
diff object minutes is timeDiff modulus 60
return the diff object
End Time class
Create a TimeDriver class:
In main:
Create a scanner object
Prompt for and capture the first time in military format (between 000 and 2400)
Do the same for military time2
Create a start time object and an end time object, passing in the time1 and time2 values respectively
Call the difference method on the start time object, passing in the end time object
Display the time difference as a call to get hours and get minutes on the diff object that was the return variable on the call to the difference method
End TimeDriver class
Example program runs:
Please enter the first time:
800
Please enter the second time:
1333
5 hours 33 minutes
Please enter the first time:
1333
Please enter the second time:
800
18 hours 27 minutes
Here is the code:
**
* This take a Military time and returns a difference between
* the start and end time.
* @author (Jim Strohl
* @version 1
*/
public class Time
{
// Instance fields (variables)
private int hours;
private int minutes;
// Constructor method
public Time (int military)
{
hours = military/100;
minutes = military%100;
}
public int getHours()
{
return hours;
}
public int getMinutes()
{
return minutes;
}
public Time difference(Time military)
{
int startMinutes = hours * 60 + minutes;
int endMinutes = military.getHours() * 60 + military.getMinutes();
int timeDiff = endMinutes - startMinutes;
int timeDifference = (timeDiff + 1440) % 1440;
int diffHours = timeDiff/60;
int diffMinutes = timeDiff%60;
Time atime = new Time(0);
atime.hours = diffHours;
atime.minutes =diffMinutes;
return atime;
}
}
Here is the Time Driver code. This drives the program and comes up with a difference .
import java.util.Scanner;
/**
* This takes the Time program and test's it.
*
* @author Jim Strohl
* @version 1
*/
public class TimeDriver
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter military start time: ");
int t1 = in.nextInt();
System.out.print("Enter military end time: ");
int t2 = in.nextInt();
Time start = new Time(t1);
Time end = new Time(t2);
Time diff = start.difference(end);
System.out.println(diff.getHours());
System.out.println(diff.getMinutes());
The only thing that I get out when tested is : Enter military start time: 940.
I expect to get the start time and end time and the difference. Example 800 - 1333 = 5 hours 33 minutes.
Then when entering the a time of 1333 - 800 = 18 hours 27 minutes. The code needs to keep it from going negative.
Message was edited by:
Gem
> Here is what I need to do.
Okay, that's the assignment. Here's what was asked of you:
"Post some code along with whats going wrong."
If you haven't started yet, just start by writing an empty class named Time (as per your assignment). Get it to compile. Once you've completed that step, move onto the next step (i.e., declaring 'hours' and 'minutes' instance fields). Make sure it compiles. Move on to the next step (i.e., write a constructor). Make sure it compiles. Write a small test class that you can run that creates a Time object using the constructor you just wrote. Make sure the constructor works. Move on to the next step (i.e., write a "get hours" method). Make sure it compiles. In you test class, test the "get hours" method you just wrote. Make sure it works.
Hopefully you're seeing a pattern by now. Let us know when you hit a snag. When you do, post your code, your test class, what you expect, what is actually happening, and any other relevant details.
Best of luck!
~
> Then when entering the a time of 1333 - 800 = 18 hours 27 minutes. The code needs to keep it from going negative.
Okay. If you want to do that, you'll need to write some sort of a conditional statement. I presume you know how to use an "if" statement. If not, let us know and we can point you in the right direction.
But I don't see what you want to accomplish by keeping "it" from "going negative". I don't understand what you mean by that. If you start at 1333 and end at the next occurring 0800, the time elapsed will have been 18 hours and 27 minutes.
~