Reputation: 5
I have to make a class named "Time" with the following
Currently I think I have a solid method down to convert the milliseconds obtained from System.currentTimeMillis() to hours/minutes/seconds. As '(System.currentTimeMillis() / 1000*60*60)) % 24 would be the hours (System.currentTimeMillis() / 1000*60)) % 60 would be minutes (System.currentTimeMillis() / 1000) % 60 would be seconds`
I'm currently confused on how to implement this into the assignment, I know its a broad question asking for some sort of guidelines that might steer in me into the right direction because I'm not very familiar with objects and feel pretty lost at the moment. Any help is greatly appreciated. I'll update the post if I can if I make any progress.
Upvotes: 0
Views: 1743
Reputation: 6178
The no-arg constructor takes System.currentTimeMillis() and invokes the second constructor using
this()
. This other constructor in turn, calls the setTime()
method. This is the method that extracts the second, minutes, and hours from that long
argument (elapsed time). The third constructor simply sets the hour, minute, and second data members. There are three getter methods as you can see, and a toString method that creates a String in the HH:MM:SS format. This should be more than enough information for you to complete the assignment.
Upvotes: 1
Reputation: 1722
First of all, break the problem into small parts. Are you comfortable creating a class with the three fields and getters/setters for them? Then do that first.
Then you can add a setTime(long) method to the class. In that class, use your method to calculate hours, minutes and seconds like you described (I didn't check if your method looked like it was going to work correctly but let's just assume that). Once you have the hours, minutes and seconds in variables inside the setTime(long) method, you can call your setHours(), setMinutes() and setSeconds() methods that you previously created.
Once you have that method, then you can create your constructors. The millisecond constructor can just call your setTime() method. The parameterless one can call System.currentTimeMillis() and use the output of that to call setTime(). The one that takes hours, minutes and seconds can simply just call the setXXX() methods for your fields directly.
Upvotes: 1