• 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

Base Conversions

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to write two methods. One called basetodec and dectobase. The basetodec method will take any base 2-36 and translate it to decimal and the same thing with dectobase that will take any decimal and translate that into any base of 2 - 36. Please can anyone help me?
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
help with what?

What have you done so far?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do YOU know how to do the conversions? i'd say that's the first thing you need to know. You can't write the code to do it until you understand the problem.

once you know how to do it for each specific case (i.e. base 3 to 10, base 4 to 10, etc), see if you can generalize it for all cases... i.e. you don't want to have to write 31 methods for each possible base to convert to, and then 31 more methods for each possible case to convert from...

So, write in english (or whatever your native language is) the steps...

1) divide by "convert to" base
2) print quotient
3) add 'convert from" base
4) repeat until quotient is less than 'convert from' base

[note: this is not the real algorithm - just some crud i made up]

once you have a general idea what the algorithm is, start writing some code. DON'T try and do it all at once - write small, testable pieces. maybe start with asking for input, then print that input out. once you KNOW you can get the input, see if you can get ONE step of your alorithm to work right. then lather, rinse and repeat. The LESS you write between compiling/testing steps, the easier life will be.
 
Andy Clark
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know how to do all the calculations on paper. It's just the whole telling the computer how to do it is a little taxing on my behalf.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Andy Clark:
I know how to do all the calculations on paper. It's just the whole telling the computer how to do it is a little taxing on my behalf.



sorry - i just wasn't sure from your original post where you needed the help.

if you know how to do it on paper, that's 70% of the battle. my advice would be to actually write down the steps. be very general. this becomes your outline. Then, go back and expand a little on each step. then refine those steps.

Also, try just doing one conversion - say, for example, from base 10 to base 7 (or any other one you feel comfortable with). once you have it broken down pretty far, start looking to see how you can generalize it - what would you have to change to make it base 10 to base 12?

but most important, when you sit down to write your code, DON'T try and do everything at once. Yes, i said that before, but it deserves to be repeated over and over. do one piece at a time. maybe just start with a program that gets the input, prints "i'm doing the conversion", then prints some output (possibly to verifiy that you REALLY did get the input correctly).

once that is done, start trying to code the conversion. continually test everytime you write a few lines. go back and retest the stuff you already did to make sure you don't break it.

And when you get stuck on something specific, post your code here and ask a specific question - you're much more likely to get help that way than with a broad "I don't know what to do" kind of question.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing that seems to me to be unclear is how you want to represent these numbers. Actual numeric types in Java are, of course, always in binary. Computers just can't work in any other base (OK, so some at the dawn of time did, but...).

If what you want is printable text representations of the numbers in the various bases, then Java will do everything for you.

Integer.parseInt(String s, int radix) will take the textual representation s of a number in base radix and convert it into a binary integer (a Java int).

Integer.toString(int i, int radix) will convert binary integer i into a textual representation in base radix.

Or is this a piece of homework/coursework in which you're not allowed to use these built-in methods?
 
Andy Clark
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the first method written as

now i just need the BaseToDec written.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps you should first describe the steps you use to do this by hand. If you can write out this description in your native language, it shouldn't be much more difficult to translate that into Java. If it helps, use this thread as a sounding board where you can think out loud, so to speak.

Layne
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic