Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Copy the program Stats.java to your computer and implement the TBI (To Be Implemented) stats() and checkIfSorted() methods following the instructions given in their method
Copy the program Stats.java to your computer and implement the TBI (To Be Implemented) stats() and checkIfSorted() methods following the instructions given in their method comment blocks. Stats.java program: import java.util.*; /* * This Java application calculates some statistics * given an array of integers. */ interface StatsConstants { // used by print() to format output... String PRINT_BEGIN = "["; String PRINT_END = "]"; String PRINT_SEPARATOR = ","; // checkIfSorted() return values... int UNSORTED = 0; int ASCENDING = 1; int DESCENDING = 2; int SKIP = 3; } public class Stats { public static void main(String[] argv) { int[][] data = { null, { }, { 0 }, { 1, 2 }, { 1, 1 }, { 1, 3, 2 }, { 5, 5, 5, 5, 5 }, { 2, 0, 5, 4, 8, 5 }, { 1, 5, 6, 6, 6, 7, 9 }, { -7, 0, 0, 3, 3, 3, 4, 4 }, { -2, -2, 0, 1, 1, 2, 2, 2 }, { -4, -2, 42, 12, 12, 3, -2 }, }; for (int i = 0; i < data.length; i++) { switch (checkIfSorted(data[i])) { case StatsConstants.SKIP: if (data[i] == null) System.out.println("null element "); else if (data[i].length == 0) System.out.println("empty array "); else System.out.println("??? "); continue; case StatsConstants.UNSORTED: Arrays.sort(data[i]); break; case StatsConstants.DESCENDING: reverseArray(data[i]); break; } printResults(stats(data[i])); } } private static void reverseArray(int[] x) { for (int i = 0, j = x.length - 1 ; i < j; i++, j--) { int k = x[i]; x[i] = x[j]; x[j] = k; } } private static void printArray(int[] x, boolean nl) { System.out.print(StatsConstants.PRINT_BEGIN); for (int i = 0, j = x.length - 1; i < j; i++) System.out.print(x[i] + StatsConstants.PRINT_SEPARATOR); System.out.print(x[x.length - 1] + StatsConstants.PRINT_END); if (nl) System.out.println(); } private static void printResults(Results r) { printArray(r.data, true); StringBuffer sb = new StringBuffer("...mean: "); sb.append(r.mean).append("; median: "). append(r.median). append("; mode: "). append(r.nomode ? "modeless" : r.mode). append("; cardinality: ").append(r.cardinality). append("; range: ").append(r.range); System.out.println(sb); System.out.println(); } static class Results { public int[] data; public int cardinality; public int range; public double mean; public double median; public int mode; public boolean nomode; } /* * TBI (To Be Implemented)... * * Instantiates and returns a Results object that * contains the calculations (statistics) for the * int[] parameter. * * Note: 'mode' is a single solitary number repeated * more often than any other number. The Results object * nomode variable is set to true if there is no mode. * * The stats() method assumes the int[] is sorted * in ascending order. * * @param data an array if ints * @return a Results object */ private static Results stats(int[] data) { } /* * TBI (To Be Implemented)... * * Checks if the contents of the int[] parameter is sorted. * The method returns values defined in interface StatsConstants. * * @param data an array of integers * @return SKIP if data is null or the array is empty; * else UNSORTED or DESCENDING or ASCENDING */ private static int checkIfSorted(int[] data) { } }
The program output must match the following. Note: Every output line that starts with ... is followed by a blank line.
null element empty array [0] ...mean: 0.0; median: 0.0; mode: 0; cardinality: 1; range: 0 [1,2] ...mean: 1.5; median: 1.5; mode: modeless; cardinality: 2; range: 1 [1,1] ...mean: 1.0; median: 1.0; mode: 1; cardinality: 2; range: 0 [1,2,3] ...mean: 2.0; median: 2.0; mode: modeless; cardinality: 3; range: 2 [5,5,5,5,5] ...mean: 5.0; median: 5.0; mode: 5; cardinality: 5; range: 0 [0,2,4,5,5,8] ...mean: 4.0; median: 4.5; mode: 5; cardinality: 6; range: 8 [1,5,6,6,6,7,9] ...mean: 5.714285714285714; median: 6.0; mode: 6; cardinality: 7; range: 8 [-7,0,0,3,3,3,4,4] ...mean: 1.25; median: 3.0; mode: 3; cardinality: 8; range: 11 [-2,-2,0,1,1,2,2,2] ...mean: 0.5; median: 1.0; mode: 2; cardinality: 8; range: 4 [-4,-2,-2,3,12,12,42] ...mean: 8.714285714285714; median: 3.0; mode: modeless; cardinality: 7; range: 46
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started