Instructions: CS 2323 HW 12 1. Palindromes a. Write a static generic method displayPairs which takes a generic ArrayList as its parameter and displays those elements by paring the first and last...

1 answer below »
n/a


Instructions: CS 2323 HW 12   1. Palindromes a. Write a static generic method displayPairs which takes a generic ArrayList as its parameter and displays those elements by paring the first and last elements, second and second to last,  (index i and index n-i-1 are equal) etc each on a line with a “|” character in between them.  If there are an odd of elements the middle most value would displayed alone on a line. b. Write a Boolean static generic method that returns true if the elements of the arrayList parameter are palindrome. Sample code: /**  * Tests if a given ArrayList is a palindrome and displays its values to the screen  * @param list  A Generic ArrayList  * @param   Type of the ArrayList  */ static  void testCode(ArrayList list) {     if (isPalindrome(list))         System.out.println("Is a Palindrome");     else         System.out.println("Is NOT a Palindrome");     displayPairs(list);     System.out.println("==============================================="); } public static void main(String s[]) {     ArrayList simple = new ArrayList<>(Arrays.asList("One Value"));     ArrayList ints = new ArrayList<>(Arrays.asList(10,20,30,20,10));     ArrayList names = new ArrayList<>(Arrays.asList             ("Mary","John","Bob","Ralph","Bob","John","Mary"));     ArrayList numbers = new ArrayList<>(Arrays.asList             (1.1,3.0,2.2,9.9,3.0,1.1));     ArrayList passNumbers = new ArrayList<>(Arrays.asList(-23.5,-23.5));     testCode(simple);     testCode(ints);     testCode(names);     testCode(numbers);     names.add("Mary");     testCode(names); }   Sample Output Is a Palindrome One Value =============================================== Is a Palindrome 10|10 20|20 30 =============================================== Is a Palindrome Mary|Mary John|John Bob|Bob Ralph =============================================== Is NOT a Palindrome 1.1|1.1 3.0|3.0 2.2|9.9 =============================================== Is NOT a Palindrome Mary|Mary John|Mary Bob|John Ralph|Bob ===============================================   2. Reverse Write a static method that returns the reverse of a generic list, without modifying the original. Sample code // Test 1 ArrayList people = new ArrayList<>(Arrays.asList (         "Alice","Bob","Charlie","David","Eric","Fred")); ArrayList reversePeople=reverseList(people); System.out.println("Before:"+people); System.out.println("After:"+reversePeople); // Test 2 ArrayList dblIn = new ArrayList<>(Arrays.asList (         10.0,-23.5,57.0,19.7,-4.1,3.14159)); ArrayList dblOut=reverseList(dblIn); System.out.println("Before:"+dblIn); System.out.println("After:"+dblOut);   Sample Output Before:[Alice, Bob, Charlie, David, Eric, Fred] After:[Fred, Eric, David, Charlie, Bob, Alice] Before:[10.0, -23.5, 57.0, 19.7, -4.1, 3.14159] After:[3.14159, -4.1, 19.7, 57.0, -23.5, 10.0]   Grading Criteria Palindrome 3 isPalindrome method 3 displayPairs method 2 Documentation   Reverse 2 Function Definition 2 Allocate new ArrayList 2 Copy elements in reverse order 2 Documentation
Answered Same DayNov 06, 2021

Answer To: Instructions: CS 2323 HW 12 1. Palindromes a. Write a static generic method displayPairs which takes...

Sudipta answered on Nov 16 2021
122 Votes
import java.util.*;
public class Palindromes {
static boolean isPalindrome(ArrayList ar
rayList){
int n=arrayList.size();
for (int i = 0; i < n / 2; i++) {
if(arrayList.get(i) instanceof String) {
if (!arrayList.get(i).equals(arrayList.get(n - i - 1)))
return false;
}
else {
if (arrayList.get(i) != arrayList.get(n - i - 1))
return false;
}
}
return true;
}
static void displayPairs(ArrayList arrayList){
int n=arrayList.size(),i=0;
for(i=0;i System.out.println(arrayList.get(i)+"|"+arrayList.get(n-i-1));
if(n%2!=0)
System.out.println(arrayList.get(i));
}
static void testCode(ArrayList arrayList){
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here