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

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

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

  • Please help me with this question Write a recursive method that returns the largest integer in...

    Please help me with this question Write a recursive method that returns the largest integer in an array. Write a test program that prompts the user to enter a list of eight integers and displays the largest largest element. Thank you! and can you give details so I know what is going on?

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • EVERYTHING IN C# Write a program that includes a method that returns the sum of all...

    EVERYTHING IN C# Write a program that includes a method that returns the sum of all the elements of an ArrayList of Integer Objects. Allow the user to enter the integers to be added to your ArrayList from the console (max of 10 integers). And also write a program that includes a method that returns the sum of all the elements of a Linked List of Integers. Allow the user to enter the integers to be added to your Linked...

  • Write the following method to test whether the array has four consecutive numbers with the same...

    Write the following method to test whether the array has four consecutive numbers with the same value. public static boolean isConsecutiveFour(int[] values) Write a java test program that prompts the user to enter a series of integers and displays if the series contains four consecutive numbers with the same value. Your program should first prompt the user to enter the input size - i.e., the number of values in the series. Expected results: Enter the number of values: 7 Enter...

  • please explain this Java problem with source code if possible Project: Strictly identical arrays Problem Deseription:...

    please explain this Java problem with source code if possible Project: Strictly identical arrays Problem Deseription: Two arrays are strictly identical if their correspondimg elements are equal Write a program with class name StrictlyIdentical that prompts the user to enter two lists ed integers of size 5 and displays whether the two are strictly identical. Usc following method header to pass two arrays and retum a Boolcan public static boolean cualsint[] array 1, int[] array2) Method will return true if...

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