Question

java Considering the following code segment, answer the multiple choice questions. public String foo(String input) {...

java Considering the following code segment, answer the multiple choice questions.

  1. public String foo(String input) {

  2. String str = input.toLowerCase();

  3. return bar(str);

  4. }

  5. private String bar(String s) {

  6. if (s.length() <= 1)   {return "";}

  7. else if (s.length() % 2 == 0)   {return "*" + bar(s.substring(2, s.length()));}

  8. else if (s.length() % 2 == 1) {return "*" + bar(s+"n");}

  9. else {return "";}

  10. }

  11. Which line(s) indicate the name of the helper method? Which line(s) contains recursive call(s)? How many * are in the return String when we call bar("Dave")?

Homework Answers

Answer #1

Ans

bar is helper function of foo. call of bar in foo function indicate that bar is helper of foo.

Below statement in foo indicate helper

return bar(str);

.

.

Recursive call occurs if function call itself.

in bar function there are two recursive calls based on condition in else if.

bar(s.substring(2, s.length())

bar(s+"n");

are recursive calls

.

.

.

bar("Dave)

length of string = 4

in if else ladder

s.length%2 turns out true that concatenate * and call recursively bar("ve") (substring returns string from index 2 to last) and return

length of ve = 2

s.length%2 turns true concatenate * and recursive call to bar("")

for bar("") length =0

so s.length<=1 turns true and return "" to previous 2nd call.

2nd call return * to 1st call

1st call return **

.

Hence two * are returned on call bar("Dave")

.

.

.If any doubt ask in the comments.

Know the answer?
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
Similar Questions
Considering the following code segment, answer the multiple choice questions. Which line(s) indicate the base case...
Considering the following code segment, answer the multiple choice questions. Which line(s) indicate the base case of the recursion? Which line(s) of code make recursion move toward the base case? public String foo(String input) { String str = input.toLowerCase(); return bar(str); } private String bar(String s) { if (s.length() <= 1)   {return "";} else if (s.length() % 2 == 0)   {return "*" + bar(s.substring(2, s.length()));} else if (s.length() % 2 == 1) {return "*" + bar(s+"n");} else {return "";} }
Give the output of the following program. Then explain what the foo() method does, in general,...
Give the output of the following program. Then explain what the foo() method does, in general, for a given input. Is foo() a tail-recursive method? Why or why not? class SomeThing {     public static void main(String args[]) {         System.out.println(foo(6));     }     private static int foo(int input) {         if (input <= 0) {             throw new RuntimeException("invalid input");         }         else if (input == 1) {             return 1;         }         else if (input %...
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
Given the following method: public boolean check(String s) { return s.length() >= 2 && (s.charAt(0) ==...
Given the following method: public boolean check(String s) { return s.length() >= 2 && (s.charAt(0) == s.charAt(1) || check(s.substring(1))); } This method will return true if and only if: 1. s.charAt(0) == s.charAt(1) 2. s starts with two or more of the same characters 3. s ends with two or more of the same characters 4. s contains two or more of the same characters 5. s contains two or more of the same character in a row
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume...
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume that the Employee class has not implemented its own toString() method. What value will the variable str contain when this code is executed? Group of answer choices the variable will become a null reference the code will not compile because Employee has not implemented toString() the default from Object, which is the class name of the object and its hash code the string representations...
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT