Question

In: Computer Science

java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...

java/netbeans

  • Write a recursive method, reverseString, that accepts a String and returns the String reversed.
  • Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList.
  • Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the ArrayList.

Solutions

Expert Solution

All the explanations is given in the comments of the code itself.

Code--
import java.util.*;
public class Reverse
{
   //method to reverse a string
   public static String reverseStr(String st)
   {
       //base condition for recursion
       if(st.isEmpty())
           return st;
       else
       {
           //remove the first character of the string
           //concat it with the returned string
           //and pass the string again recursively without the
           //first character
           return reverseStr(st.substring(1))+st.charAt(0);
       }
   }
   //method to reverse Arraylist
   public static ArrayList<String> reverseAL(ArrayList<String> al)
   {
       if(al.size()>1)
       {
           //remove the first node of the al
           String temp=al.remove(0);
           //then again call reverseAL
           reverseAL(al);
           //add the temp to al
           al.add(temp);
       }
       //base condition for recursion that is if the al is empty
       return al;
   }
   //main method
   public static void main(String[] args)
   {
       Scanner sc=new Scanner(System.in);
       ArrayList<String> al = new ArrayList<String>();
       String temp;
       int size=0;
       //prompt the user to give inputs until Done is entered
       System.out.println("Enter Inputs strings: ");
       while(true)
       {
           temp=sc.nextLine();
           if(temp.equals("Done"))
           {
               break;
           }
           else
           {
               al.add(temp);
               size++;
           }
       }
       //iterate over the array list and reverse the strings using the above function
       for(int i=0;i<size;i++)
       {
           temp=reverseStr(al.get(i));
           al.set(i, temp);
       }
       //call the function to reverse the arraylist
       al=reverseAL(al);
       //display the output after reversals
       System.out.println("Output");
       for(int i=0;i<size;i++)
       {
           System.out.println(al.get(i));
       }
   }
}

Code Screenshot--


Output Screenshot--

Note--

Please upvote if you like the effort.


Related Solutions

Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
​Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.
Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.(JAVA Code)
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT