Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a program that does the following: creates an ArrayList of integers promp

ID: 3882236 • Letter: C

Question

Create a program that does the following:

creates an ArrayList of integers

prompts the user to enter integers and adds them to the ArrayList

calls the generic method max to print out the largest element in the ArrayList.

creates an ArrayList of Strings

prompts the user to enter Strings and adds them to the ArrayList

calls the generic method max to print out the largest element in the ArrayList.

Implements the following generic method that returns the largest element in an ArrayList:

public static <E extends Comparable<E>> E max(ArrayList<E> list)

Have the program display the output to the console. Your output should be as shown in the below example:

Enter an integer to add to a list of integers, 0 to stop:

46

Enter an integer to add to a list of integers, 0 to stop:

3

Enter an integer to add to a list of integers, 0 to stop:

14

Enter an integer to add to a list of integers, 0 to stop:

0

The largest integer in the array is 46.

Enter a string to add to a list of strings, "done" to stop:

computer

Enter a string to add to a list of strings, "done" to stop:

science

Enter a string to add to a list of strings, "done" to stop:

rocks

Enter a string to add to a list of strings, "done" to stop:

done

The largest string in the array is science.

Explanation / Answer

Please find my implementation.

import java.util.ArrayList;

import java.util.Scanner;

public class ArrayListDemo {

  

   public static <E extends Comparable<E>> E max(ArrayList<E> list) {

      

       E max = list.get(0);

      

       for(int i=1; i<list.size(); i++) {

          

           if(max.compareTo(list.get(i)) < 0)

               max = list.get(i);

       }

      

       return max;

   }

  

   public static void main(String[] args) {

      

       Scanner sc = new Scanner(System.in);

      

       ArrayList<Integer> intList = new ArrayList<>();

       ArrayList<String> strList = new ArrayList<>();

      

       int num = 1;

       System.out.println("Enter an integer to add to a list of integers, 0 to stop:");

       num = sc.nextInt();

      

       while(num != 0) {

           intList.add(num);

           num = sc.nextInt();

       }

      

       System.out.println("The largest integer in the array is "+max(intList)+".");

      

       String str;

       System.out.println("Enter a string to add to a list of strings, "done" to stop:");

       str = sc.next();

      

       while(!str.equalsIgnoreCase("done")) {

           strList.add(str);

           str = sc.next();

       }

      

       System.out.println("The largest string in the array is "+max(strList)+".");

      

       sc.close();

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote