Time Class - military time format & standard format - PLZ HELP!!!!

Hi everyone,

I am enw to programming and stuch on this project... I need to create a Time class that represents time in military and standard. Can you go voer my code any figure out whats wrong? i cant get the universal String...

publicclass Time

{

privateint hour;

privateint minute;

privateint second;

/** Default constructor */

public Time(){

hour = 0;

minute = 0;

second = 0;

}

public Time(int h,int m,int s){

hour = h;

minute = m;

second = s;

}

/*

// I dont know what the IML diagram wants me to do here***

//it says +Time(t:Time) //set time to t

public Time(t) {

hour = this.hour();

minute = this.minute();

second = this.minute();

}

*/

publicvoid setHour(int h){

if (h >= 0 && h <= 23)

hour = h;

}

publicvoid setMinute(int m){

if (m >= 0 && m <= 59)

minute = m;

}

publicvoid setSecond(int s){

if (s >= 0 && s <= 59)

second = s;

}

publicint getHour(){

return hour;

}

publicint getMinute(){

return minute;

}

publicint getSecond(){

return second;

}

//increments time by one second

//

publicvoid tick()

{

if(second!=59)

second++;

else

{

second = 0;

if(minute!=59)

minute++;

else

{

minute = 0;

if(hour !=23)

hour++;

else

{

hour = 0;

}

}

}

}

//format is hh:mm:ss

public String toUniversalString()

{

System.out.println(hour +":" + minute +":" + second);

}

//format is hh:mm:ss am(pm)

public String toString()

{

boolean am =true;

int tempHour = 0;

if(hour>12)

{

tempHour = hour - 12;

am =false;

}

if(am)

System.out.println(tempHour +":" + minute +":" + second +" (am)");

else

System.out.println(tempHour +":" + minute +":" + second +" (pm)");

}

}

[5098 byte] By [Ayanik7a] at [2007-11-27 2:11:53]
# 1
Your toString() method doesn't return a String, as it is supposed to. Instead it writes a String to the console.
DrClapa at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 2
When you compiled this, did you get the error "cant get the universal String" from the compiler. If not, why did you think keeping the actual error message a secret was a good idea?
BillKriegera at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 3

public String toString()

{

boolean am = true;

int tempHour = 0;

if(hour>12)

{

tempHour = hour - 12;

am = false;

}

if(am)

System.out.println(tempHour + ":" + minute + ":" + second + " (am)");

else

System.out.println(tempHour + ":" + minute + ":" + second + " (pm)");

}

This is incorrect. You may wish to rethink how you set am/pm.

jjhusa01a at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 4
> is incorrect. You may wish to rethink how you set> am/pm.What's wrong in the calculation?Kaj
kajbja at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 5
1200(noon) is pm, not am.0000 - 1159am| 1200 - 2359pmMessage was edited by: jjhusa01
jjhusa01a at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 6
> What's wrong in the calculation?In the am/pm version of time naming, the hours go 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 in the AM and the same again in the PM.
DrClapa at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 7

DrClap is correct.

The hour is being saved in military time. Therefore at 1200 hours which is noon, will return false for is 12>12.

This will keep am=true and given you a time on 12:00am for Noon, when that is midnight.

You need to make at least 2 checks for time.

if(hour > 12){

temphour = hour - 12;

am = false;

}else if(hour == 12){

temphour = hour;

am = false;

}

Message was edited by:

jjhusa01

jjhusa01a at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 8
> > What's wrong in the calculation?> > In the am/pm version of time naming, the hours go 12,> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 in the AM and the> same again in the PM.Ok. Thanks
kajbja at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 9
Also got 1 question. This "Tick" function doesn't seem to work off/sync with any type of clock function. Is this program supposed to keep actual time?
jjhusa01a at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 10
Another OT question. Why is 24h clock called military time? Is that only in the US?(Ok, I could have googled)
kajbja at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 11
> Another OT question. Why is 24h clock called military> time? Is that only in the US?I only ever hear the term from Americans.
DrClapa at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 12

> > Another OT question. Why is 24h clock called

> military

> > time? Is that only in the US?

>

> I only ever hear the term from Americans.

Basically all of the US use the AM/PM time format, except for the US Military. Since the Military is on a different than the rest of us, it is 'their' time format.

I am not sure why it has distinguished itself so much. I could guess it may have to do with movies and television; since that is probably the most exposure most Americans get to military life.

Message was edited by:

jjhusa01

jjhusa01a at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 13

> > > Another OT question. Why is 24h clock called

> > military

> > > time? Is that only in the US?

> >

> > I only ever hear the term from Americans.

>

> Basically all of the US use the AM/PM time format,

> except for the US Military. Since the Military is on

> a different than the rest of us, it is 'their' time

> format.

>

> I am not sure why it has distinguished itself so

> much. I could guess it may have to do with movies and

> television; since that is probably the most exposure

> most Americans get to military life.

>

> Message was edited by:

> jjhusa01

But Americans knows that military time is the same as 24h clock?

kajbja at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 14
Yes. However a large number I am sure don't. Those that do not know, probably didn't know you could count hours past 12. You can't force the ignorant to learn.
jjhusa01a at 2007-7-12 2:05:34 > top of Java-index,Java Essentials,Java Programming...
# 15

thanks for the replies i figured it out!!

Everyone, at least the people it talk to, in the US knows there are 2 formats. Standard that goes up to 12 nad military up to 24. I guess military goes up to 24 because the military goes all over the world and need type of time that is similar to the rest of the world.

Like how rest of the world uses metric system and only US has their own system.....

Ayanik7a at 2007-7-21 20:20:55 > top of Java-index,Java Essentials,Java Programming...
# 16
Goooooooood morning Vietnam! It's 0600 hours. What does the "O" stand for? O my God, it's early!
jjhusa01a at 2007-7-21 20:20:55 > top of Java-index,Java Essentials,Java Programming...
# 17

> thanks for the replies i figured it out!!

>

> Everyone, at least the people it talk to, in the US

> knows there are 2 formats. Standard that goes up to

> 12 nad military up to 24. I guess military goes up to

> 24 because the military goes all over the world and

> need type of time that is similar to the rest of the

> world.

>

> Like how rest of the world uses metric system and

> only US has their own system.....

Basically, there are 24 time zones for 24 hours. Each timezone is 15degrees wide at the equator. 15*24 = 360 or 1 full rotation.

jjhusa01a at 2007-7-21 20:20:55 > top of Java-index,Java Essentials,Java Programming...