Question

*Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

*Java*

Hi. I need some help with creating generic methods in Java.

Write a program GenMethods that has the following generic methods:

(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  ArrayList removeDuplicates(ArrayList 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  void shuffle(ArrayList list)

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

public static > E max(ArrayList list) 

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

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

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

public static > 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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ceate oove o elemets as ltue dopeade as melluod udt accepts ot of dopades ,audl Stens te ne creadte o Clay publ足.ctHE wil naestal add !) , tet add(00

Add a comment
Know the answer?
Add Answer to:
*Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • Hi - So again I'm having some trouble getting started. Here is my assignment: For this...

    Hi - So again I'm having some trouble getting started. Here is my assignment: For this assignment we’ll be creating a new class called, Utilities . The Utilities class will contain two methods; removeDuplicates and linearSearch RemoveDuplicates public<T> voidremoveDuplicates(finalList<T> items) Write a generic method that removes duplicate items from the supplied List . For example: List<String> strings = new ArrayList<>(); strings.add("one"); strings.add("two"); strings.add("one"); Utilities utilities = new Utilities(); utilities.removeDuplicates(strings); The result is the list of strings contains two records; “one”...

  • Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for...

    Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for ArrayList: first method finds the maximum element in an array list; the second method computes the sum of all elements in an array list; and the third method combines (union) two lists by adding the second list to the first list). Use Integer type for all methods. See problem statements for methods signatures and sample test data. Write one separate test program to test...

  • Need help with this Java. I need help with the "to do" sections. Theres two parts...

    Need help with this Java. I need help with the "to do" sections. Theres two parts to this and I added the photos with the entire question Lab14 Part 1: 1) change the XXX to a number in the list, and YYY to a number // not in the list 2.code a call to linearSearch with the item number (XXX) // that is in the list; store the return in the variable result 3. change both XXX numbers to the...

  • "Java question" Implement a generic method that returns the minimum element in an array.        public...

    "Java question" Implement a generic method that returns the minimum element in an array.        public static<EextendsComparable<E>> E min(E[ ] list)

  • java Write a generic method that takes an ArrayList of objects (of a valid concrete object...

    java Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects

  • Code in java Using the template below: public class Lab03 { public static void main(String[] args)...

    Code in java Using the template below: public class Lab03 { public static void main(String[] args) { ArrayList<Integer> list1 = new ArrayList<Integer>(); Collections.addAll(list1, 1, 3, 5, 5 ); ArrayList<Integer> list2 = new ArrayList<Integer>(); Collections.addAll(list2, 3, 7, 3, 2, 4 ); ArrayList<Integer> result1= uniqueUnion(list1,list2); System.out.println(result1); Integer[] array = new Integer[]{ 29, 28, 27, 16, 15, -14, 3, -2, 2}; ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(array)); System.out.printf("The average is: %.2f%n", averagePositive(arrayList)); } // end main public static ArrayList<Integer> uniqueUnion(ArrayList<Integer> list1, ArrayList<Integer> list2) {...

  • The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main...

    The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main with a single generic method that produces the same output. Call the new file GenericMethodsTest.java. OverloadMethods.java: // Printing array elements using overloaded methods. public class OverloadedMethods { // method printArray to print Integer array public static void printArray(Integer[] inputArray) { // display array elements for (Integer element : inputArray) { System.out.printf("%s ", element); } System.out.printf("\n"); } // method printArray to print Double array public...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT