Question

In: Computer Science

In your latest home assignment, you were asked to write a generic method that returns an...

In your latest home assignment, you were asked to write a generic method that returns an ArrayList that contains the non-duplicate elements from the original list. For this question, write a generic method that returns an ArrayList that contains the non-duplicate elements or the duplicate elements depending on the boolean argument (dup) setting.

               If dup = false, return the list with the non-duplicates,

                 else return the list with the duplicated elements in the original list.

Please use the following two arrays for this question.

Integer[] nlist = { 0, 24, 25, 14, 25, 24, 42, 45, 42, 25};

String[] srt = {"Sam", "Bob", "Sue","Bob","Matt","Nick","Sue","Bob"};

Use the following static method (Array2Collection(….) to convert them to two corresponding ArrayLists ( 4 points)

public static <T> void Array2Collection(T a[], Collection<T> c) {

      for (T x: a)

            c.add(x);

}

Write codes to return an ArrayList <E>: if dup = false, return the list with the non-duplicates, else return with the duplicated element in the original list.

// 24 points­ for codes inside this method.

public static <E> ArrayList<E> Duplicates(ArrayList<E> list, boolean dup) {

// add codes here….

// 4 points for preparing the arrayList(s) from the two given arrays.

// 15 points for building the two arrayList of elements from the original list

// 5 points for returning the appropriate list based on the dup switch

}

Your client prgram should display this:

original list[0, 24, 25, 14, 25, 24, 14, 42, 45, 42, 25]

nodup list [0, 24, 25, 14, 42, 45]

dup list [25, 24, 14, 42, 25]

-------------

original list[Sam, Bob, Sue, Bob, Matt, Nick, Sue, Bob]

nodup list[Sam, Bob, Sue, Matt, Nick]

dup list[Bob, Sue, Bob]

Solutions

Expert Solution

Java Program:

import java.util.*;
class Main {
  public static <T> void Array2Collection(T a[], Collection<T> c)
  {
    for (T x: a)
      c.add(x);
  }

  public static <E> ArrayList<E> Duplicates(ArrayList<E> list, boolean dup)
  {
    ArrayList<E> d=new ArrayList<E>();
    if(dup)
    {
      HashMap<E,Integer> m=new HashMap<E,Integer>();
      for(E i:list)
      {
        m.put(i,m.getOrDefault(i,0)+1);
      }
      for(E i:list)
      {
        if(m.getOrDefault(i,0)>1)
        {
          if(m.getOrDefault(i,0)%2!=0)
          {
            d.add(i);
            m.put(i,m.getOrDefault(i,0)-1);
          }
          else
          {
            m.put(i,m.getOrDefault(i,0)+1);
          }
        }
      }
    }
    else
    {
      Set<E> s = new LinkedHashSet<E>(list);
      d=new ArrayList<E>(s);
    }
    return d;
  }
  public static void main(String[] args) {
    Integer[] nlist = { 0, 24, 25, 14, 25, 24,14, 42, 45, 42, 25};
    String[] srt = {"Sam", "Bob", "Sue","Bob","Matt","Nick","Sue","Bob"};
    ArrayList<Integer> li=new ArrayList<Integer>();
    Array2Collection(nlist,li);
    System.out.println("Original List "+li);
    System.out.println("Nodup List "+Duplicates(li,false));
    System.out.println("Dup List "+Duplicates(li,true));
    System.out.println("------------------------");
    ArrayList<String> ls=new ArrayList<String>();
    Array2Collection(srt,ls);
    System.out.println("Original List "+ls);
    System.out.println("Nodup List "+Duplicates(ls,false));
    System.out.println("Dup List "+Duplicates(ls,true));
  }
}

Output:


Related Solutions

We were asked this: Write a function that takes a list, and returns a list representing...
We were asked this: Write a function that takes a list, and returns a list representing each word whose reverse is also in the list. def find_reversals(lst: List[str]) -> List[str]: Each pair, such as 'abut', 'tuba', should be represented by the first element encountered. Don't report the same pairs twice. Don't list palindromes. I wrote this, which might not be optimal but passes the unit test! def find_reversals(lst): #Place to save to found_reversals=[]    #Make each string lowercase for i...
Assignment Content Imagine you have been asked by your local paper to write an article on...
Assignment Content Imagine you have been asked by your local paper to write an article on the use of Electronic Health Records (EHRs) in health care. Write a 350- to 700-word article for the paper in which you: Explain the functions and advantages of using EHRs. Explain the relationship between paper forms and EHRs. Describe the basic flow of information within a health care organization using EHRs. Cite any outside sources to support your assignment.
For Python: In this assignment you are asked to write a Python program to determine the...
For Python: In this assignment you are asked to write a Python program to determine the Academic Standing of a studentbased on their CGPA. The program should do the following: Prompt the user to enter his name. Prompt the user to enter his major. Prompt the user to enter grades for 3 subjects (A, B, C, D, F). Calculate the CGPA of the student. To calculate CGPA use the formula: CGPA = (quality points * credit hours) / credit hours...
​Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.
Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.(JAVA Code)
In Assignment #4, you are asked to determine the following: What type of survey method is...
In Assignment #4, you are asked to determine the following: What type of survey method is most appropriate for each of the following scenarios? Give your justification. c. To determine the satisfaction of an ice rink
Please write a java code. Write a generic program for New Home Construction Pricing with the...
Please write a java code. Write a generic program for New Home Construction Pricing with the following specifications. Note, each one of the 2, 3 or 4 bedroom homes are priced with standard type bathrooms. Update to deluxe or premium choice of bathrooms can be ordered by paying the difference in prices.    Types of homes Price 2 bedroom, 2 bathroom (standard type) and 1 car garage home = $350,000 3 bedroom, 2 bathroom (standard type) and 2 car garage...
In your first assignment on the job, your supervisor has asked you to report on the...
In your first assignment on the job, your supervisor has asked you to report on the company’s competitors future products. You really want to succeed. What do you do? A. You could call the competitors pretending you are a college student working on project needing information B. You could ask a college friend to call the competitors and ask for the information C. You could call the competitors, but would not tell them who you really are unless if asked...
This assignment will ask you to look at your own finances. For this assignment, write a...
This assignment will ask you to look at your own finances. For this assignment, write a paragraph showing your percentages. I DO NOT want to know how much money you spend or have as income, I am only looking at the percentages. Describe for me what the effect of inflation is on you personally given what you found out. Look at a typical month of your expenditures and income. Create a list of your expenses by category (see below). Create...
JAVA Generic versions of allOf() and anyOf() In this lab, you will write generic versions of...
JAVA Generic versions of allOf() and anyOf() In this lab, you will write generic versions of the allOf() and anyOf() methods you wrote in an earlier lab. Then you will take those generic versions and create versions that work with a predicate rather than direct comparison of values. Part 1 - Create generic versions of allOf() and anyOf() Recall that the integer-only versions of allOf() and anyOf() looked like this: // Returns true if any element of 'a' is equal...
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT