Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE, I NEED HELP ON THIS ASSIGNMENT. 05. Fifth Assignment – Utility Methods F

ID: 3870312 • Letter: P

Question

PLEASE, I NEED HELP ON THIS ASSIGNMENT.

05. Fifth Assignment – Utility Methods
For this assignment, we’ll be doing problems 19.3 and 19.4 from your textbook. Problem
19.3 asks you to write a generic method that removes duplicate items from an ArrayList.
This should be fairly straightforward. Problem 19.4 asks you to implement a generic
method for linear search.
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 Integers, 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? And so on.
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 linearSearch method works on at least some input
The linearSearch 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
linearSearch methods
The main method shows the operation of the removeDuplicates and linearSearch
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
Note: If your program does not compile, you will receive a score of 0 on the
entire assignment
Note: If you program compiles but does not run, you will receive a score of 0 on
the entire assignment
Note: I your Eclipse project is not exported and uploaded to the eLearn drop box
correctly, you will receive a score of 0 on the entire assignment

Explanation / Answer

Given below is the generic methods for removeDuplicates and linearSearch(). The question does not specify more details on the methods. The methods are implemented as follows=

1. removeDuplicates() : retains only 1 copy of every item and the order of 1st occurence is maintained. Only the duplicate copies of an item are removed. For example if the number 2 occurs 3 times, only the first occcurence of 2 is retained and the ther occureces are removed. Also the removeDuplicates makes use of the linear search implmented.

2. linearSearch(): searches for speicifed item from the beginnnign and returns index of first occurence. returns -1 if not found.

If the removeDuplicates() should remove all occurences of a duplicated item, please post a comment so that I can modifiy the code. Let me know what is the desired behaviour.

If the answer helped, please do rate it . Thank you.

import java.util.ArrayList;

public class Utilities {

  

   //removes duplicate occurences of item and retains only 1 copy of every item

   public static <T extends Comparable<T>> void removeDuplicates(ArrayList<T> list)

   {

       //traverse from back end of the list, fetch the current object,

       //use indexOf(current object) to get the index of first occurence of the object

       //if the current index and value returned by indexOf() are same, then its not a duplicate

       //otherwise it is a duplicate and remove the value at index

       for(int index = list.size()- 1; index > 0; index--)

       {

           T obj = list.get(index);

           int first = linearSearch(list, obj);

           if(first != index) // duplicate

               list.remove(index);

       }

   }

  

  

   //returns the index of first occurence of the searchObject, -1 if not found

   public static <T extends Comparable<T>> int linearSearch(ArrayList<T> list, T searchObj)

   {

       //traverse from beginning and compare each object to searchObj

       //return index if equal.

       //if not found, return -1

       for(int index = 0; index < list.size(); index++)

       {

           T obj = list.get(index);

           if(obj.equals(searchObj))

               return index;

       }

      

       return -1; //when not found

   }

  

  

   public static void main(String[] args) {

       ArrayList<Integer> numbers = new ArrayList<Integer>();

  

       numbers.add(2);

       numbers.add(3);

       numbers.add(4);

       numbers.add(2);

       numbers.add(3);

       numbers.add(5);

       System.out.println("numbers: " + numbers);

       System.out.println("linearSearch(numbers, 3) = " + linearSearch(numbers, 3));

       removeDuplicates(numbers);

       System.out.println("After removing duplicates, numbers = " + numbers);

      

      

       ArrayList<String> fruits = new ArrayList<String>();

       fruits.add("banana");

       fruits.add("apple");

       fruits.add("orange");

       fruits.add("apple");

       fruits.add("grapes");

       fruits.add("apple");

       System.out.println("fruits: " + numbers);

       System.out.println("linearSearch(fruits, "grapes") = " + linearSearch(fruits, "grapes"));

       removeDuplicates(fruits);

       System.out.println("After removing duplicates, fruits = " + fruits);

      

      

   }

}

OUTPUT

numbers: [2, 3, 4, 2, 3, 5]

linearSearch(numbers, 3) = 1

After removing duplicates, numbers = [2, 3, 4, 5]

fruits: [2, 3, 4, 5]

linearSearch(fruits, "grapes") = 4

After removing duplicates, fruits = [banana, apple, orange, grapes]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote