Need assistance with this problem. This is for a java class and using eclipse.
For this assignment we’ll be doing problems 21.3 and 21.4 from your textbook.
Problem 21.3 asks you to write a generic method that removes duplicate items from an ArrayList. This should be fairly straightforward. Problem 21.4 asks you to implement insertion sort within a generic method. Insertion sort is described in detail on pages 250-252 of your textbook.
You can write your two methods in a new class called Utilities. Please include a main method for this class that tests your methods.
Think carefully about the way you test your program. Developing a good test plan is as important as developing the code itself. Obviously, you should show that your generic methods work for different types of objects, i.e. ArrayLists or arrays of Integer, Strings, FacebookUsers,
etc. You should also test any unusual cases you can think of: what if the ArrayList or array is empty? what if it contains only one item? what if your removeDuplicates method is passed an ArrayList in which every item is identical? etc.
You will be graded according to the following rubric (each item is worth one point)
The removeDuplicates method works on at least some input
The removeDuplicates method is generic and therefore works on ArrayLists of all types of classes
The insertionSort method works on at least some input
The insertionSort method is generic and therefore works on ArrayLists of all types of classes that implement the Comparable interface
There is a main method that tests the operation of the removeDuplicates and insertionSort methods
The main method shows the operation of the removeDuplicates and insertionSort method on various types of classes, including FacebookUsers
The test cases in the main method are logical and thorough
The program compiles
The program runs
The program is clearly written and follows standard coding conventions

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
public class Utilities {
public static <T extends Comparable<T>> void insertionSort(ArrayList<T> numbers) {
for (int i = 1; i < numbers.size(); i++) {
T key = numbers.get(i);
int j = i - 1;
while (j >= 0 && numbers.get(j).compareTo(key) > 0) {
numbers.set(j + 1, numbers.get(j));
j = j - 1;
}
numbers.set(j + 1, key);
}
}
public static <T> void removeDuplicates(ArrayList<T> list) {
HashSet<T> seen = new HashSet<>();
Iterator<T> it = list.iterator();
while(it.hasNext()) {
T next = it.next();
if(seen.contains(next)) {
it.remove();
} else {
seen.add(next);
}
}
}
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>();
nums.addAll(Arrays.asList(10, 20, 20, 6, 3, 14, 13, 23, 18, 13, 17, 13));
System.out.println("Original list: " + Arrays.toString(nums.toArray()));
Utilities.<Integer>removeDuplicates(nums);
System.out.println("After removing duplicates: " + Arrays.toString(nums.toArray()));
Utilities.<Integer>insertionSort(nums);
System.out.println("After sorting: " + Arrays.toString(nums.toArray()));
}
}
Please upvote, as i have given the exact answer as asked in
question. Still in case of any issues in code, let me know in
comments. Thanks!
Need assistance with this problem. This is for a java class and using eclipse. For this...
In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...
Activity 1. Suppose you are implementing a utility class, ArrayListUtil, in which you provide some utility methods to apply on ArrayLists with various types. Provide a static method that reverses the elements of a generic ArrayList. Provide another static method that returns the reverse of a generic ArrayList, without modifying the original list.
(The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...
Create a Bitbucket private repository. Name it “CUS1156_Lab2”. Clone it in your local java Eclipse workspace. You will add the java files after you have completed them in Part 1 and Part 2 to this location. Part 1: Abstract classes From class, an abstract class represents some generic concept. Subclasses then provide their own specific implementations of any abstract methods of the abstract class. 1. Implement an abstract class called Shape it was discussed in the lecture. Include an abstract...
Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make Chicken and Cow as subclasses of Dairy. The Sheep and Dairy classes implement the Edible interface. howToEat) and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML...
*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 =...
Registration Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration”...
Money Lab using arraylists in Java Using the Coin class below create a program with the following requirements Create an arraylist that holds the money you have in your wallet. You will add a variety of coins(coin class) and bills (ones, fives, tens *also using the coin class) to your wallet arraylist. Program must include a loop that asks you to purchase items using the coins in your wallet. When purchasing items, the program will remove coins from the arraylist...
Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define...
I need some help doing this Java project. 1.Implement the MySort class with the following operations. a.bubbleSort(MyArrayList arraylist) – conduct bubble sort on the elements contained in a MyArrayList object. b.selectionSort(MyArrayList arraylist) – conduct selection sort on the elements contained in a MyArrayList object. 2.Implement the MySearch class with the following operations. a.binarySearch(MyArrayList arraylist, Comparable target) – conduct binary search on sorted elements contained in a MyArrayList object. b.linearSearchSorted (MyArrayList arraylist, Comparable target) – conduct linear search on sorted elements...