Instructions:CS 2323 HW 12
Palindromes
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.
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
T>
Type of the ArrayList
*/
static T> void testCode(ArrayListT> 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
===============================================
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