Java time class
Ok this is homework and i am struggling a bit, i cant figure out what to pass in toString method and i am not quite sure my methods for getting the current and the elapsed time are correct. I then have to pass this to the main method and display the times in output statements. That is not开发者_JS百科 really my problem, i can only have 3 variables, how can i figure out the elapsed time from the current time and still hold the original hour, second, minute variables without changing them so all data from both time constructors are held in those variables? I hope i am making sense.Thanks for any input
package chapter_10;
public class Time {
private long hour;
private long minute;
private long second;
public void time() {
long second = System.currentTimeMillis();
long minute = System.currentTimeMillis();
long hour = System.currentTimeMillis();
}
public void setTime(long elapsedTime){
long millisecond = System.currentTimeMillis();
long second = millisecond / 1000;
long minute = second / 60;
long hour = minute /60;
}
public long getHour() {
return hour;
}
public long getMinute() {
return minute;
}
public long getSecond() {
return second;
}
public String toString(){
return // what should i return here, String.towhat?
}
}
Here is the assignment, just to clarify. Time class:
give it 3 private data members for hour, minute, and second. Use type long or int. If you use int you must cast inside the ctors. add a no-arg ctor that uses code like that in Listing 2.6 on p38 to assign values to hour, minute, and second from the current time. add another ctor that will take a single long parameter named elapseTime (better would be elapsedTime), a number for milliseconds since the Unix epoch date. this second ctor will also use code as per Listing 2.6 to set the data members for that elapsed time since the epoch. a third ctor is not necessary. add a getter for each data member. Each getter will require only a single statement. Getters are needed because the data members are private. add a toString method that returns the hours, minutes, and seconds for a Time object. main class:
in the main method create a Time object with the no-arg ctor. create a few time objects with the other ctor. display the times for each object you instantiate.
You aren't setting those private data members properly at all.
The values in the time method shadow the private data members.
Worry about that, then fix the toString().
I can't guarantee that I understand your questions completely/correctly. Have you considered storing the actual result of call to currentTimeMillis()
? You can then have three variables:
long startTime;
long endTime;
long elapsedTime;
and then extracting the relevant pieces when they are needed?
Well, toString generally is a String that represents your object in a unique way. Since your object here is Time, so an unique time is represented by it's time.
So, if you write something like:
public String toString(){
return getHour() + ":" + getMinute() + ":" + getSecond() + ":" + getMillisecond();
}
This would be a nice implementation of the tostring method, since this represents with success what is this object.
PS: Your millisecond nor all your fields are being stored in your class as a field! Check that out!
Edit: Now I noticed, your code won't work at all. The System.currentTimeMillis() returns time in UTC. That means, the calculation you are doing won't work at all. You must first convert from UTC to a valid millisecond approach. Consider using the Date/Calendar class for working with time.
精彩评论