• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question from Just Java 2

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Jon Lewis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic