This is the StatsArray class I needed to write for class. The StatsArrayGUI was
ID: 3822174 • Letter: T
Question
This is the StatsArray class I needed to write for class. The StatsArrayGUI was provided, and can't be changed. When I complie my code, the GUI doesn't show. Just get a Java window icon, which displays/is nothing.
Here's my code.
import java.awt.*;
import java.util.Random; //for our random number generator
public class StatsArray {
//instance variables
private int size; //how big is the array
private int[] stats; // an array of integers
//default constructor -overloaded method
StatsArray() {
size = 10;
stats = new int[size] ; //instantiate the array called stats
}
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 index = 0; index < size; index=index++)
{ stats[index] = random.nextInt(101); }
}
public int getSum()
{
int sum = 0;
for (int index = 0; index < stats.length; index++)
sum = sum + stats[index];
return sum;
}
public int getMax()
{
int maximum = Integer.MIN_VALUE;
for(int index = 0; index < stats.length; index++)
if(maximum < stats[index])
maximum = stats[index];
return maximum;
}
public int getMin()
{
int minimum = Integer.MAX_VALUE;
for(int index = 0; index < stats.length; index++)
if(minimum > stats[index])
minimum = stats[index];
return minimum;
}
public double getAverage()
{
return (double)getSum() / stats.length;
}
public int countValues(int lowRange, int highRange)
{
int count = 0;
for(int index = 0; index < stats.length; index++)
if((stats[index] >= lowRange) && (stats[index] <= highRange))
count++;
return count;
}
public boolean isValueFound(int someNumber)
{
//check to see if someNumber is in the array
boolean found = false;
for (int index = 0; index < stats.length; index = index++)
{
if (stats[index] == someNumber) {
found = true;
return true;} }
return found;
}
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;
}
}
public void sortBArray() {
/*Here is another common sort algorithm.
It sorts the array in ascending order.
This is called a bubble sort. I have added it
so that you can take a look at it.*/
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;
}
}
}
}
Explanation / Answer
Problem might be in GUI code..
because this code is perfectly alright except just one "}" is missing at the end.
There is no syntax error in this code.
there might be some mistake in calling this class's methods in that GUI code and that's why values may not be reflected on GUI.
check the GUI code.otherwise this code is correct.(syntactically)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.