Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: How do I create a tester for my StatsArray with exception.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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:
       System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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.

  8. #8
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I create a tester for my StatsArray with exception.

    First when I called to do the

    stats.fillArray();

    nothing happened.
    Something happened. Look at the fillArray() method to "see" what happened.
    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.

  10. #10
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. We get the exception :Cannot create a file when that file already exists
    By yatin.baraiya in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: May 25th, 2012, 10:33 AM
  2. Dog Tester Class
    By mwardjava92 in forum Object Oriented Programming
    Replies: 4
    Last Post: February 11th, 2012, 05:33 PM
  3. Help with GUI Password Tester
    By java.kid21 in forum AWT / Java Swing
    Replies: 10
    Last Post: December 13th, 2010, 08:06 AM
  4. Password Tester GUI HELP
    By java.kid21 in forum Object Oriented Programming
    Replies: 3
    Last Post: December 7th, 2010, 11:35 PM
  5. bandwith tester
    By messithe1 in forum Java SE APIs
    Replies: 0
    Last Post: January 19th, 2010, 10:37 PM