Question

In: Computer Science

**Program #2: Working with Generics Implement the following method that returns the maximum element in an...

**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:

  • There are 2 separate programs to complete. Place them both in the same package.
  • Projects submitted with evidence of plagiarism will be given a score of 0.
  • Java files (source code)
  • Word document should include:
    • Screen snapshots of outputs
    • Lessons Learned
    • Check List

Deliverable format: The above deliverables should be packaged in the following format.

  • lastNameFirstInitial_ProjectXX.doc
  • lastNameFirstInitial_ProjectXX.zip [a compressed folder containing the package that has all files necessary for the program(s) to run]

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

Solutions

Expert Solution

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*/


Related Solutions

Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
   Method sandwiched returns true if num is in the element before and after       ...
   Method sandwiched returns true if num is in the element before and after            an element that is not equal to num           sandwiched([4,5,4,6,7,3], 4) returns true           sandwiched([2,1,2], 2) returns true           sandwiched([3,3,3], 3) returns false           sandwiched([4,5,6,4], 4) returns false            sandwiched([1,1,2,3,1,4,1], 1) returns true           @param nums Integer ArrayList            @param num integer           @return true if a...
In java, implement a program that returns a value of the area of the geometric objects...
In java, implement a program that returns a value of the area of the geometric objects listed below. Use the technique of method overloading concept and also uses the interfaces for dynamic method invocation. Triangle: √?(? − ?)(? − ?)(? − ?) where s=(a+b+c)/2 a,b,c are the sides of the triangle. Square: A^2 where A is the side Rectangle: (a*b) where a ,b are sides Circle: ? = ??2 where r is the radius Cube: 6a^2 where a is the...
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a method in JAVA, called binarySearch, that returns the index of the key element if...
Write a method in JAVA, called binarySearch, that returns the index of the key element if found in the array, otherwise it will return the value of minus(insertion point +1). In main, use an initializer list create an array of ints called nums holding the following values: 1, 4, 13, 43, -25, 17, 22, -37, 29. Test your binarySearch method on nums with a key value number in it (e.g. 4) and one that is not (e.g. 100). Ex. Given...
Code with Java and no imports, Method sandwiched returns true if num is in the element...
Code with Java and no imports, Method sandwiched returns true if num is in the element before and after an element that is not equal to num sandwiched([4,5,4,6,7,3], 4) returns true sandwiched([2,1,2], 2) returns true sandwiched([3,3,3], 3) returns false sandwiched([4,5,6,4], 4) returns false sandwiched([1,1,2,3,1,4,1], 1) returns true @param nums Integer ArrayList @param num integer @return true if a single number is between elements equal to num */ public static boolean sandwiched(ArrayList nums, int num) { return false; }//end sandwiched
Problem Description: Write a method that returns the smallest element in a specified column in a...
Problem Description: Write a method that returns the smallest element in a specified column in a matrix using the following header: public static double columnMin(double[][] m, int columnIndex) Write a program that reads from the keyboard the number of rows and columns, and an array of double floating point numbers with the specified number of rows and columns. The program will then invoke the columnMin method and displays the minimum value of all columns.
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Write a short program that performs the following: - Method city returns the string "Seattle": -...
Write a short program that performs the following: - Method city returns the string "Seattle": - Method named state returns the string "Washington ": - Method named report calls the city methodand calls the state method. and prints their return values, so that the output will appear as : "Seattle Washington "
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT