Your software company has been contracted to write a prototype program to perfor
ID: 3803975 • Letter: Y
Question
Your software company has been contracted to write a prototype program to perform simple statistical evaluations of integer data. Your portion of this program requires that you implement one object class: StatPackage. For final testing your class will be compiled and linked to a driver program. You need to throughly test your package with your own drivers prior to delivery of your code.
The interface specification for the StatPackage is:
The functional specification for your package is:
Your StatPackage will accept up to 500 values as input.
Mean is the mean value for the values inserted into the StatPackage, mean is essentially the same as the average.
Median is middle value if the number of items is odd or the average of the middle two values if the number of items is even.
Variance is determined by the formula:
StdDev is the square root of the variance
Histogram is a graphical output of the items inserted into the StatPackage. The format of the Histogram is:
Where each '*' represents 5 items in the partition.
Explanation / Answer
StatPackage.java:
public class StatPackage {
double values[] = new double[500];
int no_of_values = 0;
StatPackage() {
}
public void insert(double value) {
if (value <= 0 || value > 100) {
throw new IllegalArgumentException();
}
values[no_of_values++] = value;
}
public void Histogram() {
int ranges[] = new int[10];
for (int i = 0; i < no_of_values; i++) {
int s = (int) ((values[i] - 1) / 10);
ranges[s] = ranges[s] + 1;
}
for (int i = 0; i < 10; i++) {
System.out.print((i*10 + 1) + " - " + (i*10 + 10) + "| ");
for (int j = 0; j < Math.ceil(ranges[i] / 5.0); j++)
System.out.print("*");
System.out.println();
}
}
// Calculate the mean of the data in the List
public double Mean() {
double sum = 0;
for (int i = 0; i < no_of_values; i++) {
sum += values[i];
}
return no_of_values != 0 ? sum / no_of_values : 0;
}
// Calculate the median value for the data in the List
public double Median() {
if (no_of_values % 2 == 1) {
// odd;
return values[no_of_values / 2];
} else {
// even;
return (values[(no_of_values - 1) / 2] + values[(no_of_values) / 2]) / 2.0;
}
}
// Calculate the variance value for the data in the List
public double Variance() {
if(no_of_values == 0)
return 0;
double sum = 0;
for (int i = 0; i < no_of_values; i++) {
sum += values[i] * values[i];
}
double result1 = sum/no_of_values;
sum = 0;
for (int i = 0; i < no_of_values; i++) {
sum += values[i];
}
double result2 = (sum*sum)/(no_of_values*no_of_values);
return result1 - result2;
}
// Calculate the standard deviation value for the data in the List
public double StdDev() {
return Math.sqrt(Variance());
}
}
Driver.java:
import java.util.Scanner;
public class Driver
{
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
StatPackage sp = new StatPackage();
while(true) {
System.out.print("Enter a value(-1 to exit): ");
Double value = Double.parseDouble(in.nextLine());
if(value == -1)
break;
sp.insert(value);
printDetails(sp);
}
in.close();
}
private static void printDetails(StatPackage sp) {
sp.Histogram();
System.out.println("Mean: " + sp.Mean());
System.out.println("Median: " + sp.Median());
System.out.println("Variance: " + sp.Variance());
System.out.println("Standard Deviation: " + sp.StdDev());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.