Question

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” and “two”.

List<String> strings = newArrayList<>();

strings.add("one");

strings.add("one");

strings.add("one");

Utilities utilities = new Utilities();

utilities.removeDuplicates(strings);

The result is the list of strings contains one record; “one”.

LinearSearch Implement a generic method to do a linear search. Your linear search method should accept a list containing a generic type, and a key record to search for in the generic list. Your search should return the record associated with the supplied key or null if the key does not exist in the supplied list.

public <E> E linearSearch(List<E> list, E key)

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

// Utilities.java

import java.util.ArrayList;

import java.util.List;

public class Utilities {

       // generic method to remove duplicates from a list

       public<T> void removeDuplicates(List<T> items)

       {

             for(int i=0;i<items.size();i++)

             {

                    for(int j=i+1;j<items.size();j++)

                           if(items.get(i).equals(items.get(j)))

                           {

                                 items.remove(j);

                                 j--;

                           }

             }

       }

      

       // generic method to find and return the record associated with the key or null, if record not present

       public <E> E linearSearch(List<E> list, E key)

       {

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

                    if(list.get(i).equals(key))

                           return list.get(i);

             return null;

       }

      

       public static void main(String[] args) {

      

             List<String> strings=new ArrayList<>();

             strings.add("one");

             strings.add("two");

             strings.add("one");

            

             Utilities utilities =new Utilities();

             utilities.removeDuplicates(strings); //test the removeDuplicates

             System.out.println(" Strings list after removing duplicates : ");

             for(int i=0;i<strings.size();i++)

                    System.out.println(strings.get(i));

             System.out.print("\n Find \"one\" in Strings list : ");

             System.out.println(utilities.linearSearch(strings, "one"));

             System.out.print("\n Find \"three\" in Strings list : ");

             System.out.println(utilities.linearSearch(strings, "three"));

       }

}

//end of Utilities.java

Output:

Strings list after removing duplicates: one two Find one in Strings list: one Find three in Strings list null

Add a comment
Know the answer?
Add Answer to:
Hi - So again I'm having some trouble getting started. Here is my assignment: 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
  • *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 =...

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

  • Java - Car Dealership Hey, so i am having a little trouble with my code, so...

    Java - Car Dealership Hey, so i am having a little trouble with my code, so in my dealer class each of the setName for the salesman have errors and im not sure why. Any and all help is greatly appreciated. package app5; import java.util.Scanner; class Salesman { private int ID; private String name; private double commRate; private double totalComm; private int numberOfSales; } class Car { static int count = 0; public String year; public String model; public String...

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

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • Hey guys I am having trouble getting my table to work with a java GUI if...

    Hey guys I am having trouble getting my table to work with a java GUI if anything can take a look at my code and see where I am going wrong with lines 38-49 it would be greatly appreciated! Under the JTableSortingDemo class is where I am having erorrs in my code! Thanks! :) import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import javax.swing.*; public class JTableSortingDemo extends JFrame{ private JTable table; public JTableSortingDemo(){ super("This is basic sample for your...

  • Assignment: A set is a collection of items without repetitions. The goal of this assignment is...

    Assignment: A set is a collection of items without repetitions. The goal of this assignment is to implement a set as a data structure. Do the following. 1. (30 points) Starting with the template attached, implement a generic class Set(T) that maintains a set of items of generic type T using the class ArrayList(T) in the Java API. Your Set class must provide the following instance methods: add: that adds a new item. (Ignore if already in the set.) remove:...

  • A java program for this question please! Recursion: A word is considered elfish if it contains...

    A java program for this question please! Recursion: A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say that the following words are elfish: whiteleaf, tasteful, unfriendly, and waffles, because they each contain those letters. Write a recursive method called elfish(), that, given a word, tells us whether or not that word is elfish. The signature of the method should be: public static boolean elfish(String word)...

  • Hi im getting a Exception in thread "main" java.lang.NullPointerException. What is wrong? Here is my code....

    Hi im getting a Exception in thread "main" java.lang.NullPointerException. What is wrong? Here is my code. class MicrosoftMonarchs { //declaring all the arrays public String array[]; public double close[]; public int volume[]; public double open[]; public double high[]; public double low[]; //declaring both size and totalFileData public long totalFileData=0; public int size=5; //main file public static void main(String[] args) { MicrosoftMonarchs data = new MicrosoftMonarchs(); //setting all the arrays equal to the size of the given text data.array= new String[data.size];...

  • Hi, So I have a finished class for the most part aside of the toFile method...

    Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...

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