Question
  1. Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form.

    The method has the following header:

      void printReverse(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. You do not need to code up this method as part of a class – a written version of the method itself is all that is required.

    For full credit, your printReverse() method should consider the first element of the list first – i.e., the first call to printReverse() should be the one that prints the first element, the second call should be the one that prints the second element, and so on. Half credit will be given for methods that consider the last element first. In either case, your method must be recursive; no credit will be given for methods that employ iteration.

  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 = new  []{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.

The method should not do any printing and if the value null is passed in as the array parameter it should return 0 (zero).

3. Write a method called printWithSpaces()that uses recursion to print the individual characters of a string separated by spaces.

The method has the following header:

    void printWithSpaces(String s)

For example, the following call printWithSpaces("spaces") should produce the following output:

s p a c e s

This method should not return a value. The method should not do any printing if the empty string ("") or the value null is passed in as the parameter.

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 print an error message. 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 "".

5. Write a method called multByAdd()that uses recursion to multiply integers together, without using multiplication. You can assume that the second parameter, n, is positive.

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
                                    multByAdd(-6, 10); //returns -60

GIVEN STARTER FILE IS AS FOLLOW:

public class RecursionApp {
  
public static void main(String[] args) {
reversePrint(null, 0);
reversePrint("", 0);
reversePrint("reverse", 0);
System.out.println();
reversePrint("!nuf si noisruceR", 0);
System.out.println();
int[] array = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
System.out.println("The sum of the odd numbers is: " + countOddNums(array, 0));
System.out.println("The sum of the odd numbers is: " + countOddNums(array, 9));
array = null;
System.out.println("The sum of the odd numbers is: " + countOddNums(array, 0));
array = new int[]{0, 2, 4, 6, 8, 10};
System.out.println("The sum of the odd numbers is: " + countOddNums(array, 0));
printSpaces(null);
printSpaces("");
printSpaces("spaces");
printSpaces("s p a c e s ");
System.out.println(multByAdd(6, 4));
System.out.println(multByAdd(1, 1000));
System.out.println(multByAdd(1000, 0));
System.out.println(multByAdd(0, 1000));
System.out.println(multByAdd(-10, 44));
System.out.println(weave("ace", "bdf"));
System.out.println(weave("Rcrini u!", "euso sfn"));
System.out.println(weave("aab", "abbaaa"));
System.out.println(weave("ccdeeeeeeeeeeeeeeeeeeeee", "cdd"));
System.out.println(weave(null, null));
System.out.println(weave(null, "cat"));
System.out.println(weave("dog", null));
System.out.println(weave("Isn't recursion fun?", ""));
System.out.println(weave("", "Isn't recursion fun?"));
}

public static void reversePrint(String s, int i) {
// put your solution here
  
}
  
public static int countOddNums(int[] a, int i) {
//put your solution here
return 0;
}
  
public static void printSpaces(String s) {
//put your solution here
}
  
public static int multByAdd(int n, int m) {
//put your solution here
return 0;
}
  
public static String weave(String s1, String s2) {
//put your solution here
return "";
}
}

AND THE OUTPUT OF THE PROGRAM SHOULD LOOK LIKE THE OUTPUT:

The string is empty or does not exist. The string is empty or does not exist. Reverse print: esrever Reverse print: Recursion
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:

JavaRecursions.java

package classess5;
public class JavaRecursions {

   public static void main(String[] args) {
       reversePrint(null, 0);
       reversePrint("", 0);
       reversePrint("reverse", 0);
       System.out.println();
       reversePrint("!nuf si noisruceR", 0);
       System.out.println();
       int[] array = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
       System.out.println("The sum of the odd numbers is: " + countOddNums(array, 0));
       System.out.println("The sum of the odd numbers is: " + countOddNums(array, 9));
       array = null;
       System.out.println("The sum of the odd numbers is: " + countOddNums(array, 0));
       array = new int[]{0, 2, 4, 6, 8, 10};
       System.out.println("The sum of the odd numbers is: " + countOddNums(array, 0));
       printSpaces(null);
       printSpaces("");
       printSpaces("spaces");
       printSpaces("s p a c e s ");
       System.out.println(multByAdd(6, 4));
       System.out.println(multByAdd(1, 1000));
       System.out.println(multByAdd(1000, 0));
       System.out.println(multByAdd(0, 1000));
       System.out.println(multByAdd(-10, 44));
       System.out.println(weave("ace", "bdf"));
       System.out.println(weave("Rcrini u!", "euso sfn"));
       System.out.println(weave("aab", "abbaaa"));
       System.out.println(weave("ccdeeeeeeeeeeeeeeeeeeeee", "cdd"));
       System.out.println(weave(null, null));
       System.out.println(weave(null, "cat"));
       System.out.println(weave("dog", null));
       System.out.println(weave("Isn't recursion fun?", ""));
       System.out.println(weave("", "Isn't recursion fun?"));
   }

   public static void reversePrint(String s, int i) {
       // put your solution here
       if(s==null || s.length()==0){
           System.out.println("The string is empty or does not exist.");
       }else{
           if(i>=s.length()){
               return;
           }else{
               if(i==0){
                   System.out.print("Reverse print : ");
               }
               System.out.print(s.charAt(s.length()-i-1));
               reversePrint(s,i+1);
           }
       }
   }

   public static int countOddNums(int[] a, int i) {
       if(a==null){
           return 0;
       }else if(i>=a.length){
           return 0;
       }else{
           if(a[i]%2==0){
               return countOddNums(a,i+1);
           }else {
               return a[i]+countOddNums(a,i+1);
           }
       }
   }

   public static void printSpaces(String s) {
       if(s==null || s.length()==0){
           System.out.println();
           return ;
       }else{
           System.out.print(s.charAt(0)+" ");
           printSpaces(s.substring(1));
       }
       //put your solution here
   }

   public static int multByAdd(int n, int m) {
       if(m<=0){
           return 0;
       }else{
           return n+multByAdd(n, m-1);
       }
   }

   public static String weave(String s1, String s2) {
       if(s1==null || s2==null){
           System.out.println("Can't weave two strings if one or both strings are null");
           return "";
       }else if(s1.length()==0 && s2.length()==0){
           return "";
       }else{
           if(s1.length()>0 && s2.length()>0){
               return   s1.charAt(0)+""+s2.charAt(0)+weave(s1.substring(1),s2.substring(1));
           }else if(s1.length()>0){
               return   s1.charAt(0)+weave(s1.substring(1),s2);
           }else if(s2.length()>0){
               return   s2.charAt(0)+weave(s1,s2.substring(1));
           }else{
               return "";
           }

       }
   }

}

output:

Quick Access : Java EE * Java * Debug & Console X * * * B SEO, <terminated> JavaRecursions [Java Application] C:\Program Fil

Add a comment
Answer #2

//Java program

public class RecursionApp {
  
public static void main(String[] args) {
reversePrint(null, 0);
reversePrint("", 0);
reversePrint("reverse", 0);
System.out.println();
reversePrint("!nuf si noisruceR", 0);
System.out.println();
int[] array = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
System.out.println("The sum of the odd numbers is: " + countOddNums(array, 0));
System.out.println("The sum of the odd numbers is: " + countOddNums(array, 9));
array = null;
System.out.println("The sum of the odd numbers is: " + countOddNums(array, 0));
array = new int[]{0, 2, 4, 6, 8, 10};
System.out.println("The sum of the odd numbers is: " + countOddNums(array, 0));
printSpaces(null);
printSpaces("");
printSpaces("spaces");
printSpaces("s p a c e s ");
System.out.println(multByAdd(6, 4));
System.out.println(multByAdd(1, 1000));
System.out.println(multByAdd(1000, 0));
System.out.println(multByAdd(0, 1000));
System.out.println(multByAdd(-10, 44));
System.out.println(weave("ace", "bdf"));
System.out.println(weave("Rcrini u!", "euso sfn"));
System.out.println(weave("aab", "abbaaa"));
System.out.println(weave("ccdeeeeeeeeeeeeeeeeeeeee", "cdd"));
System.out.println(weave(null, null));
System.out.println(weave(null, "cat"));
System.out.println(weave("dog", null));
System.out.println(weave("Isn't recursion fun?", ""));
System.out.println(weave("", "Isn't recursion fun?"));
}

public static void reversePrint(String s, int i) {
   if(s==null || s.length()==0) {
       System.out.println("The string is empty or does not exit");
       return;
   }
   if(i==s.length())return;
   reversePrint(s,i+1);
   System.out.print(s.charAt(i));
  
}
  
public static int countOddNums(int[] a, int i) {
   if(a==null || a.length==0) {
       System.out.println("The string is empty or does not exit");
       return 0;
   }
   if(i==a.length)return 0;
   if(a[i]%2==1) {
       return a[i] + countOddNums(a,i+1);
   }
   return countOddNums(a,i+1);
}
  
public static void printSpaces(String s) {
   if(s==null || s.length()==0) {
       System.out.println("The string is empty or does not exit");
       return;
   }
   if(s.length()==0)return;
   System.out.print(s.charAt(0)+" ");
   printSpaces(s.substring(1));
}
  
public static int multByAdd(int n, int m) {
   if(m==0 || n==0)return 0;
   return n + multByAdd(n, m-1);
}
  
public static String weave(String s1, String s2) {
   if(s1==null || s2==null) {
       System.out.println("Can't weave two strings if one or both strings are null");
       return "";
   }
   if(s1.isEmpty() || s2.isEmpty()) {
   return s1 + s2;
   }
   return s1.substring(0, 1) + s2.substring(0, 1) + weave(s1.substring(1), s2.substring(1));

}
}
//sample output

<terminated> RecursionApp [Java Application] C:\Program Files Java\jdk1.8.0_151\bin\javaw.exe The string is empty or does not

Add a comment
Know the answer?
Add Answer to:
Write a method called printReverse() that takes a string and uses recursion to print the contents...
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
  • Write a static method called printWithSpaces that takes a String as its parameter and prints the...

    Write a static method called printWithSpaces that takes a String as its parameter and prints the characters of the string separated by spaces. For example: > Methods.printWithSpaces("method") m e t h o d You should have a single space after the last character. This method should not return a value. That is similar to this code for printing the string vertically public static void printVertical(String s) { for (int i = 0; i < s.length(); i++) { char c =...

  • JAVA Write a method that accepts a String as an argument. The method should use recursion...

    JAVA Write a method that accepts a String as an argument. The method should use recursion to display each individual character in the String. Then, modify the method you just wrote so it displays the String backwards. The following code does not correctly display the String backwards. It merely moves the first character of the String to the end: public static void displayCharacter(String s)    {        if(s.length() == 0)            return;        else       ...

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • 2. public static string trim(String str) This method should take a string str and use recursion...

    2. public static string trim(String str) This method should take a string str and use recursion to return a string in which any leading and/or trailing spaces in the original string are removed. For example: trim(" hello world ") should return the string "hello world" trim("recursion ") should return the string "recursion" The String class comes with a built-in trim() method that does the same thing as the method that we're asking you to write; you may not use that...

  • This method takes an array of Strings as a parameter and has no return value. The...

    This method takes an array of Strings as a parameter and has no return value. The purpose of this method is to count all the characters in an array of strings. Count the number of characters in every string in the array and assign to numberChars. Count the number of lowercase letters in every string in the array and assign to numberLower. Count the number of uppercase letters in every string in the array and assign to numberUpper. Count characters...

  • Q1) Write a method called inToTree of ExpressionTree class which takes a string of infix expression...

    Q1) Write a method called inToTree of ExpressionTree class which takes a string of infix expression (fully parenthesized), resets the root of the expression tree to such tree which constructed from the expression. The root set to null of the expression contains characters other than value, operator, and parenthesis or if it’s not an in-fix full parenthesized expression. You may assume each value is single digit only . public void inToTree(String expression){ You may use a JCF Deque class as...

  • JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers,...

    JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers, and returns the sum of all the odd numbers in the array. The method should return 0 if the array contains no odd numbers. For example, if the given array is [12, 10, 5, 8, 13, 9] then the method should return 27 (5+13+9=27). Starter Code: public class Odds { public static int sumOdds(int[] numbers) { //TODO: complete this method    } }

  • Write java class “BinarySearch” that uses recursion to find the index of a target value in...

    Write java class “BinarySearch” that uses recursion to find the index of a target value in an ascending sorted array. If not found, the result is -1. The array has to be unsorted before you sort it. Use “ BinarySearchDriver.java” to drive the “BinarySearch” class /************************************************************* * BinarySearchDriver.java * Student Name * *. *************************************************************/ public class BinarySearchDriver { public static void main(String[] args) {     int[] array = new int[] {55, 88, 33, 5, 8, 12, 16, 23, 45}; /unsorted...

  • Write a method named printReverse() that accepts an int array as its parameter and prints that...

    Write a method named printReverse() that accepts an int array as its parameter and prints that array in reverse order. This method should not have a return statement (void). - All elements should be printed on the same line, separated by a space - A new line should be printed after the entire array prints - If the array passed in held the values: {3,2,1} - Output: 1 2 3 //newline printed here

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
ADVERTISEMENT