In: Computer Science
In Java please. Thank you!
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.
The method has the following header:
void reverseString(String s, int i) { }where s is a reference to the string, and i 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).
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.
The method has the following header:
void addSpaces(String s) { }
For example, the following call addSpaces("spaces")should return the following string:
s p a c e s
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 ("").This method has the following header:
String weave(String s1, 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 ("").
The method has the following header:
int multByAdd(int m, int n) { }
For example, the following calls should return the following values:
multByAdd(5, 4); //returns 20The method has the following header:
boolean palindrome (String s) { }
For example, the following calls should return the following values:
palindrome("HannaH"); //returns true
Java Program:
class Main {
static String reverseString(String s,int i)
{
if(s==null||s=="")
{
return "String is empty";
}
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";
}
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)||(s1==""&&s2==""))
{
return "";
}
else if(s1==null||s1=="")
{
return s2;
}
else if(s2==null||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(s1.charAt(0)+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) //base case
{
return 0;
}
else if(n>0)
{
return m+multByAdd(m,n-1);
}
else
{
return -1*multByAdd(m,-n);
}
}
static boolean palindrome(String s)
{
if(s.length()<2) //base case
{
return true;
}
else
{
return (s.charAt(0)==s.charAt(s.length()-1))&&palindrome(s.substring(1,s.length()-1));
}
}
public static void main(String[] args) {
System.out.println(reverseString("Mango",0));
int arr[]={1,2,3,4,5};
System.out.println(addOddNums(arr,3));
System.out.println(addSpaces("spaces"));
System.out.println(weave("recurse","NOW"));
System.out.println(multByAdd(10,-6));
System.out.println(palindrome("HannaH"));
}
}
Sample Output: