Homework #7 Assignment

Create a project around the following code:

import java.util.Scanner;

import javax.swing.JOptionPane;

public class DividerApp {

	/**
	 * @param args not used
	 */
	public static void main(String[] args) {
		String input = JOptionPane.showInputDialog("Enter two integer values whose quotient will be computed:");
		try {
			//TODO: guard against null input; quit the program if null
			Scanner reader = new Scanner(input);
			double numerator = reader.nextDouble();
			double denominator = reader.nextDouble();
			double quotient = Calculator.divide(numerator, denominator);
			JOptionPane.showMessageDialog(null, "The quotient is " + quotient);
		}
		//TODO: catch all exceptions that can be generated by the Scanner; loop back to the first instruction if exceptions are encountered	
		//TODO: catch exceptions generated by the Calculator; print a "can't divide by zero" message instead of the quotient	
	}
}

//This local class performs simple calculations
//Note the class is not public, so it can be placed in the same file as the public main class
class Calculator {
	// private ctor; make this class non-instantiable
	private Calculator() {
		// do nothing
	}

	/**
	 * compute the quotient of the values specified as arguments
	 * @param num the numerator
	 * @param denom the denominator
	 * @return the quotient numerator/denominator
	 * @throws checked Exception if denominator is 0
	 */
	public static double divide( double num, double denom ) {
		//TODO: Create and throw a checked Exception if the denominator is 0; set the Exception reason to "can't divide by zero"
		return num/denom;
		//Note: Without exceptions, dividing by zero would result in a return value of Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY.
	}
}

Follow the TODO instructions to implement exception handling. Submit your modified DividerApp.java file to Blackboard (in the "HW 7" assignment).