How do I create a tester for my StatsArray with exception.
So I created this StatsArray.java with exception.
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
//default constructor
StatsArray() {
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()
{
int i = 0;
String input;
do
{
try
{
System.out.println("Enter a value" + stats[i]);
Scanner Scan= new Scanner(System.in);
input = Scan.next();
int iValue = Integer.parseInt(input);
checkIfNegative(iValue);
stats[i] = iValue;
i++;
}
catch (NumberFormatException e)
{
System.err.println("An int was not entered");
}
catch (IllegalArgumentException e)
{
System.err.println("Illegal argument!");
}
}
while (i < stats.length);
}
public void checkIfNegative(int someValue)
{
if (someValue < 0)
{
throw new IllegalArgumentException( );
}
}
public void displayOut()
{
for(int i = 0; i < stats.length; i++)
{
System.out.println(stats[i]);
}
}
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 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;
}
}
}
So now, how do I create my StatsArrayTester.java. So far, this is what I got.
import java.util.Scanner;
public class StatsArrayTester
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
StatsArray stats = new StatsArray();
System.out.println("Welcome to our StatsArray\n");
stats.fillArray();
}
}
Any help will be appreciated. Thanks
Re: How do I create a tester for my StatsArray with exception.
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.
What methods and features of the class do you want to test?
Make a list of each method to be tested and add some notes about how that method should be tested.
Re: How do I create a tester for my StatsArray with exception.
Never be sorry for figuring it out yourself.
Did you still need help with anything? It looks like you've figured everything out.
Re: How do I create a tester for my StatsArray with exception.
One problem is that the code has lost all its formatting. Logically nested statements should be indented.
All statements should not start in the first column.
Re: How do I create a tester for my StatsArray with exception.
I still need help. That output is the expected output. I don't know how to fill its array with values form the user by invoking the fillArrayFromUser method and displaying the array by invoking the displayOut method.
Re: How do I create a tester for my StatsArray with exception.
The fillArrayFromUser method has code in it. What happens when the code is executed?
When debugging, use the Arrays class's toString() method to print out the contents of arrays so you can see what is in them:
Code :
System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
Re: How do I create a tester for my StatsArray with exception.
Ohhh. I thought that was the actual output. I wasn't paying attention.
Just as you called the fillArray() method in the main() method, you'll call the fillArrayFromUser() method. Try that and see what happens. From there, you should start to understand the concept of calling methods of a class using an instance of that class.
Re: How do I create a tester for my StatsArray with exception.
First when I called to do the
stats.fillArray();
nothing happened. When I added stats.fillArrayFromUser();, this is what's printed.
Enter a value91
Looks likes its generating a random number.
Re: How do I create a tester for my StatsArray with exception.
Quote:
First when I called to do the
stats.fillArray();
nothing happened.
Something happened. Look at the fillArray() method to "see" what happened.
Quote:
When I added stats.fillArrayFromUser();, this is what's printed.
Enter a value91
Looks likes its generating a random number.
Kind of. Again, look at the fillArrayFromUser() method to see what your program is doing.
You seem to be unfamiliar with this code or surprised by what it's doing, almost as though you didn't write it. That's a bit odd.
Re: How do I create a tester for my StatsArray with exception.
Our instructor actually gave us the code and we just have to add the +fillArrayFromUser(). I'm still learning the array and I get confused with it.