Question

Java, finish the recursive function that converts a binary string to decimal, do not change main...

Java, finish the recursive function that converts a binary string to decimal, do not change main or function header.

public static void main( String[] args ) {
if( binToDec( "1101100" ) == 108 ) {
System.out.println( "binToDec1 is correct!" );
}
if( binToDec( "1011101" ) == 93 ) {
System.out.println( "binToDec2 is correct!" );
}

}

public static int binToDec( String bin ) {


}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

public class BinaryToDecimalRecursive {

    public static void main(String[] args) {
        if (binToDec("1101100") == 108) {
            System.out.println("binToDec1 is correct!");
        }
        if (binToDec("1011101") == 93) {
            System.out.println("binToDec2 is correct!");
        }

    }

    public static int binToDec(String bin) {
        if (bin.isEmpty()) {
            return 0;
        } else {
            return binToDec(bin.substring(0, bin.length() - 1)) * 2 + (bin.charAt(bin.length() - 1) - '0');
        }
    }
}

binToDecl is correct! binToDec2 is correct! Process finished with exit code 0



Please let me know if you have any doubts Please upvote this answer. Thanks!!

Add a comment
Know the answer?
Add Answer to:
Java, finish the recursive function that converts a binary string to decimal, do not change main...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a recursive method in java to find GCD of two integers using Euclid's method. Integers...

    Write a recursive method in java to find GCD of two integers using Euclid's method. Integers can be positive or negative. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“The GCD of 24 and 54 is “+r.findGCD(24,54)); //6                 }                 public int findGCD(int num1, int num2){                                 return -1;                 } }

  • how to change this code to string instead of int    public static void main(String[] args)...

    how to change this code to string instead of int    public static void main(String[] args) {        int[] sample = { 212, 580, 6, 7, 28, 84, 112, 434};        Dictionary bst = new Dictionary();        for (int x : sample) {            bst.insert(x);        }        System.out.println(bst.find(65));        System.out.println(bst.smallest());        System.out.println(bst.largest()); //       bst.delete(84);        System.out.println(bst.numOfLeafNodes());        System.out.println(bst.height());        bst.traverseInOrder();    } }

  • THE LANGUAGE IS JAVA!! Define stubs for the methods called by the below main(). Each stub...

    THE LANGUAGE IS JAVA!! Define stubs for the methods called by the below main(). Each stub should print "FIXME: Finish methodName'followed by a newline, and should return -1. Example output: FIXME: Finish getUserNum() FIXME: Finish getUserNum() FIXME: Finish computeAvg() Avg: -1 import java.util.Scanner; 3 public class MthdStubsStatistics { 1* Your solution goes here */ public static void main(String [] args) { int userNum1; int userNum2; int avgResult; userNum1 = getUserNum(); userNum2 = getUserNum(); avgResult = computeAvg(userNumi, userNum2); System.out.println("Avg: " +...

  • 1. What is the height of a full binary search tree that contains 63 nodes 2....

    1. What is the height of a full binary search tree that contains 63 nodes 2. What is the output of this code? Describe what this recursive code does. public class Driver {    public static void main (String[ ] args)   {        String text = “what do I do”;        System.out.println(Mystery.task(text));   } } public class Mystery {    public static int task(String exp)   {         if(exp.equals(“”)           return 0;        else           return 1 + task(exp.substring(1));    } } //from the Java 7 API documentation: public String substring(int...

  • Given the following Java code: public static void main (String[] args) { int num; System.out.println("Enter a...

    Given the following Java code: public static void main (String[] args) { int num; System.out.println("Enter a number"); num = scan.nextInt(); <-first input statement while (num != 0) { System.out.println ("The number is: " + num); System.out.println("Enter a number"); num = scan.nextInt(); } //End While } //End Module The first input statement shown above is called the:

  • in java Part 1 In this section, we relook at the main method by examining passing...

    in java Part 1 In this section, we relook at the main method by examining passing array as parameters. Often we include options/flags when running programs. On command line, for example, we may do “java MyProgram -a -v". Depending on options, the program may do things differently. For example, "a" may do all things while "-v" display messages verbosely. You can provide options in Eclipse instead of command line: "Run ... Run Configurations ... Arguments". Create a Java class (Main.java)....

  • What is the Java output? Part One: class Driver { public static void main(String[] args) {...

    What is the Java output? Part One: class Driver { public static void main(String[] args) { int a = 5; int b = 3; if (a < b || a * 2 < b) System.out.print(a - b); System.out.print(b + a); } } Part Two: class Driver { public static void main(String[] args) { int a = 5; int b = 8; if (a < b) if (a * 2 < b) System.out.print("foo"); else System.out.print("bar"); else System.out.print("buz"); } }

  • What display is produced by the execution of this JAVA program if we make the following...

    What display is produced by the execution of this JAVA program if we make the following call f(0,0). Please explain your answer public class function {    static void f(int x){        System.out.println(1);    }    static void f(double x){        System.out.println(2);    }    static void f(char x){        System.out.println(3);    }    static void f(long x){        System.out.println(4);    }    public static void main(String[] args) {       } }

  • **JAVA** Write a binary search method for a String array that might contain nulls. Your method...

    **JAVA** Write a binary search method for a String array that might contain nulls. Your method should not crash or throw a runtime exception. See the driver program for examples. The method header is: public static int binarySearchWithNulls(String[] words, String target) must be recursive.

  • In Java This is the method we did in class. How do I reverse the string...

    In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); }    public static String printBackwards(String one)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT