Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

StatsArray with Exceptions /* Example output: (please show your code handling exceptions) Welcome to our StatsArray Enter value [0] : 10 Enter value [1] :

StatsArray with Exceptions

image text in transcribed

/*

Example output: (please show your code handling exceptions) Welcome to our StatsArray Enter value [0] : 10 Enter value [1] : 11.1 Invalid value. Enter an int. Enter value [1] : 11 Enter value [2] : 20 Enter value [3] : hello Invalid value. Enter an int. Enter value [3] : 30 Enter value [4] : 44 Enter value [5] : 5 Enter value [6] : -66 Negative value. Not allowed. Enter value [6] : 6 Enter value [7] : 777 Enter value [8] : 81 Enter value [9] : 19

*/

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

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[i] = random.nextInt(101) ;

}}

public int getSum(){

//add up all the values in the arrayint total = 0;for (int i = 0; i

} public int getMax(){

//return the maximum value in the array

int maxValue = stats[0];for (int i = 0; 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

} 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

if ( (stats[i] >= lowRange) && (stats[i]

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

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

{

if (stats[j]

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

min = i;for (int j = (i + 1); j

if (stats[j]

min = j;

}

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

}

}

}

tics Array with Exceptions Class. (10 points) Using the Stats Array class provided (or your own) modify the class based on the UML below. You will be adding 3 new methods to the StatsArray class Stats Array size int the size of the array stats intI l flan int array named stats +fillArray() void llfill the int array with random data in the range 100 +fillArrayFromUser(): void l *New prompt the user to input values. ll See additional information below UML. +checklfNegative(someValue int) void *New throws IllegalArgumentException ll if someValue is negative. ll See additional information below UML. +display (Graphics g) void lfdisplay the contents of the array on a GUI +displayout(): void ll New display the contents of the array using System.out. ll See additional information below UML +getMax(): int Ilfind and return largest value max +getMin0 int llfind and return smallest value min +getSum() int //find and return sum of all values in the array +getAverage0 double llfind and return the average of all values in the array +count Values (int lowRange, int highRange) i count how many numbers are IowRange and highRange +isValueFound(int someNumber): boolean if the array contains someNumber, return true otherwise return false +sortArray() void ilsort 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() Invoke 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 IllegalArgumentException if it is thrown Assign the good value to the next element in the array checklfNegative method. Throw an egal ArgumentException if the value passed in is negative. Throw statement might look like th throw new IllegalArgumentException displayout method. Using a for loop, display all of the elements of the array using System.out.println. It should look like the output in the example output belo 2.statsArray Tester,ja (10 points) Implement a tester class called StatsArrayTester to test your modified StatsArray class Va Specifically, StatsArray Tester should do the following in this order. See the output for further clarification Create a StatsArray object called values The values object will fill its array with values from the user by invoking the filArrayFromUser method The values o bject 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 all of the elements in values' array using the getAverage method Calculate and display the maximum of all of the elements in values' array using getMax method. Calculate and display the minimum of all the elements in the values' array using the getMin method The values object will sort its array by invoking the sortArray method The values object will display the current contents of its array by invoking the displayOut method tics Array with Exceptions Class. (10 points) Using the Stats Array class provided (or your own) modify the class based on the UML below. You will be adding 3 new methods to the StatsArray class Stats Array size int the size of the array stats intI l flan int array named stats +fillArray() void llfill the int array with random data in the range 100 +fillArrayFromUser(): void l *New prompt the user to input values. ll See additional information below UML. +checklfNegative(someValue int) void *New throws IllegalArgumentException ll if someValue is negative. ll See additional information below UML. +display (Graphics g) void lfdisplay the contents of the array on a GUI +displayout(): void ll New display the contents of the array using System.out. ll See additional information below UML +getMax(): int Ilfind and return largest value max +getMin0 int llfind and return smallest value min +getSum() int //find and return sum of all values in the array +getAverage0 double llfind and return the average of all values in the array +count Values (int lowRange, int highRange) i count how many numbers are IowRange and highRange +isValueFound(int someNumber): boolean if the array contains someNumber, return true otherwise return false +sortArray() void ilsort 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() Invoke 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 IllegalArgumentException if it is thrown Assign the good value to the next element in the array checklfNegative method. Throw an egal ArgumentException if the value passed in is negative. Throw statement might look like th throw new IllegalArgumentException displayout method. Using a for loop, display all of the elements of the array using System.out.println. It should look like the output in the example output belo 2.statsArray Tester,ja (10 points) Implement a tester class called StatsArrayTester to test your modified StatsArray class Va Specifically, StatsArray Tester should do the following in this order. See the output for further clarification Create a StatsArray object called values The values object will fill its array with values from the user by invoking the filArrayFromUser method The values o bject 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 all of the elements in values' array using the getAverage method Calculate and display the maximum of all of the elements in values' array using getMax method. Calculate and display the minimum of all the elements in the values' array using the getMin method The values object will sort its array by invoking the sortArray method The values object will display the current contents of its array by invoking the displayOut method

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Science Project Ideas In Health Care Volume 1

Authors: Zemelak Goraga

1st Edition

B0CPX2RWPF, 979-8223791072

More Books

Students also viewed these Databases questions