aspose file tools*
The moose likes Beginning Java and the fly likes Question from Just Java 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question from Just Java 2" Watch "Question from Just Java 2" New topic
Author

Question from Just Java 2

Jon Lewis
Greenhorn

Joined: Aug 31, 2007
Posts: 3
Hi all, I'm new to all this and am trying to learn through the book Just Java 2, but one of the very first excercises has me stumped!
It's a simple digital clock, and the problem is to change the file that handles the view so that it displays roman numerals instead of "regular" numbers. Here's the original code to display normally:

I thought an easy way to change it (and the book didn't really explain a lot in this chapter) was to just use some if--else like so:

But after doing that the clock still shows arabic numerals only without a zero before single digits since I removed the if (i<10) part.
Please forgive me for asking what I know must be a really stupid question to many of you.
Thank you for your help.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

Your approach to modifying the method looks like it should work. Can you post more complete code to demonstrate how it's not working?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jon Lewis
Greenhorn

Joined: Aug 31, 2007
Posts: 3
Hi Marc, thanks for the reply and kind welcome.
The first code I posted was the complete java file for the display portion of the little app, and according to the excercise it is the only file I would need to change (which makes sense).
The only code I left out was, I simply repeated the else if statements going up to 9 in roman numerals...which is I think where the problem is, but the only other way I could think of is, having 59 else if statements and that seems excessive.
The rest of the code remained untouched. I really appreciate you helping me think this through--I can't wait to find out what it is I'm missing!
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Jon -

I tried it & it worked for me - I think I know where your problem is. You left the original definition str = Integer.toString(i) in your code. If your if-else statements only went up to 9 and didn't include a final else (in case all your parameters were either 0 or > 9), then str would not be redefined. In that case, the original definition would be returned. I used a switch statement and included a default value so that I would know whether it worked or not. (Obviously I also added a main function so I could make it work -)

Maybe the exercise is more subtle than it looks - is there any chance that you are supposed to construct the Roman numerals out of the separate digits in the parameter i? Because that's the way the Roman numerals are constructed by hand.

Also, I couldn't get the Timestamp class to work for me - had to use Date. If you answer this, could you tell me something about using Timestamp? I crawled through the API on this class but couldn't get anything useful out of it ...

Good luck!
Jon Lewis
Greenhorn

Joined: Aug 31, 2007
Posts: 3
Thanks for the reply Jinny. The Timestamp class is, I believe, unique to this excercise. Here is the whole Timestamp class:

Again, the excercise states that this doesn't need to be changed but since you got it to work using just Calendar I wonder why it's there at all.
A couple of questions...Don't I need to convert the integers produced by the calls to the Calendar class to strings, and since there's no roman numeral for zero, could that be part of what I'm missing?
Also, would you be kind enough to post the code that you used?
Thanks again and thanks in advance to anyone who'll help with this maddening problem!
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Jon -

AHA! Since there is a Timestamp class listed in the Java API, I didn't realize that your Timestamp class was a "personal" one created for this exercise - thanks!

Actually, no you don't - for 2 reasons. One is that when you convert to Roman numerals you are testing the integer parameter passed into your getDigitAsString method; you are then defining the string as "I", "II", etc. So all you have to do is to declare the String variable as something like or something like that; you then give it a value in your if statements. The other is that I think that when you concatenate a number with a string by saying something like i + ":" the Java software is smart enough to convert the integer variable to a string before concatenating. I know I've used this facility in output statements; you might try it here to make sure it works.

This is the code I used; note that I'm lazy too and wasn't interested in thinking hard enough to get everything converted so my default "case" is just there so I will know that the whole thing sort of works ...



Good luck! Let me know if the above doesn't make sense.
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Jon -

Have you seen the problem with the if-else statements? Have you been able to solve the exercise without using 60 if-else statements?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question from Just Java 2