Question
ABOVE YOU CAN SEE WHAT MY CODE OUTPUTS AND WHAT THE SOLUTION CODE OUTPUT IS (THE CORRECT ANSWER) I NEED HELP FIXING THE FOLLOWING ERRORS
ABOVE YOU CAN SEE WHAT MY CODE OUTPUTS AND WHAT THE SOLUTION CODE OUTPUT IS (THE CORRECT ANSWER)
I NEED HELP FIXING THE FOLLOWING ERRORS IN MY CODE (USING JAVA)^
***CODE DOWN BELOW***
public class Homework4 { static String reverseString(String s,int i) { if(s==null||s=="") { return "String is empty or null."; } else if(i==s.length()) { return ""; } else { return reverseString(s,i+1)+String.valueOf(s.charAt(i)); } }
static int addOddNums(int[] a,int i) { if(a==null) { return -1; } else if(i>a.length-1) { return 0; } else { return (a[i]%2!=0?a[i]:0)+addOddNums(a,i+1); } }
static String addSpaces(String s) { if(s==null) { return "String is empty or null."; } else if(s.length()==0) { return ""; } else { return s.charAt(0)+" "+addSpaces(s.substring(1)); } }
static String weave(String s1,String s2) { if(s1==null||s2==null) { return null; } else if(s1=="") { return s2; } else if(s2=="") { return s1; } else { String st1,st2; if(s1.length()==1) { st1=""; } else { st1=s1.substring(1); } if(s2.length()==1) { st2=""; } else { st2=s2.substring(1); } //System.out.println(String.valueOf(s1.charAt(0))+String.valueOf(s2.charAt(0))); return (String.valueOf(s1.charAt(0))+String.valueOf(s2.charAt(0)))+weave(st1,st2);
} } static int multByAdd(int m,int n) { if(n==0||n0) { return m+multByAdd(m,n-1); } else { return -1*multByAdd(m,-n); } }
static boolean palindrome(String s) { if(s.length()
public static void main(String[] args) { System.out.println("1,6c1"); System.out.println(" Welcome to Homework4. Happy Coding!"); } }
Recursion For this assignment you are going to write six different methods. Each method is to be written recursively. Any method that is written iteratively will not receive any credit, even if it is correct and produces the same results or output. You will be given a starter file. You are not allowed to change the signatures of any of the given methods. You are not allowed to add any methods to your solutions. 1. Write a method called reverseString() that takes a string and uses recursion to returns the input string in reverse order. The method has the following header: void reverseString(String s, int i) { } where s is a reference to the string, and is an integer parameter that you may use as you see fit. However, do not assume a specific value for i. If the string is null or empty (""), return the string "String is empty or null." (without the double-quotes). 2. Write a method called addoddNums() that uses recursion to add all of the odd numbers in an integer array starting from a certain index. The method has the following header: int addOddNums(int[] a, int i) { } where a is the array and i is an index into the array. For example: int[] array = {1, 2, 3, 4, 5); int sum = addoddNums (array, 0); // This call should return a value 9 (1+3+5) int sum = addoddNums (array, 3); // This call should return a value 5. If the value null is passed in as the array parameter it should return -1. 3. Write a method called adaSpaces that uses recursion to return individual characters of a string separated by spaces. The method has the following header: void addSpaces (String s) { } For example, the following call addSpaces ("spaces") should return the following string: spaces The method should not do any printing. The method should return the string "String is null" (without the double-quotes) if the value null is passed in as an argument. Hint: Don't forget about the empty string (""). 4. Write a method called weave that uses recursion to return the string that is formed by "weaving" together the characters in the strings s1 and s2 to create a single string. This method has the following header: String weave (String si, String s2) { } For example: weave("aaaa", "bbbb") should return the string "abababab" weave("hello", "world") should return the string "hweolrllod" If one of the strings is longer than the other, its "extra" characters - the ones with no counterparts in the shorter string - should appear immediately after the "woven" characters (if any) in the returned string. For example, weave("recurse", "NOW") should return the string "rNeOcWurse", in which the extra characters from the first string - the characters in "urse" - come after the characters that have been woven together. This method should not do any printing; it should simply return the resulting string. If null is passed in for either parameter, the method should null. If the empty string('') is passed in for either string, the method should return the other string. For example, weave("hello", "") should return "hello" and weave("", "") should return the empty string(""). 5. Write a method called multByAdd() that uses recursion to multiply integers together, without using multiplication. If the second parameter n, is negative return 0 (zero). The method has the following header: int multByAdd(int m, int n) } For example, the following calls should return the following values: multByAda(5, 4); //returns 20 multByAda(100, 0); // returns 0 multByAdd(-6, 10); //returns -60 6. Write a method called palindrome () that uses recursion to determine whether or not a string is a palindrome. You can get more information on a palindrome here: Palindrome Linke. The method has the following header: boolean palindrome (Strings) { } For example, the following calls should return the following values: palindrome ("Hannah"); //returns true palindrome("abca"); //returns false palindrome ("Q"); //returns true palindrome("abbbbbbbbbbc"); //returns false Test Cases 25 2 23/25pts Add Odd Nums 1 1/1 pts - Click for details PASSED Add Odd Nums 2 1/1 pts - Click for details PASSED Add Odd Nums 3 1/1 pts - Click for details PASSED Add Odd Nums 4 1/1 pts - Click for details PASSED Add Spaces 1 1/1 pts - Click for details PASSED Add Spaces 2 0/1 pts - Click for details FAILED Add Spaces 3 1/1 pts - Click for details PASSED Initilal Test Case 0/1 pts - Click for details FAILED Mult By Add 1 1/ 1 pts - Click for details PASSED Mult By Add 2 1/1 pts - Click for details PASSED Mult By Add 3 1/1 pts - Click for details PASSED Mult By Add 4 1/1 pts - Click for details Debugging Information for Add Spaces 2 O DEBUG INFO Your instructor has chosen to give you the following information to help you debug your code and improve your submission. COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None YOUR CODE'S OUTPUT 1 Input: string is ** (empty) 2 Correct string = String is empty or null. 3 Your string = 4 BE SOLUTION CODE OUTPUT 1 Input: string is (empty) 2 Correct string = String is empty or null. 3 Your string = String is empty or null. 4 Debugging Information for Initial Test Case x Your instructor has chosen to give you the following information to help you debug your code and improve your submission. COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None INPUT OF THE TEST CASE None YOUR CODE'S OUTPUT 1 1,6c1 2 Welcome to Homework4. Happy Coding! 10 CONO -- UNIX DIFF OF CORRECT OUTPUT AND YOUR OUTPUT 1,9c1 Welcome to Homework4. Happy Coding! > Welcome to Homework4. Happy CodingStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started