0

This is an assignment question from school: Create a recursive method called toBinary that takes a single integer as a parameter and outputs the equivalent in binary.

Here's my code:

import java.util.*;
class MethodAssign6{
static void toBinary(int a){
    if(a==0){
        System.out.print("theArrayOrStringIWant");
    }
    else{
        System.out.println(a%2);
        toBinary(a/2);
    }
}
public static void main(String[]args){
    toBinary(24);
}
}

As you can see, I have no idea how to create an array to hold all the a%2 values when the question only wants me to have a single integer as a parameter. Anyone please help me I would appreciate very much.

4 Answers 4

3

I think you're on the right track—but your current method will print the number out backwards. Try doing the recursive call and then printing:

static void toBinary(int a){
    if(a!=0) {
        toBinary(a/2);
        System.out.print(a%2);
    }
}

You could also get a similar effect using a String:

static String toBinary(int a){
    if(a==0) {
        return "";
    }
    else {
        return toBinary(a/2) + (a%2);
    }
}

public static void main(String[]args){
    System.out.println(toBinary(24));
}
1
  • @cook cook - It's really not that different from what you already had—you were almost there. I'm glad my answer made sense! If this was the answer you were looking for then it'd be nice if you accepted it.
    – DaoWen
    Oct 17, 2012 at 16:09
1

Generally, recursive methods like this will have a public method and also a private method used to complete the computation. So the public method takes a single integer, then sets up the array to hold the values. The private array takes the integer, and also the current array so it can add it's value to the array. Finally, when the private method returns, the public method will parse the array and return the result.

1

If all you have to do is print the bits, you don't actually need to hold them in an array. You can just use System.out.print as you go. Make sure you pay attention to the order in which you make the call to print and the recursive call, lest you print the number backwards.

-1
// static String[] valsbyte = new String[10]; // global variable if you return a string
static byte[] valsbyte = new byte[10]; // global variable


// static String[] toBinary(int a){ // if you return a string
static byte[] toBinary(int a){
n++;
if(n>10){ // check recursion to jump over process and internal method call


// valsbyte[n] = a%2; // if you reurn a string

valsbyte[n] = new Integer(ax).byteValue(); // convert to byte



//LAST LINE OF METHOD
return valsbyte[n];
1
  • Welcome to Stack Overflow. Generally, it's preferred that answers explain why they are correct, rather than just being a code-dump. You'll get more upvotes (and therefore reputation and privileges) if you give more thorough answers.
    – KRyan
    Oct 17, 2012 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.