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

Stats.java Design, code, debug, and test a Stats class that computes basic stati

ID: 670284 • Letter: S

Question

Stats.java

Design, code, debug, and test a Stats class that computes basic statistics for a set of integers, without using any methods in Java's Math class. Also design, code, debug, and test aStatsTest class with unit tests for each method in Stats. Your Stats class should contain an array field with the set of integers, and the following methods and constructors (I strongly suggest that you code & test them in order):

setValues(int[] data) that checks for errors and sets the field to the array parameter.

getSize() that returns the number of values in the array field.

toString() that uses a loop to return a String containing all of the integers.

constructor Stats(int[] data) that takes an array of integers as a parameter.

getTotal() that uses a loop to return the sum of all of the values.

getAverage() that returns the average value (the sum of all of the values divided by the number of values).

getMin() and getMax() that each use a loop to return the smallest and largest values in the array.

setValues(int size) that creates an array of that size, and prompts the user to enter that number of values.

constructor Stats(int size) that takes an integer parameter, and calls setValues(size).

setValues() with no parameters that prompts the user for the number of values, and then prompts the user to enter that number of values.

a constructor Stats() that calls setValues().

replace(int old, int new) with two integer parameters that finds each occurrence of the first integer in the array, replaces it with the second integer, and returns the number of such replacements that occurred.

Explanation / Answer

package mani;

import java.util.Random;
import java.util.Scanner;

public class stats {
int[] arr;
public stats(){
   setValues();
}
public stats(int s){
   setValues(s);
}
public stats(int[] data){
   setValues(data);
}
public void setValues(){
  
  
   Scanner input=new Scanner(System.in);
   System.out.print("Enter the size of array: ");
   int s=input.nextInt();
   arr=new int[s];
   System.out.println("Enter the array values.");
   for(int i=0;i<s;i++){
       System.out.print("a["+i+"]= ");
       arr[i]=input.nextInt();
   }
}
public void setValues(int[] data){
   arr=new int[data.length];
   for(int i=0;i<data.length;i++){
      
       arr[i]=data[i];
   }
}
public void setValues(int s) {
   arr=new int[s];
   Scanner input=new Scanner(System.in);
   System.out.println("Enter the array values.");
   for(int i=0;i<s;i++){
       System.out.print("a["+i+"]= ");
       arr[i]=input.nextInt();
   }
}
public double getAverage(){
   double avg=0;
   for(int i=0;i<arr.length;i++){
       avg=avg+arr[i];
   }
   avg=avg/arr.length;
   return avg;
}
public int getTotal(){
   int sum=0;
   for(int i=0;i<arr.length;i++){
       sum=sum+arr[i];
   }
   return sum;
}
public int getMin(){
   int min=arr[0];
   for(int i=0;i<arr.length;i++){
       if(min>arr[i]){
           min=arr[i];
       }
   }
   return min;
}
public int getMax(){
   int max=arr[0];
   for(int i=0;i<arr.length;i++){
       if(max<arr[i]){
           max=arr[i];
       }
   }
   return max;
}
public int getSize(){
   return arr.length;
}
public int replace(int old,int New){
   int count=0;
   for(int i=0;i<arr.length;i++){
       if(arr[i]==old){
           arr[i]=New;
           count++;
       }
   }
   return count;
}
public String toString(){
   String output="";
   for(int i=0;i<arr.length;i++){
       output=output+Integer.toString(arr[i])+" ";
   }
   return output;
}


}

package mani;

import java.util.Scanner;

public class statsTester{
   public static void main(String[] args){
       int[] a=new int[10];
       stats s1=new stats();
       System.out.println("Array of integers of object1: "+s1.toString());
       stats s2=new stats(10);
       System.out.println("Array of integers of object2: "+s2.toString());
       Scanner input=new Scanner(System.in);
       System.out.println("Enter the array values.");
       for(int i=0;i<10;i++){
           System.out.print("a["+i+"]= ");
           a[i]=input.nextInt();
       }
       stats s3=new stats(a);
       System.out.println("sum of the integers: "+s3.getTotal());
       System.out.println("average of the integers: "+s3.getAverage());
       System.out.println("Maximum of the integers: "+s3.getMax());
       System.out.println("Minimum of the integers: "+s3.getMin());
       System.out.println("how many times the 1 is replaced by 11: "+s3.replace(1, 11));
   }
}