In: Computer Science
**Program #2: Working with Generics
Implement the following method that returns the maximum element in an array.
public static <E extends Comparable<E>> E max(E[] list)
What should you do?
Write a program to test the method above with various types.
Deliverables:
Deliverable format: The above deliverables should be packaged in the following format.
Unencrypt.java, Permutations.java, and MaxWithGenerics.java
Check List
# |
Y/N |
Comments |
|
Source java files |
|||
Files: |
|||
LastNameFirstinitial_Project04.zip |
|||
LastNameFirstInitial_Project04.doc |
|||
Program compiles |
|||
Program runs |
|||
Checklist is completed and included in the Documentation |
|||
Documentation file: |
|||
Comprehensive Test Plan |
|||
Screenshots of running program |
|||
UML diagram |
|||
Lessons Learned |
|||
Checklist |
Hi. I have answered the same question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Make sure you copy below two classes into separate files as mentioned. Do not copy everything to a single file.
//MaxWithGenerics.java
public class MaxWithGenerics {
// required method
public static <E extends Comparable<E>> E max(E[] list) {
// if array is null or is empty, returning null
if (list == null || list.length == 0) {
return null;
}
// otherwise setting first element as maxVal
E maxVal = list[0];
// looping through remaining elements
for (int i = 1; i < list.length; i++) {
// if current element is greater than maxVal, setting current
// element
// as maxval
if (list[i].compareTo(maxVal) > 0) {
maxVal = list[i];
}
}
// returning maxVal
return maxVal;
}
}
//Test.java
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
// creating arrays of different Comparable types
String[] names = { "John", "Alice", "Damien", "Bran" };
Integer[] integers = { 1, 0, 3, -2, 8, 5 };
Double[] doubles = { 3.0, 5.9, 2.9, 1.5, 6.6, 4.3, 1.1 };
Character[] letters = { 'A', 'Z', 'D', 'X' };
// displaying arrays and max value of each of them
System.out.println("Names: " + Arrays.toString(names));
System.out.println("Max value: " + MaxWithGenerics.max(names));
System.out.println("Integers: " + Arrays.toString(integers));
System.out.println("Max value: " + MaxWithGenerics.max(integers));
System.out.println("Doubles: " + Arrays.toString(doubles));
System.out.println("Max value: " + MaxWithGenerics.max(doubles));
System.out.println("Letters: " + Arrays.toString(letters));
System.out.println("Max value: " + MaxWithGenerics.max(letters));
}
}
/*OUTPUT*/
Names: [John, Alice, Damien, Bran]
Max value: John
Integers: [1, 0, 3, -2, 8, 5]
Max value: 8
Doubles: [3.0, 5.9, 2.9, 1.5, 6.6, 4.3, 1.1]
Max value: 6.6
Letters: [A, Z, D, X]
Max value: Z
/*UML diagram*/