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
  • What is the time complexity (Big-O) of the following code? class Main {    // Recursive...

    What is the time complexity (Big-O) of the following code? class Main {    // Recursive function to generate all permutations of a String    private static void permutations(String candidate, String remaining)    {        if (remaining.length() == 0) {            System.out.println(candidate);        }        for (int i = 0; i < remaining.length(); i++)        {            String newCandidate = candidate + remaining.charAt(i);            String newRemaining = remaining.substring(0, i) +...

  • 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();    } }

  • In java need help with the TODO sections creating recursive methods public class CountUpDown { /**...

    In java need help with the TODO sections creating recursive methods public class CountUpDown { /** * countUp - a recursive function that counts up from 1 to n * * @param n the integer value to count up to */ private static void countUp(int n) { // TODO PRELAB // IMPLEMENT THIS RECURSIVE METHOD } /** * countDown - a recursive function that counts down from n to 1 * * @param n the integer value to count down...

  • 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:

  • java 1. Write a method header on line three with the following specs: Returns: a boolean...

    java 1. Write a method header on line three with the following specs: Returns: a boolean Name: beTrue Parameters: none public class Main {       {        return true;    } } 2. Write a method header on line two with the following specs: Returns: a String Name: makeCapital Parameters: a String named "name" You should not be writing code on any line other than #2 public class Main {       {    String ans = name.toUpperCase();...

  • 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"); } }

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