Question:
Time Class in Java(20 characters)?
anonymous
2009-09-27 11:50:17 UTC
OK, so I'm supposed to create a time class for a java assignment. This is what it needs:
-Data fields hour, minute, and second that represent a time.<---got this one

-A no-arg constructor that creates a Time object for the current time.(the data fields value will represent the current time)<---pretty sure I got this one covered

-A constructor that constructs a Time object with a specified elapsed time since midnight, Jan 1, 1970, in milliseconds, (the data fields value will represent the time) <---- most confused about this

-Three get methods for the data fields<----got this one down

Then I am supposed to write a program that creates two time objects Time() and Time(555550000) and display their hour, minute and second. For the Time(555550000) I have it displaying correctly(10 hours, 19 minutes, 10 seconds). Not sure about the Time(). Here is what I have so far.



public class ex9_1 {
public static void main(String[] args){
Time timetest= new Time(555550000);
System.out.println(timetest.getHour());
System.out.println(timetest.getMinute());
System.out.println(timetest.getSecond());
Time timetest2= new Time();
System.out.println(timetest2.getHour());
System.out.println(timetest2.getMinute());
System.out.println(timetest2.getSecond());


}
}
class Time{
private long hour;
private long minute;
private long second;
private long realhour;
private long realminute;
private long realsecond;
public Time(){
second=System.currentTimeMillis()/1000;
minute=second/60;
hour=minute/60;
realsecond=second%60;
realminute=minute%60;
realhour=hour%24;

}
Time(long millisecond){
second=millisecond/1000;
minute=second/60;
hour=minute/60;
realsecond=second%60;
realminute=minute%60;
realhour=hour%24;
}

public long getHour(){
return this.realhour;
}
public void setHour(long newHour){
this.hour=newHour;
}
public long getMinute(){
return this.realminute;
}
public void setMinute(long newMinute){
this.minute= newMinute;
}
public long getSecond(){
return this.realsecond;
}
public void setSecond(long newSecond){
this.second= newSecond;
}
}


I'm mostly confused about what it is asking for in the time since 1970 thing. Not exactly sure what it is even asking(does it want a separate constructor that is never even called in the test program?). Any feedback is appreciated.
Three answers:
Mark aka jack573
2009-10-01 01:41:36 UTC
OK, from your code, you have some other errors.



You have a second and a realSecond and the same for the hours and minutes.

private long hour;

private long minute;

private long second;

private long realhour;

private long realminute;

private long realsecond;



In your set methods, you are setting the minutes, or seconds, etc, but in your get methods, you are returning the realMinutes, realSeconds, etc.



You should only have one set of these. Whether you want to call them realSeconds or seconds it does not really matter, but oyu should have them as seconds, minutes, hours though.



I understand that you are using the seconds, etc in the two constructors. You should do that differently.



Get rid of all the realSecond, realHour, etc. and just have the second, minute, hour. Now in your constructors, where you have second = blah, hours = blah, minute = blah, you should have

long s = blah;

long m = blah;

long h = blah;



Then use s, m, and h for the realSeconds, realMinutes, realHours, which are now called seconds, minutes, hours.

second = s % 60;

minute = m % 60;

hour = h % 24;



Then change you get methods to return second, minute, hour and not realSecond, realMinute, realHour.



You should have them as a plural seconds, as opposed to second, it describes what you have better, but since your requirements say that you need to have them in singular fowm, leave them alone.



he way you are creating the Time() is good. For what you probably don't understand, or do now since you have other answers is that the System.currentTimeMillis() does get you the current time in milliseconds from Jan 1 1970.



So apart from changing your variable names and the way the contructors and methods work, you are right with the rest.
jackkirby
2009-09-27 19:09:01 UTC
If you call Date.getTime(), you are returned a long that represents the number of milliseconds since Jan. 1, 1970. Read the JavaDoc for the Date class (http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html), it should tell you everything you need to know. Create a Date object and then you can pull the other items from it. There are methods already there. Good luck.
_anonymous_
2009-09-27 19:09:40 UTC
the 1970 thing is called the "UNIX epoch." Java keeps times as the number of milliseconds after midnight on Jan 1, 1970. it's a 64-bit integer so it doesn't break down in 2038 (it's the next Y2K, read about it: http://en.wikipedia.org/wiki/Year_2038_problem )



tip: all of these functions can be based on the java.util.Date and java.util.GregorianCalendar functions. read the javadoc:



Date class -->http://java.sun.com/javase/6/docs/api/java/util/Date.html

GregorianCalendar class -->http://java.sun.com/javase/6/docs/api/java/util/Scanner.html


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...