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)
// 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:

Hi - So again I'm having some trouble getting started. Here is my assignment: For this...
*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 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 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 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 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 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 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:...
Java you are required to add a public Object get( Object key) method into the Hashtable class. As defined in the Java documentation, the get() method returns the value with which the specified key is associated, or null if this hash table contains no record for the key. More formally, if this hash table contains a mapping from a key k to a value v such that (key.equals(k)), then this method returns v; otherwise it returns null. (There can be...
Hello I'm having a bit of trouble with this assignment. Any help would be appreciated! Overview You must implement a Java class which simulates a Student object. These Student objects will represent grade records for students. Variable Details name : private String : represents the Student’s name sid: private String : represents the Student’s ID number quizzes: private double[] : an array whose size is equal to NUM_QUIZZES. Each element will be used to store one of...
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];...