Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

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 »
Instructions:
CS 2323 HW 12
 
1. Palindromes
a. Write a static generic method displayPairs which takes a generic A
ayList 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.
. Write a Boolean static generic method that returns true if the elements of the a
ayList parameter are palindrome.
Sample code:
**
 * Tests if a given A
ayList is a palindrome and displays its values to the screen
 * @param list  A Generic A
ayList
 * @param   Type of the A
ayList
 *
static  void testCode(A
ayList 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[]) {
    A
ayList simple = new A
ayList
(A
ays.asList("One Value"));
    A
ayList ints = new A
ayList
(A
ays.asList(10,20,30,20,10));
    A
ayList names = new A
ayList
(A
ays.asList
            ("Mary","John","Bob","Ralph","Bob","John","Mary"));
    A
ayList numbers = new A
ayList
(A
ays.asList
            (1.1,3.0,2.2,9.9,3.0,1.1));
    A
ayList passNumbers = new A
ayList
(A
ays.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|Bo
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|Bo
===============================================
 
2. Reverse
Write a static method that returns the reverse of a generic list, without modifying the original.
Sample code
Test 1
A
ayList people = new A
ayList
(A
ays.asList (
        "Alice","Bob","Charlie","David","Eric","Fred"));
A
ayList reversePeople=reverseList(people);
System.out.println("Before:"+people);
System.out.println("After:"+reversePeople);
Test 2
A
ayList dblIn = new A
ayList
(A
ays.asList (
        10.0,-23.5,57.0,19.7,-4.1,3.14159));
A
ayList 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, XXXXXXXXXX]
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 A
ayList
2 Copy elements in reverse orde
2 Documentation
Answered Same Day Nov 06, 2021

Solution

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

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here