Question

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 all three methods. 11.4 (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 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. 11.12 (Sum ArrayList) Write the following method that returns the sum of all numbers in an ArrayList: public static double sum(ArrayList list) Write a test program that prompts the user to enter 5 numbers, stores them in an array list, and displays their sum. 11.14 (Combine two lists) Write a method that returns the union of two array lists of integers using the following header: public static ArrayList union( ArrayList list1, ArrayList list2) For example, the union of two array lists {2, 3, 1, 5} and {3, 4, 6} is {2, 3, 1, 5, 3, 4, 6}. Write a test program that prompts the user to enter two lists, each with five integers, and displays their union. The numbers are separated by exactly one space in the output. Here is a sample run: Enter five integers for list1: 3 5 45 4 3 Enter five integers for list2: 33 51 5 4 13 The combined list is 3 5 45 4 3 33 51 5 4 13

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListMax {

    public static Integer max(ArrayList<Integer> list) {
        int max = list.get(0);
        for(int i = 0; i < list.size(); ++i) {
            if(list.get(i) > max) {
                max = list.get(i);
            }
        }
        return max;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter integers(0 to stop): ");
        ArrayList<Integer> list = new ArrayList<Integer>();
        int num;
        while (true) {
            num = in.nextInt();
            if(num == 0) {
                break;
            }
            list.add(num);
        }
        System.out.println("Maximum number in " + list + " is " + max(list));
    }

}

Enter integers (0 to stop): 21 29 27 Maximum number in [23, 21, 29, 27] is 29 Process finished with e xit code 0

import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListSum {

    public static double sum(ArrayList<Integer> list) {
        int total = 0;
        for(int i = 0; i < list.size(); ++i) {
            total += list.get(i);
        }
        return total;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter 5 numbers: ");
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(int i = 0; i < 5; ++i) {
            list.add(in.nextInt());
        }
        System.out.println("sum of numbers in " + list + " is " + sum(list));
    }

}

Enter 5 numbers: sum of numbers in [1, 2, 3, 4, 5] is 15.0 Process finished with e xit code 0

import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListCombine {

    public static ArrayList<Integer> union(ArrayList<Integer> list1, ArrayList<Integer> list2) {
        ArrayList<Integer> ret = new ArrayList<Integer>();
        for(int i = 0; i < list2.size(); ++i) {
            ret.add(list1.get(i));
        }
        for(int i = 0; i < list2.size(); ++i) {
            ret.add(list2.get(i));
        }
        return ret;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter five integers for list1: ");
        ArrayList<Integer> list1 = new ArrayList<Integer>();
        ArrayList<Integer> list2 = new ArrayList<Integer>();
        for(int i = 0; i < 5; ++i) {
            list1.add(in.nextInt());
        }
        System.out.print("Enter five integers for list2: ");
        for(int i = 0; i < 5; ++i) {
            list2.add(in.nextInt());
        }
        ArrayList<Integer> combined = union(list1, list2);
        System.out.print("The combined list is ");
        for(int i = 0; i < combined.size(); ++i) {
            System.out.print(combined.get(i) + " ");
        }
        System.out.println();
    }

}

Enter five integers for listl: aris:r i::і:W: :utíxyrer2x İ.YKr :Lim::: 33 51 5 4 13 The combined list is 3 5 45 4 3 33 51 5

Add a comment
Know the answer?
Add Answer to:
Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for...
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
  • 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...

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

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

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

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

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

  • Code in java Using the template below: public class Lab03 { public static void main(String[] args)...

    Code in java Using the template below: public class Lab03 { public static void main(String[] args) { ArrayList<Integer> list1 = new ArrayList<Integer>(); Collections.addAll(list1, 1, 3, 5, 5 ); ArrayList<Integer> list2 = new ArrayList<Integer>(); Collections.addAll(list2, 3, 7, 3, 2, 4 ); ArrayList<Integer> result1= uniqueUnion(list1,list2); System.out.println(result1); Integer[] array = new Integer[]{ 29, 28, 27, 16, 15, -14, 3, -2, 2}; ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(array)); System.out.printf("The average is: %.2f%n", averagePositive(arrayList)); } // end main public static ArrayList<Integer> uniqueUnion(ArrayList<Integer> list1, ArrayList<Integer> list2) {...

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

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

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

  • I need to Write a test program that prompts the user to enter 10 double values...

    I need to Write a test program that prompts the user to enter 10 double values into an array, calls a method to calculate the average of the numbers, and displays the average. Next, write a method that returns the lowest value in the array and display the lowest number. The program should then prompt the user to enter 10 integer values into an array, calculates the average, and displays the average. The program should have two overloaded methods to...

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