Error Checking

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:

19 1 5 -1 27

Error Checking

The method requires that its parameters contain correct data. The following will not work:

operate.printRange( ar1, 1, 10 );

There are only eight elements of ar1. If you asked to print elements 1 through 10 you would see:

19 1 5 -1 27 19 5 java.lang.ArrayIndexOutOfBoundsException

When the method tries to access the non-existing 8th element, the program throws an exception. Here is the method, again, with some new blanks:

class ArrayOps
{
  . . .

  // print elements start through end
  void printRange ( int[] x, int start, int end )
  {
    for ( int index=start; index <= end &&  && ; index++  )
      System.out.print( x[index] + " " );
    System.out.println();
  }

}

The new version of the method makes sure that index is zero or greater, and is less than the length of the array.

QUESTION 12:

Complete the new, improved method.