File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Design question (algorithm) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Design question (algorithm)" Watch "Design question (algorithm)" New topic
Author

Design question (algorithm)

Peter Allen
Greenhorn

Joined: Apr 13, 2002
Posts: 13
This question has always bothered me in a book I read a while ago and I can't find a proper solution. It's a program to convert a given number(integer) to roman numerals, for some reason I think I'm not doing this the most efficient way. Whenever code is repeated like this, there has to be something I missed. Loops aren't even covered yet in the book, yet this exercise is given. I've long finished the book, but I just wanted to know what I was doing wrong if loops weren't intended to be used, and my code looks bad enough as it is... here it is

There must be some other design I was overlooking, especially since this book is easy, including the exercises. This code is very ugly. Can anyone see what I'm overlooking in the design? Thanks.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
This isn't the perfect response:
About two weeks ago I read an article on extreme programming, unit testing, and object oriented design that was based on this very task (converting numbers to roman numerals). It might have been on extremeprogramming.org or JavaWorld.com or JUnit.org or who knows where. I think the article was more than 3 months old (leaning towards a year), but I don't really recall. And no, of course I don't remember the author.
If I run across it (again), I'll post a link; or maybe someone else knows of the article - maybe someone over in the OO, Patterns, UML and Refactoring forum.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Peter Allen
Greenhorn

Joined: Apr 13, 2002
Posts: 13
Thanks, I'll look around for it. I'm positive that my book has a serious problem in its exercises. It assigns this particular problem prematurely. I've been working at a solution, and there is no way this can be done using only if statements (which is as far as the book goes when assigned this exercise). It hasn't even gone into loops, Strings (in detail), anything. Only simple math functions and if and case statements. I guess it's ok, I want a real solution using any technique, just because this problem has been bugging me
[ April 13, 2002: Message edited by: Peter Allen ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Seems like the only times you'd need multiple letters would be for 'M', 'C', 'X', and 'I'.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
John Dale
Ranch Hand

Joined: Feb 22, 2001
Posts: 399
Maybe they will give you this same exercise over and over, introducing each of the various programming elements, and let you them see how various programming element improve things. Eventually you could use loops, arrays, methods calls, and classes.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
While this isn't the article I previously referred to, it does discuss the problem you are considering:
Exercise 9.3 from Introduction to Programming Using Java by David J. Eck and the solution.
(I'm still looking for that other article...)
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Found it!
Test First Design Example: Roman Numeral Converter
referenced from:
XProgramming.com on Test First Guidelines
Good Luck.
Peter Allen
Greenhorn

Joined: Apr 13, 2002
Posts: 13
Thank you for the reply. Interestingly enough I found another solution from delphi code I saw, also some java code and I combined it. This does both functions, to roman numerals and also to arabic numbers. If anyone is interested, take a look
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
While I haven't really analyzed and tested the code, it looks like a much nicer design than the first time around.
Well done.
[ April 17, 2002: Message edited by: Dirk Schreckmann ]
 
GeeCON Prague 2014
 
subject: Design question (algorithm)