+1 (315) 557-6473 

Sort an array list of integer using Java Homework solution


Array list Application Assignment


Assignment 5

Create a simple Java application in the IDE of your choice (e.g., IntelliJ) that follows the specifications in this document.

Add the following components to your application. Look to aggressively sub-divide your application into methods (and very soon, classes.)

    a. Declare and instantiate (create) an ArrayList of Integers using generic syntax.

    b. Create a loop that repeatedly inputs strings until an empty string is an input.

    c. Each string that is input should have any integers extracted via whitespace delimiting and saved into the ArrayList. Any non-integer input is disregarded.

    • For example, if “12 4f5 3.14 x 7 y6” was a line of input, you should save (only) 12 and 7 to the ArrayList.

    d. The strings should be processed in the most efficient way possible and without the need for exception handling. There is a code example, CIS231Ch11Examples.java, available on Canvas that will help you with some of your code.

    e. Once input is complete display (and unambiguously label) the following information with generous whitespace for max readability. Use whitespace to separate sections – don’t just cram all of the output together.

  1. Your name (first and last) followed by “CIS 231 - Assignment 1”
  2. The number of integers that were input
  3. All of the values input, in ascending order, 10 on a line (the severe penalty for using bubble sort, Bogo sort, or the like) with the same numeric formatting as used in Lab 4. Use a sort (insertion or selection) we’ve covered in class.
  4. The lowest and highest values input
  5. The average of all values input (as a double to two decimal places)
  6. The mode of the data set and its frequency (generated without a nested loop or external data structure.)

You can utilize any classes from the Java API as you see fit with the exception of the sort code, which you must write java assignment yourself.

Solution:

import java.util.ArrayList; import java.util.Random; import java.util.Scanner; import static java.lang.Integer.parseInt; public class A5231flast { // FIXME - Please enter your first name and last name static final String FIRST_NAME = "FIRST"; static final String LAST_NAME = "LAST"; /** * * @return */ private static ArrayList getInput() { // Declare and instantiate (create) an ArrayList of Integers using generic syntax. ArrayList list = new ArrayList<>(); // Create a loop that repeatedly inputs strings until an empty string is an input. // Mockup for the get input process by using Random Number Generator System.out.println("Please enter your input data."); System.out.println("Enter a line with empty string will terminate the input"); Scanner scanner = new Scanner(System.in); String line; while (true) { line = scanner.nextLine(); if (line.trim().isEmpty()) { break; } else { for (String str : line.split(" ")) { try { list.add(parseInt(str)); } catch (NumberFormatException exception) { continue; } } } } // Each string that is input should have any integers extracted via whitespace // delimiting and saved into the ArrayList. Any non-integer input is disregarded. // - For example, if “12 4f5 3.14 x 7 y6” was a line of input, you should save (only) // 12 and 7 to the ArrayList. // Return the data list return list; } /** * Get the minimum value index from the given list * @param list - An ArrayList list object which stores the integer data * @param i - The starting index of the given sub-list for searching * @param j - The ending index of the given sub-list for searching * @return */ private static int minIndex(ArrayList list, int i, int j) { // If the starting index and the ending index is the same, // directly output the given index value if ( i == j ) { return i; } // Find the minimum value from the remaining elements int k = minIndex(list, i + 1, j); // Return the minimum of current and remaining. return list.get(i) < list.get(k) ? i : k; } /** * * @param list - An ArrayList list object which stores the integer data * @param n - The number of elements in the target subset * @param index - The starting index of the list for sorting */ private static void selectionSort(ArrayList list, int n, int index) { // Return when starting and size are same if (index == n) return; // calling minimum index function for minimum index int k = minIndex(list, index, n-1); // Swapping when index nd minimum index are not same if (k != index){ // Swap the data int temp = list.get(k); list.set(k, list.get(index)); list.set(index, temp); } // Recursively calling selection sort function selectionSort(list, n, index + 1); } /** * Calculate the average value of the given data list * @param list - An ArrayList list object which stores the integer data * @return The average value of the given data list */ private static double average(ArrayList list) { // Initialize the sum to be zero for later accumulation int sum = 0; // Using the for-loop to traverse the data for calculating the average value for (int n : list) { sum += n; } return (double)sum / list.size(); } /** * Print the program title */ private static void printTitle() { // Print out the project title System.out.printf(" CIS 231 - Assignment 5 - %s %s\n", FIRST_NAME, LAST_NAME); System.out.println(""); } /** * Print the size of the given ArrayList object * @param list - An ArrayList list object which stores the integer data */ private static void printListSize(ArrayList list) { // Print out the data list System.out.printf("Number: %20d\n", list.size()); System.out.println(""); } /** * Print the Data List * @param list - An ArrayList list object which stores the integer data */ private static void printList(ArrayList list) { int i = 0; System.out.println("Data List:"); // Using the for-loop to traverse the data list for (int n : list) { System.out.printf("%4d ", n); // If the modulus number is 9, which means the printed // line has 10 data already before the counter increment if (i % 10 == 9) { System.out.println(""); } // Increment the number of data count i++; } System.out.println(""); } /** * Print the lowest and the highest element in the given data list * @param list - An ArrayList list object which stores the integer data */ private static void printExtreme(ArrayList list) { // Since the provided data list has been sorted, therefore // the lowest and the highest value should the first one and the list one // from the provided data list System.out.printf("Lowest: %20d\n", list.get(0)); System.out.printf("Highest: %20d\n", list.get(list.size() - 1)); System.out.println(""); } /** * Print the average value from all the elements stored in the data list * @param list - An ArrayList list object which stores the integer data */ private static void printAverage(ArrayList list) { // Calling the function average to compute the average value of the given list // And then, Print out the report System.out.printf("Average: %20.2f\n", average(list)); System.out.println(""); } /** * Print the model of the data set and its frequency * @param list - An ArrayList list object which stores the integer data */ private static void printMode(ArrayList list) { // We assume that the given list has been sorted, // therefore we can directly count which distinct // value occurs the highest frequency to report as mode // We initialize the searching job with the first index element int mode = list.get(0); int value = list.get(0); int maxFreq = 0; int count = 0; // Using the for-loop to traverse all the element inside the data list for (int n : list) { // When the element is the same as the previous one, // increment the counter if (value == n) { count++; } else { value = n; count = 1; } // When the counter is the larger than the previous counted element, // Update the mode value found and its corresponding frequency count if (count > maxFreq) { mode = value; maxFreq = count; } } // Print out the report System.out.printf("Mode: %20d\n", mode ); System.out.printf("Frequency: %20d\n", maxFreq ); System.out.println(""); } /** * Main Driver Function * @param args - The console program input argument list */ public static void main(String[] args) { // Declare and instantiate (create) an ArrayList of Integers using generic syntax. ArrayList list = getInput(); // Once input is complete displayed (and unambiguously labeled) the following // information with generous whitespace for max readability. Use whitespace to // separate sections – don’t just cram all of the output together. // 1. Your name (first and last) followed by “CIS 231 - Assignment 1” printTitle(); // 2. The number of integers that were input printListSize(list); // 3. All of the values input, in ascending order, 10 on a line (the severe penalty for // using bubble sort, Bogo sort, or the like) with the same numeric formatting // as used in Lab 4. Use a sort (insertion or selection) we’ve covered in // class. // Run the selection sort to sort the data list in ascending order selectionSort(list, list.size(), 0); // Print the list as requested printList(list); // 4. The lowest and highest values input printExtreme(list); // 5. The average of all values input (as a double to two decimal places) printAverage(list); // 6. The mode of the data set and its frequency (generated without a nested // loop or external data structure.) printMode(list); } }