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

This is a two part question. I will post a fast review for quick, complete solut

ID: 3833304 • Letter: T

Question

This is a two part question. I will post a fast review for quick, complete solution.

I) Using theStatsArray class provided, modify the class based on the UML below. You will be adding three new methods to the StatsArray class.

II) Implement a tester class called StatsArrayTester to test your modified StatsArray class. Specifically, StatsArrayTester should do the following in this order. See the output for further clarification.

See screenshots, skeleton code for StatsArray class below.

import java.awt.*;

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

public class StatsArray {

private int size; //how big is the array

private int[ ] stats; // an array of integers

StatsArray() { //default constructor

size = 10;

stats = new int[size];

}

public void display(Graphics g)

   {

int x = 50; //coordinates for displaying

int y = 40; //display the array with position number

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 (int) in the range 0 - 100

Random 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 array

int 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 array

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 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 <= 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) { //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. (10 points) 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 intI] llan int array named stats +fillArray() void lfill the int array with random data in the range 0-100 +fillArrayFromUser void l New prompt the user to input values. ll see additional information below UML. +checklf Negative (someValue int) void l New throws IllegalArgumentException ll if somevalue is negative. Il See additional information below UML +display (Graphics g) void lldisplay the contents of the array on a GUI +displayout void ll New display the contents of the array using System.out See additional information below UML. +getMax int //find and return largest value max +getMin0 int //find and return smallest value min +getSum() int llfind 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 IowRange and highRange +isValueFound(int someNumber): boolean ll if the array contains so return true otherwise return false +sortArray() void //sort the array

Explanation / Answer

Given below,

File: StatsArray.java

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

public class StatsArray {
private int size; //how big is the array
private int[] stats; // an array of integers

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

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

public void fillArrayFromUser() {
// prompt the user to input values
int i = 0;
Integer someValue;
Scanner scanner = new Scanner(System.in);

do {
try {
System.out.print("Enter value [" + i + "] : ");
someValue = Integer.parseInt(scanner.nextLine());
checkIfNegative(someValue);
stats[i] = someValue;
i++;
} catch (NumberFormatException e) {
System.out.println("Invalid value. Enter an int.");
} catch (IllegalArgumentException e) {
System.out.println("Negative value. Not allowed.");
}
} while (i < size);

}

public void checkIfNegative(int someValue) {
// throws IllegalArgumentException if someValue is negative
if (someValue < 0) {
throw new IllegalArgumentException();
}
}

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

public void displayOut() {
System.out.println("Current Array Values");
System.out.println("----------------------------");

int sum = 0;
int min = 0;
int max = 0;

for (int i = 0; i < stats.length; i++) {
System.out.println(" Stats [" + i + "] = " + stats[i]);
sum += stats[i];
if (i == 0) {
min = stats[i];
max = stats[i];
} else {
if (stats[i] < min) {
min = stats[i];
}
if (stats[i] > max) {
max = stats[i];
}
}

}
}

public int getSum() {
//add up all the values in the array
int 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 array
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 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 <= 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) {
//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;
}
}
}

File: StatsArrayTester.java

public class StatsArrayTester {
public static void main(String[] args) {
System.out.println("Welcome to our StatsArray ");

StatsArray values = new StatsArray();

values.fillArrayFromUser();

values.displayOut();

System.out.println(String.format(" Sum : %d", values.getSum()));
System.out.println(String.format("Average : %.1f", values.getAverage()));
System.out.println(String.format("Max value: %d", values.getMax()));
System.out.println(String.format("Min value: %d", values.getMin()));

values.sortArray();

values.displayOut();

System.out.println(" Goodbye");
}
}

Sample Execution Output:

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
Current Array Values
----------------------------
   Stats [0] = 10
   Stats [1] = 11
   Stats [2] = 20
   Stats [3] = 30
   Stats [4] = 44
   Stats [5] = 5
   Stats [6] = 6
   Stats [7] = 777
   Stats [8] = 81
   Stats [9] = 19

Sum : 1003
Average : 100.3
Max value: 777
Min value: 5
Current Array Values
----------------------------
   Stats [0] = 5
   Stats [1] = 6
   Stats [2] = 10
   Stats [3] = 11
   Stats [4] = 19
   Stats [5] = 20
   Stats [6] = 30
   Stats [7] = 44
   Stats [8] = 81
   Stats [9] = 777

Goodbye

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