Define a class DataSet that computes the sum, the average, the largest value, an
ID: 3629788 • Letter: D
Question
Define a class DataSet that computes the sum, the average, the largest value, and the smallest value of a sequence of integers. Supply the following methods in your class:void addValue(int x)?In addition to tracking the sum and the count of the values to get the average, this method should keep track of the largest and the smallest values that you've seen so far. Use the Math.min and Math.max methods to update these two values when you process the input parameter x. Use Integer.MIN_VALUE for the initial minimum value and Integer.MAX_VALUE for the initial maximum value.
int getSum()
double getAverage()
int getLargest()
int getSmallest()
Explanation / Answer
It show average , min value , max value and sum of the numbers in every step. To exit enter non integer data(char etc ...) import java.math.*; import java.util.*; public class cramster3 { static int sum = 0 ,max = 0 , min = 0 , data = 0 , lenght = 0; static int arr[] = new int[100]; static Boolean bo = true; public static void main(String [] args) { while(bo) { addvalue(); System.out.println("Max value : " + max); System.out.println("Min value : " + min); System.out.println("Sum : " + getSum() ); System.out.println(" Average : " + getAverage() ); } } static void addvalue() { Scanner input = new Scanner(System.in); if(input.hasNextInt()) data = input.nextInt(); else bo = false; if(lenght == 0) { max = data; min = data; } lenght++; sum += data; max = Math.max(max, data); min = Math.min(min, data); } static int getSum() { return sum; } static double getAverage() { return (double)sum/lenght; } int getLargest() { return max; } int getSmallest() { return min; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.