In: Computer Science
Task #1 Develop a recursive method to reverse a list Develop a method with the prototype public static void reverse (ArrayList inputList) based on selecting the first list element as the head and the remaining list as its tail. Here is the recursive design. 1) Base case: The problem is trivial when the list size is 0 or 1. 2) Decomposition: For lists with size > 1: a) Extract its head (element) and leave the tail (the input list with the head removed). You can look up the method that does this for the List interface. b) Make a recursive call to obtain the tail reversed. Page 2 of 3 3) Composition: Append the extracted head element to the reversed tail obtain the original list reversed. Task #2 Develop a recursive method to find the maximal element Note that this is not possible unless the list elements are comparable to each other. Java provides a generic Interface for this called Comparable. Based on this, develop a method with the following prototype public static > E max(List inputList) You can use the same problem decomposition technique as in Task #1. Think about how the composition of result should be made. Task #3 Develop a recursive method to sum the list elements Obviously, this is not possible unless the list elements are of numeric type. Develop a recursive summing method with the prototype public static double sum (List inputList) Task #4 Use command line arguments to supply the list elements Use the following main() method: public static void main(String args[]) { ArrayList argList = new ArrayList<>(); ArrayList numericArgs = new ArrayList<>(); for (String s : args) { argList.add(s); try { numericArgs.add(Double.parseDouble(s)); } catch (NumberFormatException e) { System.out.println(e.getMessage() + "is not numeric...skipping"); } } System.out.print("Command line arguments before reversal: "); for (int i=0; i
public class Main {
// methods to implement
public static String reverseRecursiveLeft (String input) {
// base case
if(input.length() <= 1) {
// if string have 1 or less character left return the string
return input;
}
// recursive solution
// for string greater then 1 character long divide it in to head
and tail part
String head = input.substring(0, 1); // get first character from
substring method
String tail = input.substring(1); // get remaining character from
substring method
// make recursive call to obtain tail in reverse and append head to
it
return reverseRecursiveLeft(tail) + head;
}
public static String reverseRecursiveRight (String input)
{
// base case
if(input.length() <= 1) {
// if string have 1 or less character left return the string
return input;
}
// recursive solution
// for string greater then 1 character long divide it in to head
and tail part
String head = input.substring(input.length()-1); // get the right
most character from substring method
String tail = input.substring(0,input.length()-1); // get remaining
character from substring method
// make recursive call to obtain tail in reverse and append it to
head
return head + reverseRecursiveLeft(tail);
}
public static String reverseRecursiveMiddle (String input) {
// base case
if(input.length() <= 1) {
// if string have 1 or less character left return the string
return input;
}
else if(input.length()==2) {
// if string have 2 character return them in reverse order
String left = input.substring(0, 1);
String right = input.substring(1, 2);
return right + left;
}
// recursive solution
// for string greater then 2 character long divide it in to head,
middle and tail part
String head = input.substring(0, 1); // get the left most character
from substring method
String tail = input.substring(input.length()-1); // get the right
most character from substring method
String middle = input.substring(1,input.length()-1); // get
remaining character from substring method
// make recursive call to obtain middle in reverse and append it to
tail then append the head
return tail + reverseRecursiveLeft(middle) + head;
}
public static int reverse (int input) {
// check if input is negative
if(input < 0) {
// turn input in to positive integer as only digits are
reversed
input = input * -1;
// convert integer to string with wrapper class
String str = Integer.toString(input);
// reverse the string
String reverseDigit = reverseRecursiveLeft(str);
// convert string to integer
int integer = Integer.parseInt(reverseDigit);
// return the integer with negative value
return integer*-1;
}
else {
// convert integer to string with wrapper class
String str = Integer.toString(input);
// reverse the string
String reverseDigit = reverseRecursiveLeft(str);
// convert string to integer
int integer = Integer.parseInt(reverseDigit);
// return the integer
return integer;
}
}
static boolean isPalindrome (String input) {
// base case
if(input.length() <= 1) {
// if string have 1 or less character left return true as the
character is in middle of the string
return true;
}
else if(input.length()==2) {
// if string have 2 character check are they equals
String left = input.substring(0, 1);
String right = input.substring(1, 2);
return left.equals(right);
}
// recursive solution
// for string greater then 2 character long divide it in to head,
middle and tail part
String head = input.substring(0, 1); // get the left most character
from substring method
String tail = input.substring(input.length()-1); // get the right
most character from substring method
String middle = input.substring(1,input.length()-1); // get
remaining character from substring method
// make recursive call to check if middle is palindrome
// compare head and tail and return the result with logic and with
middle
return head.equals(tail) && isPalindrome(middle);
}
public static boolean isPalindrome (int input) {
// check if input is negative
if(input < 0) {
// turn input in to positive integer as only digits are counted for
palindrome
input = input * -1;
}
// convert integer to string
String str = Integer.toString(input);
// return if string is in palindrome
return isPalindrome(str);
}
// main method to run program
public static void main(String args[]) {
String str = "hello world!";
System.out.println("Reverse of ");
System.out.println(str);
System.out.println("by reverseRecursiveLeft is:");
System.out.println(reverseRecursiveLeft(str));
System.out.println("by reverseRecursiveRight is:");
System.out.println(reverseRecursiveRight(str));
System.out.println("by reverseRecursiveMidlle is:");
System.out.println(reverseRecursiveMiddle(str));
System.out.println();
int p = 423651;
System.out.print("Reverse of ");
System.out.print(p);
System.out.print(" is: ");
System.out.println(reverse(p));
System.out.println();
int n = -54637;
System.out.print("Reverse of ");
System.out.print(n);
System.out.print(" is: ");
System.out.println(reverse(n));
System.out.println();
String s = "AbcDcbA";
System.out.print(s);
System.out.print(" is palindrome? ");
System.out.println(isPalindrome(s));
System.out.println();
System.out.print(n);
System.out.print(" is palindrome? ");
System.out.println(isPalindrome(n));
int pali = 1258521;
System.out.print(pali);
System.out.print(" is palindrome? ");
System.out.println(isPalindrome(pali));
}
}