File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Design question (algorithm)

 
Peter Allen
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Allen
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like the only times you'd need multiple letters would be for 'M', 'C', 'X', and 'I'.
 
John Dale
Ranch Hand
Posts: 399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
 
Peter Allen
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic