In: Computer Science
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
import java.util.*; /* * This Java application calculates some statistics * given an array of integers. * * @creator gdt * @created 02017.12.15 * @updated 02019.01.21 morphed the assignment */ 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\n"); else if (data[i].length == 0) System.out.println("empty array\n"); else System.out.println("???\n"); 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
Answer:
Progam:
import java.util.*;
interface StatsConstants {
String PRINT_BEGIN = "[";
String PRINT_END = "]";
String PRINT_SEPARATOR = ",";
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\n");
else if (data[i].length == 0)
System.out.println("empty array\n");
else
System.out.println("???\n");
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;
}
//Result of stats
private static Results stats(int[] data) {
Results result = new
Results();
int sum = data[0];
int maxCount = 0, count
= 0;
result.data =
data;
result.cardinality =
data.length;
result.range =
data[data.length-1] - data[0];
result.mode =
data[0];
result.nomode =
data.length != 1;
if (data.length % 2
== 0)
result.median = (data[data.length/2] + data[(data.length/2) - 1]) /
2.0;
else
result.median = data[data.length/2];
for (int i = 1; i
< data.length; i++) {
sum += data[i];
if (data[i] == data[i-1]) {
count++;
if (count > maxCount) {
result.nomode = false;
result.mode = data[i];
maxCount = count;
} else if (count == maxCount) {
result.nomode = true;
}
} else {
count = 0;
}
}
result.mean = sum / (double) data.length;
return result;
}
// Checks if sorted method
private static int checkIfSorted(int[] data)
{
if (data == null ||
data.length == 0)
return StatsConstants.SKIP;
if (data[0] <
data[data.length - 1]) {
for (int i = 0; i < data.length-1; i++) {
if (data[i] > data[i + 1])
return StatsConstants.UNSORTED;
}
return StatsConstants.ASCENDING;
} else if (data[0] >
data[data.length - 1]) {
for (int i = 0; i < data.length-1; i++) {
if (data[i] < data[i + 1])
return StatsConstants.UNSORTED;
}
return StatsConstants.DESCENDING;
}
return
StatsConstants.UNSORTED;
}
}
Out put: