Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java class DataSet as described by the following class diagram. A DataSe

ID: 3571211 • Letter: W

Question

Write a Java class DataSet as described by the following class diagram. A DataSet object contains aggregate information about a collection of integer data items. A newly constructed DataSet has no data items. As data items are added to the set, the minimum, maximum, sum and count are updated. At any time, the client can retrieve the minimum, maximum, or average of the data added so far. The isEmpty() method returns true if no data items have been added and false otherwise. If isEmpty() returns true, the min, max, and average are undefined, in other words the getMin(), getMax(), and get Avg() methods could return any value.

Write pseudo code for the addDatum method before coming to lab on Friday.

1.       Write a client program that reads integers from the keyboard until the “sentinel” value of 0 is entered. When the user enters 0 the program will print the min, max, and average of all the numbers entered (not counting the sentinel 0). If no data were entered before the sentinel appeared, display the message "no data entered" instead of the statistics. Be sure to prompt the user for each integer. Before you write the Java code for this exercise, create a flow chart for the process.

2.       Modify your client program from exercise 1 so that it maintains two data sets: one for the positive data and another for the negative data. When the sentinel 0 is entered, the program should display the min, max, and average for the positives and for the negatives. Display the messages "no negative data entered" and "no positive data entered" instead of the statistics as appropriate.

Challenge: Improve your DataSet class so that it also tracks the second-to-largest and second-to-smallest data values. Take into account duplicate values.

Explanation / Answer

public class StatCal {

private int count; // Number of numbers that have been entered.
private double sum; // The sum of all the items that have been entered.
private double squareSum; // The sum of the squares of all the items.
private double max = Double.NEGATIVE_INFINITY; // Largest item seen.
private double min = Double.POSITIVE_INFINITY; // Smallest item seen.

public void enter(double num) {
// Add the number to the dataset.
count++;
sum += num;
squareSum += num*num;
if (num > max)
max = num;
if (num < min)
min = num;
}

public int getCount() {   
// Return number of items that have been entered.
return count;
}

public double getSum() {
// Return the sum of all the items that have been entered.
return sum;
}

public double getMean() {
// Return average of all the items that have been entered.
// Value is Double.NaN if count == 0.
return sum / count;
}

public double getStandardDeviation() {
// Return standard deviation of all the items that have been entered.
// Value will be Double.NaN if count == 0.
double mean = getMean();
return Math.sqrt( squareSum/count - mean*mean );
}
  
public double getMin() {
// Return the smallest item that has been entered.
// Value will be infinity if no items have been entered.
return min;
}
  
public double getMax() {
// Return the largest item that has been entered.
// Value will be -infinity if no items have been entered.
return max;
}

} // end class StatCalc

Main Program



public class SimpleStats {

public static void main(String[] args) {

StatCalc calc; // Computes stats for numbers entered by user.
calc = new StatCalc();

double item; // One number entered by the user.

TextIO.putln("Enter your numbers. Enter 0 to end.");
TextIO.putln();

do {
TextIO.put("? ");
item = TextIO.getlnDouble();
if (item != 0)
calc.enter(item);
} while ( item != 0 );

TextIO.putln(" Statistics about your calc: ");
TextIO.putln(" Count: " + calc.getCount());
TextIO.putln(" Sum: " + calc.getSum());
TextIO.putln(" Minimum: " + calc.getMin());
TextIO.putln(" Maximum: " + calc.getMax());
TextIO.putln(" Average: " + calc.getMean());
TextIO.putln(" Standard Deviation: " + calc.getStandardDeviation());

} // end main()
  
} // end SimpleSta

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote