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?

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Andy Clark
Greenhorn

Joined: Nov 07, 2004
Posts: 3

posted

0

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.

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.

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(Strings, 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?

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.

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.