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

StatsArray not sure what i need to be doing. import java.awt.*;import java.util.

ID: 3819290 • Letter: S

Question

StatsArray not sure what i need to be doing.

import java.awt.*;import java.util.Random; //for our random number generator
public class StatsArray {

private int size; //how big is the arrayprivate int[ ] stats; // an array of integers
//default constructorStatsArray() {

size = 10;stats = new int[size];

}
public void display(Graphics g)

   {

int x = 50; //coordinates for displayingint y = 40;//display the array with position numberfor(int i = 0; i < stats.length; i++) {

g.drawString("Stats [" + i + "] = "+ stats[i],x, (y + 15 * i) );

}

}
public void fillArray(){

//fill the array with random numbers (int) in the range 0 - 100Random random = new Random();for(int i = 0; i < stats.length; i++) {

stats[i] = random.nextInt(101) ;

       }}

public int getSum(){

//add up all the values in the arrayint total = 0;for (int i = 0; i < stats.length; i++)total = total + stats[i];return total;

}
public int getMax(){

//return the maximum value in the array

int maxValue = stats[0];for (int i = 0; i < stats.length; i++){if (stats[i] > maxValue)maxValue = stats[i];}return maxValue;

}
public int getMin(){

//return the minimum value in the arrayint minValue = stats[0];

for (int i = 0; i < stats.length; i++){if (stats[i] < minValue)minValue = stats[i];}return minValue;

}
public double getAverage(){

//return the average. must be a double

return (double)getSum() / stats.length;

}
public int countValues(int lowRange, int highRange){

//count how many numbers are >= lowRange and <= highRangeint count = 0;for (int i = 0; i < stats.length; i++) {

if ( (stats[i] >= lowRange) && (stats[i] <= highRange) ){

count++;

}

}return count;

}
public boolean isValueFound(int someNumber) {//check to see if someNumber is in the array

boolean found = false;for(int i = 0; (i < stats.length && !found); i++) {

if (stats[i] == someNumber) {found = true;}

}return found;

}
public void sortBArray() {

/*sort the array in ascending order - bubble sort*/int tempValue;for (int i = 0; i < (stats.length - 1); i++)
{for (int j = (i + 1); j < (stats.length); j++)

{

if (stats[j] < stats[i]){tempValue = stats[i];

stats[i] = stats[j];stats[j] = tempValue;

}

}

}

}

    public void sortArray() {

/*sort the array in ascending order - selection sort*/
int tempValue;int min;
for (int i = 0; i < (stats.length - 1); i++){

min = i;for (int j = (i + 1); j < (stats.length); j++){

if (stats[j] < stats[min]){

min = j;

}

}tempValue = stats[min];stats[min] = stats[i];stats[i] = tempValue;

}

}

}

1 Statistics Array with Exceptions Class. Using the StatsArray class provided (or your own), modify the class based on the UML below. You will be adding 3 new methods to the StatsArray class StatsArray size int /the size of the array stats int llan int array named stats +fillArray0 void llfill the int array with random data in the range 0 100 +fillArrayFromUser(): void *New prompt the user to input values. ll See additional information below UML some value int) void Newt throws IllegalArgumentException ll if somevalue is negative. See additional information below UML. +display (Graphics g) void lfdisplay the contents of the array on a GUI +displayout(): void l *New display the contents of the array using System.out. ll See additional information below UML. +getMax0 nt I/find and return largest value max +getMin0 int lifind and return smallest value min +getSum0 int /Mind and return sum of all values in the array +getAverage double lifind and return the average of all values in the array +countValues(int lowRange, int highRange) int //count how many numbers are owRange and highRange +isValueFound (int someNumber): boolean ll if the array contains someNumber, return true otherwise return false +sortArray0 void //sort the array Additional Information about StatsArray implementation: fillArrayFromUser method Using a do..while loop, a try block and two catch blocks, do the following Prompt the user for a value. Your prompt should look like the example output should indicate to the user which element they are inputting a value into Using Scanner, read each value input by the user as a String Convert the value to an integer using Integer.parselnt() oke the checklfNegative method to see if the number is negative. With a catch statement, handle NumberFormatException (an int was not entered) if it is thrown With another catch statement, handle ArgumentException if it is thrown Assign the good value to the next element in the array checklfNegative method. Throw a egalArgumentException if the value passed in is negative. Throw statement might look like this throw new egal Argument Exception displayout method. Using a for loop, display all of the elements of the array using System.out.println. lt should look like the output in the example output below class called StatsArrayTester to test your modified StatsArray class. Specifically, 2.StatsArrayTester java Implement a te StatsArrayTester should do the following in this order. See the output for further clarification Create a StatsArray object caled values The values object will fill its array with values from the user by invoking the flArrayFromuser method The values object will display the current contents of its array by invoking the displayOut method Calculate and display the sum of all of the elements in values' array using the getSum method Calculate and display the average of al of the elements in values' array using the getAverage method. Calculate and display the maximum of all of the elements in values' arra using getMax method Calculate and display the minimum of all the elements in the values' array using the getMin method The values object sort its array by invoking the sortArray method The values object will display the cument contents of its array by invoking the displayOut method

Explanation / Answer

import java.awt.*;
import java.util.Random; //for our random number generator
import java.util.Scanner;


public class StatsArray {

   private int size;
   private int[ ] stats;

       
   //default constructor
   StatsArray() {
       size = 10;
       stats = new int[size];
   }


   public void display(Graphics g)
   {
       int x = 50; //coordinates
       int y = 40;
       //display the array
       for(int i = 0; i < stats.length; i++)
       {
           g.drawString("Stats [" + i + "] = "+ stats[i],
           x, (y + 15 * i) );
       }
   }

   public void fillArray()
   {
       //fill the array with random numbers from 0 - 100
       Random random = new Random();
       for(int i = 0; i < stats.length; i++)
       {
           stats[i] = random.nextInt(101) ;
       }
   }

   public int getSum()
   {
      
       int total = 0;
       for (int i = 0; i < stats.length; i++)
           total = total + stats[i];
       return total;
   }


   public int getMax()
   {
      
           int maxValue = stats[0];
           for (int i = 0; i < stats.length; i++){
               if (stats[i] > maxValue)
                   maxValue = stats[i];
           }
           return maxValue;
   }

   public int getMin()
   {
      
           int minValue = stats[0];
           for (int i = 0; i < stats.length; i++){
               if (stats[i] < minValue)
                   minValue = stats[i];
           }
           return minValue;
   }

   public double getAverage()
   {
      
           return (double)getSum() / stats.length;
   }

   public int countValues(int lowRange, int highRange)
   {
      
       int count = 0;
       for (int i = 0; i < stats.length; i++) {
           if ( (stats[i] >= lowRange) && (stats[i] <= highRange) )
           {
               count++;
           }
       }
       return count;
   }


   public boolean isValueFound(int someNumber) {
      
       boolean found = false;

       for(int i = 0; (i < stats.length && !found); i++) {
           if (stats[i] == someNumber) {
               found = true;
           }
       }
       return found;
   }


   public void sortBArray() {
      

       int tempValue;

       for (int i = 0; i < (stats.length - 1); i++)
       {
           for (int j = (i + 1); j < (stats.length); j++)
           {
               if (stats[j] < stats[i])
               {
                   tempValue = stats[i];
                   stats[i] = stats[j];
                   stats[j] = tempValue;
               }
           }
       }

   }


    public void sortArray() {
          

           int tempValue;
           int min;

           for (int i = 0; i < (stats.length - 1); i++)
           {
               min = i;
               for (int j = (i + 1); j < (stats.length); j++)
               {
                   if (stats[j] < stats[min])
                   {
                       min = j;
                   }
               }
               tempValue = stats[min];
               stats[min] = stats[i];
               stats[i] = tempValue;


           }

       }
    public void fillArrayFromUser()
    {
       int value = 0;
        String input;
        String keepGoing = "y";
        int i=0;
       Scanner scan = new Scanner(System.in);
       do
       {
           boolean flag=true;
           while(flag){
          try{
           System.out.println("Enter the "+i+" value of the array :" );
           input=scan.next();
           value=Integer.parseInt(input);         
           CheckIfNegative(value);
           flag = false;
          }catch (NumberFormatException e) {
                System.out.println("You've entered non-integer number");
                System.out.println("This caused " + e);
            }catch(IllegalArgumentException s){
        System.out.println("Negative encouneterd. value must be non-negative");
           
        }
        }         
           stats[i]=value;
           i=i+1;
           System.out.println("Enter y to continue");
           keepGoing = scan.next();
       }while(keepGoing.equals("y") || keepGoing.equals("Y"));
          
    }
   public void CheckIfNegative(int someValue)
    {
       if(0 > someValue)
       {
           throw new IllegalArgumentException( );
       }
    }

  
    public void displayOut()
    {
       for (int i = 0; i < (stats.length); i++)
       {
           System.out.println(" Value stats["+i+"]="+stats[i]);
       }
    }
    public static void main(String [] args)
    {
        StatsArray values=new StatsArray() ;
        values.fillArrayFromUser();
        values.displayOut();
        System.out.println("The total sum of array is : "+values.getSum());
        System.out.println("The average of array is : "+values.getAverage());
       System.out.println("The maxium element of array is : "+values.getMax());
       System.out.println("The minimum element of array is : "+values.getMin());
       values.sortArray();
       values.displayOut();
    }
}

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