Question: Min, max, median (generic methods): JAVA Given main(), complete LabProgram.java by implementing the following methods: inputIntegers() Take a scanner as a parameter Read 5 integers

Min, max, median (generic methods): JAVA

Given main(), complete LabProgram.java by implementing the following methods:

inputIntegers()

Take a scanner as a parameter

Read 5 integers from a user

Store the integers in an ArrayList of type Integer

Return the ArrayList

inputDoubles()

Take a scanner as a parameter

Read 5 doubles from a user

Store the doubles in an ArrayList of type Double

Return the ArrayList

inputWords()

Take a scanner as a parameter

Read 5 one-word strings from a user

Store the strings in an ArrayList of type String

Return the ArrayList

print()

Take an ArrayList as a parameter

Output the elements of the ArrayList parameter

For coding simplicity, follow each output element by a space, including the last one

Output a newline after the last element

getStatistics()

Take an ArrayList as a parameter

Store the minimum, median, and maximum values of the ArrayList parameter in a new ArrayList

Return the new ArrayList containing the minimum, median, and maximum values

Hint: Sort the ArrayList by using Collections.sort() to find the minimum, median, and maximum values

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

public class LabProgram {

// Input 5 Integers and return the Integers in an ArrayList public static ArrayList inputIntegers(Scanner scnr) { /* Type your code here. */ ArrayList integers = new ArrayList<>();

// loop through for the 5 integers for(int i = 0; i < 5; i++) integers.add(scnr.nextInt());

// return the computed list of integers return integers; }

// Input 5 Doubles and return the Doubles in an ArrayList public static ArrayList inputDoubles(Scanner scnr) { /* Type your code here. */ ArrayList doubles = new ArrayList<>();

// loop through for the 5 doubles for(int i = 0; i < 5; i++) doubles.add(scnr.nextDouble());

// return the computed list of doubles return doubles; }

// Input 5 Strings, and return the Strings in an ArrayList public static ArrayList inputWords(Scanner scnr) { /* Type your code here. */ ArrayList words = new ArrayList<>();

// loop through for the 5 words for(int i = 0; i < 5; i++) words.add(scnr.next());

// return the computed list of words return words; }

// Output the elements of the ArrayList parameter public static > void print(ArrayList list) { /* Type your code here. */ for(int i = 0; i < lst.size(); i++) // print each element of the list followed by a single space System.out.print(lst.get(i) + " "); System.out.println(); }

// Return the min, median, and max of the ArrayList parameter in a new ArrayList public static > ArrayList getStatistics(ArrayList list) { /* Type your code here. */ Collections.sort(lst);

// store the first element of the sorted list as the minimum value stats.add(lst.get(0)); // store the mid element of the sorted list as the median value stats.add(lst.get(lst.size() / 2)); // store the last element of the sorted list as the maximum value stats.add(lst.get(lst.size() - 1));

// return the statistics return stats; }

public static void main(String[] args) { Scanner scnr = new Scanner(System.in);

// Input a list of 5 Integers and print the ArrayList's statistics ArrayList integers = inputIntegers(scnr); print(integers); ArrayList integerStatistics = getStatistics(integers); print(integerStatistics); System.out.println();

// Input a list of 5 Doubles and print the ArrayList's statistics ArrayList doubles = inputDoubles(scnr); print(doubles); ArrayList doubleStatistics = getStatistics(doubles); print(doubleStatistics); System.out.println();

// Input a list of 5 words (Strings) and print the ArrayList's statistics ArrayList words = inputWords(scnr); print(words); ArrayList wordStatistics = getStatistics(words); print(wordStatistics); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!