Starting from:

$25

Assignment №9 - Implementing Generic Methods

Implementing Generic Methods Definition 1. A palindrome is a word, phrase, or sequence that reads the same backward as forward. An array is a palindrome if its elements read the same backward as forward. It can be recursively determined whether or not the contents of an array is a palindrome: 1. If the length of the array is 1 or less, it is a palindrome. 2. If its length is 2 and its first and last elements match, it is a palindrome; otherwise, it is not. 3. If its length is more than 2, if its first and last elements do not match, it is not a palindrome. However, if they do, then recursively determine whether the subarray constructed by excluding the first and last elements is a palindrome. Definition 2. Generics or genericity is a way of defining classes, interfaces and methods by using type variables to represent types. A more technical term for generics is parametric polymorphism. In class we discussed these three kinds of generic programming in Java. In today’s lab, you will only implement generic methods. The class containing the methods will not be generic but the methods will be. Recall that when writing generic methods, the type parameters are placed in angle brackets before the return type of the methods. This lab exercise also affords you an opportunity to revisit writing recursive methods. In order to review some of the concepts that you studied in class, the code you write may be slightly less efficient. The Palindrome Class Define a class Palindrome that consists of the following generic methods: 1. public static boolean isPalindrome(E[] data): this method returns true if the array is palindromic; otherwise, false. Implement this method using the recursive definition given above. 2. private static E getFirst(E[] data): an auxiliary method used in the implementation of the isPalindrome method; it returns the first element of the specified array. 3. private static E getLast(E[] data): an auxiliary method used in the implementation of the isPalindrome method; it returns the last element of the specified array. Assignment № 9 Page 1 4. public static E[] reverse(E[] data): this method constructs and returns a new array whose type is the same as the type of the specified array and whose contents is the contents of the specified array in reverse. 5. public static <U,V boolean equals(U[] data1, V[] data2): determines whether or not the two specified arrays are equal. The two arrays are equal if the have the same type (belong to the same class), have they same length, and have the same contents; otherwise, they are not equal. You may implement the equals method using iteration, which is pretty straightforward, or recursion if you feel a little bit ambitious. Some Useful Java API Methods 1. .split(regex): is an instance method in the String class that returns an array of strings using the regex as a delimiter for the string that invokes the method. For example, "Hello".split(""); returns the array {"H", "e", "l", "l", "o"}. 2. String.join(delimiter,String[]): is a static method of the String class that converts an array of strings to a string using the specified delimiter between its elements. For example, given String[] greeting = {"H", "e", "l", "l", "o"};, String.join("",greeting) gives the string“Hello". It is the opposite of the split method. 3. In Java, a generic array cannot be instantiated. In order to copy a generic array, use the Arrays.copyOfRange method: newArray = Arrays.copyOfRange(srcArray,startIndex,endIndex+1). There are other copy methods in the Java API that do exactly the same thing. An alternate, albeit tedious, way to do this is to create an array of type Object and then typecast it into the generic type using (E[]), where E is the type variable and then copy the data from the source array using a loop. 4. Since the instanceof operator does not work with generic types, you may find the getClass() method, which returns the name of the class the object belong to as a string, handy when checking for the actual type used to specialize a generic type. 5. Arrays.toString(arrayName): gives a string representation of the array as a set in a pair of square brackets with commas as delimiters between elements. It requires the java.util package. This will come in handy when you need to print the contents of an array without the explicit use of a loop. Assignment № 9 Page 2 PalindromeDemo Define a class PalindromeDemo, the main class of the program. This class will consist of only one method, the main method. In this class, do the following: 1. Define and print an Integer[] array. 2. Using the Palindrome.isPalindrome method, determine whether or not the array is a palindrome and display the array and a message indicating so. 3. Using the Palindrome.reverse method, construct a new array whose elements are those of the array created in step 1 in reverse. Print the array. 4. Again, using the Palindrome.isPalindrome method, determine whether or not the array constructed in step 3 is a palindrome and display the array and a message indicating so. 5. Repeat steps 1-4 using an array of characters (Character[]), this time. 6. Repeat steps 1-4 using a one-word string. You will need to convert the string to an array of strings and join the array to print it as a single string. Additional Requirements Exhaustively, test your program to ensure that it works. Be sure to use the .equals method when comparing elements of the arrays since those elements are objects and not primitive types. Write header comments for each class using the following Javadoc documentation template: /** * Explain the purpose of this class; what it does
* CSC 1350 Lab # 9 * @author YOUR NAME * @since DATE THE CLASS WAS WRITTEN */ For the PalindromeDemo class, after the @since line, add the following Javadoc: * @see Palindrome Add Javadoc style documentation for each method in the Palindrome class. Run the Javadoc utility to make sure that it generates documentation for these classes. Locate your source files, Palindrome.java and PalindromeDemo.java and enclose them in a zip file, YOURPAWSID_lab09.zip, and submit your lab assignment for grading using the digital dropbox set up for this purpose. Assignment № 9 Page 3 Here is a sample program interaction: Listing 1: Sample Run 1 1 Using an Integer Array: 2 Original: [2, 3, 5, 7, 11, 13] 3 [2, 3, 5, 7, 11, 13] is not a palindrome. 4 Reversed Copy: [13, 11, 7, 5, 3, 2] 5 [2, 3, 5, 7, 11, 13] and [13, 11, 7, 5, 3, 2] are not equal. 6 7 Using a Character Array: 8 Original: [m, a, r, j, o, r, a, m] 9 [m, a, r, j, o, r, a, m] is not a palindrome. 10 Reversed Copy: [m, a, r, o, j, r, a, m] 11 [m, a, r, j, o, r, a, m] and [m, a, r, o, j, r, a, m] are not equal. 12 13 Using a String: 14 Original: racecar 15 racecar is a palindrome. 16 Reversed Copy: racecar 17 racecar and racecar are equal. Assignment № 9 Page 4

More products