import java.util.*; // for Scanner public class ExceptionUser // uses the OutOfRangeException class { public static void main(String[] args) { Scanner input=new Scanner(System.in); // set up for keyboard input int testScore, total=0, number; // number will be the number of test scores to input double average; // inform the user of instructions for the program System.out.println("Please enter a series of test scores and I will compute the average.\nEach test score needs to be between 0 and 100.\nHow many test scores are there?"); try { // try to get the number of tests, must be between 1 and Integer.MAX_VALUE (about +2 billion) number=input.nextInt(); if(number<=0) throw new OutOfRangeException("number of test scores (variable name: number)", number, 1); // if out of range, throw exception for(int i=0;i100) throw new OutOfRangeException("testScore", testScore, 0, 100); // if out of range (0..100) throw exception total+=testScore; // otherwise add legal score to total } catch(OutOfRangeException e) { // catch an out of range test score System.out.println(e + "\nTry again."); i--; // reset i by one because of incorrect input } } // since the try-catch is in a for loop, a wrong test score can be handled by getting a new one until the user gets it right average=(double)total/number; System.out.println("The average of " + number + " test scores is " + average); } catch(OutOfRangeException e) // catches a bad input for number (1..MAX_VALUE) { System.out.println(e); // in this case, the program terminates (we could have wrapped this inside a do-while loop if desired) } } }