Objectives

  • Explore use of Comparable interface.
  • Use Collections and/or Collection in the solution
  • Apply generics to methods using arrays.
  • Apply generics to methods using List ADTs.

Generic Methods

Use of generic parameters gives us "type erasure," that is, the symbol 'E', 'T', 'K', 'V' (or whichever may be used), is replaced at compile time with the data type indicated within diamond syntax. Generics can be applied to individual methods in addition to classes. In this lab, our methods, but not our class, will use generic parameters.

Instructions for GenMethods.java

Write a program GenMethods that has the following generic methods. (Also include a getIdentificationString method again)

(1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list.

public static < E> ArrayList< E>
removeDuplicates(ArrayList< E> list)

(2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand = new Random(340L); ), and it should do 30 of these swaps (this number was chosen arbitrarily by us for testing purposes).

public static < E> void shuffle(ArrayList< E> list)

(3) Write the following method that returns the largest element in an ArrayList:

public static < E extends Comparable< E>> E
max(ArrayList< E> list)

(4) Implement the following generic method for linear search.

public static < E extends Comparable< E>> int
linearSearch(E[] list, E key)

(5) Implement the following method that returns the maximum element in an array:

public static < E extends Comparable< E>> E max(E[] list)

(6) Implement a generic method that returns the maximum element in a two- dimensional array.

public static > E max(E[][] list)

(7) Write the following main() method that tests the above methods following this guide:

Read in a number n that represents the number of elements
in the lists.

Read in n elements to initialize and fill an array of
Integers named 'list' while simultaneously initializing a
linked list of Integers named 'linked' from the same
input.

Print 'list'. (use Arrays.toString(array))

Print 'linked' (just put 'linked' into print statement)

Read in k key value to search for in list.

Call linearSearch(list, k) and print the result: Key k
was found at position result, or Key k was not found.

Call max(list) and print the result: 'Result' is the max
element

Read in an integer m for first dimension of a 2-D array.

Read in an integer p for second dimension of a 2-D array.

Initialize a 2-D array using m and p named 'list2'

Read in m x p elements to fill 'list2'.

Print 'list2'. You can not just use a single print
statement, but will instead need to implement nested for
loops. (Format: rows of data on separate lines with a
space in between each data)
Example:
1 2 3 4
2 3 4 5
3 4 5 6

Call max(list2) and print the result: 'Result' is the max
element

Instantiate an ArrayList of type Integer named 'alist'
from 'linked' (meaning 'alist' is a copy of 'linked')

Print out 'alist' using System.out.println(alist);

Call removeDuplicates using 'alist' as the parameter

Print the now unique 'alist' using
System.out.println(alist);

Call shuffle using 'alist' as the parameter.

Print 'alist' again using System.out.println(alist);

Find the max element of 'alist' and print: 'Result' is
the max element
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.