Testing the Method

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Perhaps. Usually "tools" merely indicate that an error has happened and let their caller decide on what error message to print.

Testing the Method

The modified method needs to be tested. Here is the testing class with several calls to printRange():

class ArrayDemo
{
  public static void main ( String[] args ) 
  {
    ArrayOps operate = new ArrayOps();
    int[] ar1 =  { -20, 19, 1, 5, -1, 27, 19, 5 } ;
    
    System.out.println("Test A:");
    operate.printRange( ar1, 0, 3);
    
    System.out.println("Test B:");
    operate.printRange( ar1, -1, 4);
    
    System.out.println("Test C:");
    operate.printRange( ar1, 1, 12);
  }

}      

The program prints:

Test A:
-20 19 1
Test B:

Test C:
19 1 5 -1 27 19 5

In "Test B" the method is asked to start with an index of -1. The test in the for loop returns false right away, and the loop body is never executed.

In "Test C" the method is asked to print beyond the end of the array, but it quits after it has printed the last element.

QUESTION 14:

What will this call print out:

    operate.printRange( ar1, 3, 3);