Question

Need assistance with this problem. This is for a java class and using eclipse. For this...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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!

Add a comment
Know the answer?
Add Answer to:
Need assistance with this problem. This is for a java class and using eclipse. For this...
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
  • In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

    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...

  • (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...

    (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...

  • Activity 1. Suppose you are implementing a utility class, ArrayListUtil, in which you provide some utility...

    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.

  • Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design...

    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...

    *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 =...

  • Money Lab using arraylists in Java Using the Coin class below create a program with the...

    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...

  • C++ Search & Sort

    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...

  • (This program is to be done on Java) 7.5 ArrayLists Part 1 A significant limitation of...

    (This program is to be done on Java) 7.5 ArrayLists Part 1 A significant limitation of the array is you cannot add or delete elements from the array. The size is fixed and you can only do workarounds. An example is the following: public class Ex_7_5_Prep { public static void main(String[] args) { int[] intArray = { 5, 10, 15, 20 }; printArray(intArray); intArray = addNewElement(intArray, 25); printArray(intArray); } public static int[] addNewElement(int[] originalArray, int newInt) { // Create new...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • You are given a specification for some Java classes as follows.   A building has a number...

    You are given a specification for some Java classes as follows.   A building has a number of floors, and a number of windows. A house is a building. A garage is a building. (This isn’t Florida-like … it’s a detached garage.) A room has a length, width, a floor covering, and a number of closets. You can never create an instance of a building, but every object that is a building must have a method that calculates the floor space,...

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