Question

In: Computer Science

Create a class named RemoveDuplicates and write code: a. for a method that returns a new...

Create a class named RemoveDuplicates and write code:

a. for a method that returns a new ArrayList, which contains the nonduplicate elements from the original list public static ArrayList removeDuplicates(ArrayList list)

b. for a sentinel-controlled loop to input a varying amount of integers into the original array (input ends when user enters 0)

c. to output the original array that displays all integers entered

d. to output the new array that displays the list with duplicates removed

Use this TEST INPUT: 3 6 1 1 1 8 4 4 0

Solutions

Expert Solution

/* Here is your code,to run on the website whose screenshot is provided you have to rename class to Main*/

import java.util.*;

public class RemoveDuplicates {

   public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)
   {

       ArrayList<T> newList = new ArrayList<T>();

       for (T element : list) {

           if (!newList.contains(element)) {

               newList.add(element);
           }
       }

       return newList;
   }
   public static void display(ArrayList list){
   System.out.println(list);
   }

   public static void main(String args[])
   { Scanner sc=new Scanner(System.in);
       ArrayList<Integer>
           list = new ArrayList<>();
           System.out.print("Enter list: ");
while(true){
int temp=sc.nextInt();
if(temp!=0)
list.add(temp);
else
break;
}System.out.println("original list");
   display(list);

       ArrayList<Integer>
           newList = removeDuplicates(list);
System.out.println("new list");
       display(newList);
   }
}


Related Solutions

Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to...
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to read integers from the keyboard with the method nextInt. Use a try catch block to capture non-integer input, and tell the user, "This is not an Integer". The program loop should end when the user enters the string "quit". (Hint: "quit" is clearly not a number and will be captured in the try / catch block.) A typical Output Dialog is shown below: Enter...
The following code is for a class named Box. The class Box includes a constructor method...
The following code is for a class named Box. The class Box includes a constructor method Box, and a method getVolume(). For your assignment you are to develop a java class named MatchBox. Your class MatchBox must extend the class Box and in addition to the attributes width, height, and depth that are defined in the class Box, MatchBox must add a new attribute weight. The getVolume method must both report the values of width, height, depth, and weight, but...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a...
Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call: isPalindrome(q) should...
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT