Question

Questions Write a method that returns the union of two array lists of integers using the...

Questions Write a method that returns the union of two array lists of integers using the following header: public static ArrayList<Integer> union(ArrayList<Integer> list1, ArrayList<Integer> list2)

Write a test program that: 1. prompts the user to enter two lists, each with five integers, 2. displays their union numbers separated by exactly one space., and 3. finds the maximum number and the minimum number in the combined list. Here is a sample run of the program.

Enter five integers for list1: 5 3 22 32 6 Enter five integers for list2: 9 7 33 4 17 The Combined list is 5 3 22 32 6 9 7 33 4 17 The Maximum is 33 The Minimum is 3

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

package com.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class ArrayListUnion {

   public static ArrayList<Integer> union(ArrayList<Integer> list1, ArrayList<Integer> list2) {
       ArrayList<Integer> unionList = new ArrayList<Integer>();
       //set to hold unique values
       Set<Integer> output = new HashSet<Integer>();

       for (int i = 0; i < list1.size(); i++) {
           output.add(list1.get(i));

           for (int j = 0; j < list2.size(); j++) {
               output.add(list2.get(j));
           }
       }
       //Add to output list
       for(int i : output) {
           unionList.add(i);
       }
       //return
       return unionList;
   }

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       //Create two list
       ArrayList<Integer> list1 = new ArrayList<Integer>();
       ArrayList<Integer> list2 = new ArrayList<Integer>();
       //Add int
       list1.add(5);
       list1.add(3);
       list1.add(22);
       list1.add(32);
       list1.add(6);

       list2.add(9);
       list2.add(7);
       list2.add(33);
       list2.add(4);
       list2.add(17);
       //Call union
       ArrayList<Integer> unionList = union(list1, list2);
       //Display
       System.out.println("List 1: " + list1);
       System.out.println("List 2" + list2);
       System.out.println("The Combined list is: " + unionList);
       //Sort list to find minimum and maximum
       Collections.sort(unionList);
       //Display minimum and maximum
       System.out.println("Minimum: " + unionList.get(0));
       System.out.println("Maximum: " + unionList.get(unionList.size() - 1));
   }

}

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

List 1: [5, 3, 22, 33, 6]
List 2[9, 7, 33, 4, 17]
The Combined list is: [33, 17, 3, 4, 5, 22, 6, 7, 9]
Minimum: 3
Maximum: 33

Add a comment
Know the answer?
Add Answer to:
Questions Write a method that returns the union of two array lists of integers using the...
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
  • Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for...

    Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for ArrayList: first method finds the maximum element in an array list; the second method computes the sum of all elements in an array list; and the third method combines (union) two lists by adding the second list to the first list). Use Integer type for all methods. See problem statements for methods signatures and sample test data. Write one separate test program to test...

  • Write a program that will store 7 integers (entered by the user) into an array called...

    Write a program that will store 7 integers (entered by the user) into an array called list1. Next, prompt the user for 5 integers and put them into an array called list2. Now, list all of the numbers that are in BOTH lists by calling a function called inBoth and passing list1 and list2 to that function. List 1 ------ Enter an integer: 4 Enter an integer: 2 Enter an integer: 7 Enter an integer: 1 Enter an integer: 5...

  • (Remove duplicates) Write a method that removes the duplicate elements from an array list of integers...

    (Remove duplicates) Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList < Integer > list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers in their input order separated by exactly one space. Sample Run 1 Enter ten integers: 34 5 3 5 6 4 33 2 2 4 The distinct integers are 34 5 3...

  • (Maximum element in ArrayList) Write the following method that returns the maximum value in an ArrayList...

    (Maximum element in ArrayList) Write the following method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0. public static Integer max(ArrayList<Integer> list) Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach.

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.          public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

  • Python. Write a function swap_lists that takes in two lists and swap their elements in place....

    Python. Write a function swap_lists that takes in two lists and swap their elements in place. You can not use another list. You can not return lists. Input lists have the same size. def swap_lists(alist1, alist2): """Swaps content of two lists. Does not return anything. >>> list1 = [1, 2] >>> list2 = [3, 4] >>> swap_lists(list1, list2) >>> print(list1) [3, 4] >>> print(list2) [1, 2] >>> list1 = [4, 2, 6, 8, 90, 45] >>> list2 = [30, 41,...

  • Using PYTHON: (Find the index of the smallest element) Write a function that returns the index...

    Using PYTHON: (Find the index of the smallest element) Write a function that returns the index of the smallest element in a list of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: def indexOfSmallestElement(lst): Write a test program that prompts the user to enter a list of numbers, invokes this function to return the index of the smallest element, and displays the index. PLEASE USE LISTS!

  • Write a method called alternate that accepts two Lists as its parameters and returns a new...

    Write a method called alternate that accepts two Lists as its parameters and returns a new List containing alternating elements from the two lists, in the following order: • First element from first list • First element from second list • Second element from first list • Second element from second list • Third element from first list • Third element from second list If the lists do not contain the same number of elements, the remaining elements from the...

  • In JAVA: Write a method that reverses the array passed the argument and returns this array....

    In JAVA: Write a method that reverses the array passed the argument and returns this array. Write a test program that prompts the user to enter ten numbers, invokes the method to reverse the numbers and display the numbers. Write a method that returns a new array by eliminating the duplicate values in the array using following method header: a. Public static int[] eliminateDuplicates(int list) b. Write a test program that reads in ten integers and invoke the method, the...

  • Write a test program that prompt the user to enter seven numbers, stores them in an...

    Write a test program that prompt the user to enter seven numbers, stores them in an array list and do the following: 1. Displays its elements (numbers) in increasing order by invoking a method with the following header that sorts the array: public static void sort(ArrayList<Integer>list) 2. Write a method that returns and displays the sum of all numbers (elements) of the ArrayList. Using the following header: public static double sum(ArrayList<Integer>list) 3. Write a method that removes the duplicate numbers...

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