Hardest problem iv'e tried to fix!

Hi again, i need help please.

I am trying to get the time to print in military time.

I am constructing methods to call on in order to get it to print.

I have no problems compiling and running but i am having trouble setting a format.

What i am suppose to do is post time like this hh:mm:ss. But when the user for example enters 8 hours 7 minutes and 5 seconds

for example my code is printing out like this h:m:s instead of 0h:0m:0s. I have already made a method with an if statement to add a zero in front of numbers with 1 digit but it does not work. Here is the code if you are confused!!

publicclass MilitaryTime

{

privateint hour;

privateint minute;

privateint second;

public MilitaryTime()

{

hour=0;

minute=0;

second=0;

}

public MilitaryTime(int hour,int minute,int second)

{

this.hour=hour;

this.minute=minute;

this.second=second;

}

public MilitaryTime(Time t)

{

}

publicvoid setHour(int h)

{

hour=h;

if(hour<=9)

{

hour=0+hour;

}

}

publicvoid setMinute(int m)

{

minute=m;

if(minute<=9)

{

minute=0+minute;

}

}

publicvoid setSecond(int s)

{

second=s;

if(second<=9)

{

second=0+second;

}

}

publicint getHour()

{

return hour;

}

publicint getMinute()

{

return minute;

}

publicint getSecond()

{

return second;

}

publicvoid tick()

{

second=second+1;

}

public String toUniversalString()

{

String s;

return s=hour+":"+minute+":"+second;

}

public String toString()

{

setHour(hour);

setMinute(minute);

setSecond(second);

String s;

return s=hour+":"+minute+":"+second;

}

}

These are the class which has the methods, here is my main program (not finished yet):

import javax.swing.*;

publicclass TestMilitaryTime

{

publicstaticvoid main(String[]args)

{

MilitaryTime time=new MilitaryTime(2,1,3);

System.out.println(time.toString());

}

}

Please Please help

Thanks

MS

[4995 byte] By [Progr@mera] at [2007-11-27 1:24:22]
# 1

hour=0+hour;

This won't do anything. You could put something like this into your toString() method.

String hourString = (hour < 10) ? "0"+hour : String.valueOf(hour);

//do same for minutes and seconds

return hourString + ":" + minuteString + ":" + secondString;

CaptainMorgan08a at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 2

> hour=0+hour;

> This won't do anything. You could put something like

> this into your toString() method.

> String hourString = (hour < 10) ? "0"+hour :

> String.valueOf(hour);

>

> //do same for minutes and seconds

>

> return hourString + ":" + minuteString + ":" +

> secondString;

I just got home from, school sory i responded to late.

I will be back in 30 mins to finish this up and try your suggestion. Thanks and look forward to seeiung me in the next 30 mins.

Progr@mera at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 3
> look forward to seeiung me in the next 30 mins.Words fail to describe how excited I am.
floundera at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 4

If i do understand clearly your needs, i think your toString method should look like this:

public String toString()

{

StringBufferbuffer;

buffer = new StringBuffer();

// add the hour (with leading zero if its neccesary)

if(this.hour < 10) buffer.append("0");

buffer.append(this.hour).append(":");

// add the minute (with leading zero if its neccesary)

if(this.minute < 10) buffer.append("0");

buffer.append(this.minute).append(":");

// add the second (with leading zero if its neccesary)

if(this.second < 10) buffer.append("0");

buffer.append(this.second).append(":");

return buffer.toString();

}

I also think that the toUniversalString method its unnecessary, due you have the implementation on the toString method.

And finally because the hour, minute and second properties are string the add a zero (0) to those values has no effect on the sets methods.

Hope its helps.

Greetings

frederidpa at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 5
> > look forward to seeiung me in the next 30 mins.> > Words fail to describe how excited I am.LOOOOL you should've been a comedian not a programmer.
Progr@mera at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 6

> If i do understand clearly your needs, i think your

> toString method should look like this:

>

> > public String toString()

> {

> StringBufferbuffer;

>

> buffer = new StringBuffer();

>

> // add the hour (with leading zero if its

> s neccesary)

> if(this.hour < 10) buffer.append("0");

> buffer.append(this.hour).append(":");

>

> // add the minute (with leading zero if its

> s neccesary)

> if(this.minute < 10) buffer.append("0");

> buffer.append(this.minute).append(":");

>

> // add the second (with leading zero if its

> s neccesary)

> if(this.second < 10) buffer.append("0");

> buffer.append(this.second).append(":");

>

> return buffer.toString();

> }

>

>

> I also think that the toUniversalString method

> its unnecessary, due you have the implementation on

> the toString method.

> And finally because the hour, minute and second

> properties are string the add a zero (0) to those

> values has no effect on the sets methods.

>

> Hope its helps.

>

> Greetings

Thanks i will try that right now and let you know if it works.

Progr@mera at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 7
Ok cool that worked, but how would i check to see if it is AM or PM after the hh:mm:ss? Would i make an if statement for every possibility or just put it in a loop?
Progr@mera at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 8
There is no AM or PM in military time.
floundera at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 9

> There is no AM or PM in military time.

Yes, your right but i have to do this universal time and military time.

Also i have another question. If a user enters anything out of range i have to set that specific input zero. For example if the user enters 25:11:12 i have to output 00:11:12. I already made if statements in my code but when i try to call it in anohter class it says void type not allowed here. This is what i have so far.....

public class MilitaryTime

{

private int hour;

private int minute;

private int second;

public MilitaryTime()

{

hour=0;

minute=0;

second=0;

}

public MilitaryTime(int hour, int minute, int second)

{

this.hour=hour;

this.minute=minute;

this.second=second;

}

public MilitaryTime(MilitaryTime t)

{

}

public void setHour(int h)

{

if(hour>=24)

{

hour=00;

}

else

{

hour=h;

}

}

public void setMinute(int m)

{

if(minute>=60)

{

minute=00;

}

else

{

minute=m;

}

}

public void setSecond(int s)

{

if(second>=60)

{

second=00;

}

else

{

second=s;

}

}

public int getHour()

{

return hour;

}

public int getMinute()

{

return minute;

}

public int getSecond()

{

return second;

}

public void tick()

{

second=second+1;

}

public String toUniversalString()

{

String s;

return s=hour+":"+minute+":"+second;

}

public String toString()

{

StringBufferbuffer;

buffer = new StringBuffer();

// add the hour (with leading zero if its neccesary)

if(this.hour < 10)

buffer.append("0");

buffer.append(this.hour).append(":");

// add the minute (with leading zero if its neccesary)

if(this.minute < 10)

buffer.append("0");

buffer.append(this.minute).append(":");

// add the second (with leading zero if its neccesary)

if(this.second < 10)

buffer.append("0");

buffer.append(this.second).append(":");

return buffer.toString();

}

}

And here is the main method i am using to invoke that class. But i do not understand why is doesn't let me setHour when i invoke it below.

import javax.swing.*;

public class TestMilitaryTime

{

public static void main(String[]args)

{

MilitaryTime time= new MilitaryTime(24,1,3);

System.out.println(time.setHour(24)); //<<<<Here how do i set time to get to my class method without an error?

System.out.println(time.toString());

}

}

Any ideas?

Thanks again...

Message was edited by:

Progr@mer>

Progr@mera at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 10
System.out.println(time);No need to add the toString.> why is doesn't let me setHour when i invoke it below.I don't see anywhere in your code where you invoke this method.Edited you post as I was replying I see.Message was edited by: flounder
floundera at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 11
if(hour >= 24)Think about your logic here.
floundera at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 12

> > if(hour >= 24)

>

> Think about your logic here.

Well as far as i can see hour can't be 24. it can only be 23:59:59 right?

If it is 24 or more then the instructions say set it to 0.

I don't see how the logic is wrong can u explain please?

Progr@mera at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 13

> > System.out.println(time);

>

> No need to add the toString.

>

> > why is doesn't let me setHour when i invoke it

> below.

>

> I don't see anywhere in your code where you invoke

> this method.

>

> Edited you post as I was replying I see.

>

> Message was edited by:

> flounder

Ohh ya sory i had to edit it i forgot to repaste the invoktion, lol.

Progr@mera at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 14

System.out.println(time.setHour(24));

Your method is void so it doesn't return anything to print. But more importantly why are you trying to print?

public void setHour(int h) {

if(hour>=24) {

hour=00;

} else {

hour=h;

}

}

Say my hour is 5 and I call setHour(42) so h = 42. Trace through your program and tell me what value hour will have at the end of the method.

floundera at 2007-7-12 0:15:27 > top of Java-index,Java Essentials,New To Java...
# 15

> System.out.println(time.setHour(24));

> Your method is void so it doesn't return anything to

> print. But more importantly why are you trying to

> print?

>

> public void setHour(int h) {

>if(hour>=24) {

>hour=00;

> } else {

> hour=h;

>

>

> Say my hour is 5 and I call setHour(42) so h = 42.

> Trace through your program and tell me what value

> hour will have at the end of the method.

Hour should equal 0 right? It doesn't make sense to me if i can't call on the method or use it then why do i make its void? If i can't get in to that method then what use is it to me?

Please explain man im losing it!!

P.S. your earning your Duke points its already your lol

Progr@mera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 16
For example if a user enter 42 for the hour just like you said, how should i call on a method to check if 42 is a valid hour? The set hour is void so i can't use it, but that is where i made the if statement. How do i use that?
Progr@mera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 17

NO NO NO NO NO! Actually trace through the code, look at the statements and execute them.Don't just go "it should be 0" because that is what you are expecting to happen. If that is beyond you, compile and run this.

class TimeWarp {

public static void main(String[] args) {

int hour = 5;

int h = 42;

if(hour >= 24) {

hour = 0;

} else {

hour = h;

}

System.out.println(hour);

}

}

floundera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 18
Some logic likeif(valid) change;else ;//do nothing, or scold the user by printlnThe "valid" expression is already an internal check.
Icycoola at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 19

Ok i see waht your saying..

is this correct?

public void setHour(int h)

{

if(h>=24)

{

h=00;

}

else

{

hour=h;

}

}

public void setMinute(int m)

{

if(m>=60)

{

m=00;

}

else

{

minute=m;

}

}

public void setSecond(int s)

{

if(s>=60)

{

s=00;

}

else

{

second=s;

}

}

I hope it is or ill jump off the bridge i'm typing on :))

Progr@mera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 20
Have you tried it? That will let you know if it is correct. I give you a hint, I originally told you your logic in the if statement was incorrect, nothing else!
floundera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 21

I ran your code and saw what i did wrong, and changed it, so did i do it corect this time?

public void setHour(int h)

{

if(h>=24)

{

h=00;

}

else

{

hour=h;

}

}

public void setMinute(int m)

{

if(m>=60)

{

m=00;

}

else

{

minute=m;

}

}

public void setSecond(int s)

{

if(s>=60)

{

s=00;

}

else

{

second=s;

}

}

If not can you tell me how to call on this setHour thing in order for me to check if it works and work it out? I tried to do

System.out.println(setHour());

But it gave me an error.

Please man help!

Progr@mera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 22

*sigh*

Must I start yelling at you?

I repeat: your setHour method returns nothing to print but why are you trying to print anything?

You setHour method was almost correct except that I pointed out the error in your logic. You have now fixed this but you also made other changes to the method which are wrong. Why did you do that?

floundera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 23
Please for the love of god stop typing random crap and wondering why it doesn't work and then pasting it here and expecting us to debug it for you. You need to think about what your are coding and learn to debug your own code.
floundera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 24

> *sigh*

>

> Must I start yelling at you?

>

> I repeat: your setHour method returns nothing to

> print but why are you trying to print anything?

>

> You setHour method was almost correct except that I

> pointed out the error in your logic. You have now

> fixed this but you also made other changes to the

> method which are wrong. Why did you do that?

I understand your frustration and please understand mine!!

Why the hel l am i using the void thing if it does nothing?

What i am trying to do is set hour or minute or second to 0 if the value is incorrect.

In my main program what can i call to check for this? Since YOU HAVE MADE IT CLEAR THAT I CANT INVOKE THE VOID METHOD :), then

1.in which method should i check for the values?

and

2.how should i invoke it?

Hang with me man im falling off the bridge quick.

Progr@mera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 25
Thanks for your help man, im going to sleep, i'll come back tomorrow morning beacuse if i sit anotehr minute on the computer i swear i will break it.Heres your well deserved duker points
Progr@mera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 26

You are missing my point about the void method. Of course you can invoke them, there wouldn't be a point to having them if you couldn't. Perhaps if you answer these questions you will see what I'm driving at.

After invoking the method:

Why are you printing?

What are you trying to print?

floundera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 27

> You are missing my point about the void method. Of

> course you can invoke them, there wouldn't be a point

> to having them if you couldn't. Perhaps if you answer

> these questions you will see what I'm driving at.

>

> After invoking the method:

> Why are you printing?

If the input of the user is incorrect i am trying to set the time to 00;

> What are you trying to print?

I am trying to print 00.

Progr@mera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 28
Then perhaps you should have some print statements in the setHour method instead and get rid of the print statement in main because it is wrong. Just invoke the method.
floundera at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...
# 29

What languages are you coming from? Try to make sure you see the differnces in the following:int x = 2;

int y = 0+x;

System.out.println(y);

y = 00+x;

System.out.println(y);

String z = 0+x;

System.out.println(z);

String z = "0"+x;

System.out.println(z);

It looks like maybe you worked with scripting languages, and don't quite get the differences between types. The main lesson learned here is that the "+" operator has different outputs depending on whether both operands are ints, both are Strings, or there is a mix (and with many of these operators, when using them with mixed types, it is important also to keep track of which operand is which type).

Also you need to think about the method signature. There is nothing wrong with invoking a method that has a return type of void. There is something wrong with expecting System.out.println(Object o) to take in void as an argument. If you want to invoke System.out.println(String s), then setHour() should have a return type of String (and probably should be renamed - or given javadoc - to avoid confusion).

bheilersa at 2007-7-21 20:06:22 > top of Java-index,Java Essentials,New To Java...