Question

In: Computer Science

In Java please. Thank you! Recursion For this assignment you are going to write six different...

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.

  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 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).

  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 addSpaces()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:

    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 ("").
  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 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 ("").

  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:

    multByAdd(5, 4); //returns 20
    multByAdd(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 Link (Links to an external site.).

    The method has the following header:

    boolean palindrome (String s) { }

    For example, the following calls should return the following values:

    palindrome("HannaH"); //returns true
    palindrome("abca"); //returns false
    palindrome("Q"); //returns truepalindrome("abbbbbbbbbbc"); //returns false

Solutions

Expert Solution

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:


Related Solutions

Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
in Java please For this assignment you are to write a class that supports the addition...
in Java please For this assignment you are to write a class that supports the addition of extra long integers, by using linked-lists. Longer than what is supported by Java's built-in data type, called long. Your program will take in two strings, consisting of only digits, covert each of them to a linked-list that represents an integer version on that string. Then it will create a third linked-list that represents the sum of both of the linked lists. Lastly, it...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
Please answer this two part question. Thank you! For this assignment you must write the following...
Please answer this two part question. Thank you! For this assignment you must write the following functions in Scheme: 2.1 Write a recursive function called eval-poly that takes a list of numbers representing the coefficients of a polynomial and a value for ? and evaluates the polynomial for the given value of ?. The list of coefficients should start with the term of lowest degree and end with the term of highest degree. If any term of intermediate degree is...
please use java swing and recursion and the suggested method hints listed Objective: Write a program...
please use java swing and recursion and the suggested method hints listed Objective: Write a program in which draws (yes it actually makes a picture) a triangular fractal using recursion. This is best if done using a java applet. Suggested Methodology The idea for it is this First draw a filled equilateral triangle Next draw another filled equilateral triangle of a different color that’s upside down in the middle of that triangle Using the other triangles formed repeat step 2...
Please answer this multi-part question. Thank you! For this assignment you must write the following function...
Please answer this multi-part question. Thank you! For this assignment you must write the following function in Scheme: 4 Write a recursive function called mergesort that sorts a list by doing the following: (a) Use split to split the list into two roughly equal-sized partitions. (b) Recursively sort both partitions. (c) Use merge to merge the sorted partitions together. Once again you will need two base cases, one for the empty list and the other for a single-element list. >...
Please answer this multi-part question. Thank you! For this assignment you must write the following functions...
Please answer this multi-part question. Thank you! For this assignment you must write the following functions in Scheme: 4 Write a recursive function called mergesort that sorts a list by doing the following: (a) Use split to split the list into two roughly equal-sized partitions. (b) Recursively sort both partitions. (c) Use merge to merge the sorted partitions together. Once again you will need two base cases, one for the empty list and the other for a single-element list. >...
Hello. If you’re going to hand write the solution, please do so legibly. Thank you. Determine...
Hello. If you’re going to hand write the solution, please do so legibly. Thank you. Determine the amount of sales units that would be necessary under break-even sales under present and proposed conditions. Johnson & Sons Company, operating at full capacity, sold 101,250 units at a price of $129 per unit during the current year. Its income statement for the current year is as follows: Sales $13,061,250 Cost of goods sold 6,450,000 Gross profit $6,611,250 Expenses: Selling expenses $3,225,000 Administrative...
**Please write in Java, in a very simple/beginner coding style/language - thank you!** Directions: Given a...
**Please write in Java, in a very simple/beginner coding style/language - thank you!** Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing zeros (ie: the number of zeros at the end of the factorial). Input: integer nn, a non-negative integer where n≤20n≤20 Output: integer xyxy, the concatenation of x and y, where x is the sum of the digits in n! and y is the number of the zeros in n!) Note, 0≤x,y0≤x,y....
Create a six month MARKET forecast and please explain it. Thank you
Create a six month MARKET forecast and please explain it. Thank you
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT