Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How should I do this?

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default How should I do this?

    So I'm making a Unit Converter which will be able to convert 43 different type of units (temperature, currency, volume, mass, length, speed) to any of the 43 types. But right now I'm a little unsure on how to proceed. I'm up to the part where the actual converting happens (doing this all using JFrames btw), and I'm not sure if I need to make 1849 if statements (43*43) for each possible combination of units, or if there's a simpler way. There must be a simpler way but I don't know it because I'm really a beginner at Java. So anyway, I would appreciate any ideas!

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How should I do this?

    The first thing that pops into my head is to have a Map<Unit, Map<Unit, Conversion>> data structure where Unit is an enum of all the different types of units, and Conversion is an interface that basically takes one Unit and converts it to another.

    So you'd have something like:

    conversionMap.get(Unit.INCHES).put(Unit.FEET, new Conversion(){public void convert(double inches){return inches/12;}});

    So then to convert from inches to feet you'd do:

    double feet = conversionMap.get(Unit.INCHES).get(Unit.FEET).convert(inches);

    Anyway, that's just the first thing I thought of, so there are probably better ways.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: How should I do this?

    I would still have to use 1,849 statements for this though...I don't want to have to copy + paste + change slightly that many times.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How should I do this?

    Haha, well, I'm not sure there's a better way, since there is no automatic way to know the conversions. You're going to have to write quite a bit of code for 43 X 43 conversions, so you might want to start off smaller and add to it as you go.

    And, you would not have to write 1,849 statements, as not every unit converts to every other unit.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How should I do this?

    If you have the units specified in some sort of parsable format, then rather than manually hard coding each, you could write a script which prints out to the command line those values in the form of the source code you need - which you can then just copy and paste into your class.

Tags for this Thread