import java.lang.Math; public class math_test { public static double inverse (double x) throws Exception { if (x != 0.0) { return 1.0 / x; } else { throw new Exception("Cannot compute inverse of 0.0!"); } } public static double squareroot (double x) throws Exception { if (x >= 0.0) { return Math.sqrt(x); } else { throw new Exception("Cannot compute square root of a negative number!"); } } public static double absolutevalue(double x) { // no need to throw an exception: all values of x are allowed if (x >= 0.0) { return x; } else { return -x; } } public static void main(String a[]) { // will throw an ArrayIndexOutOfBoundsException // int c[] = new int[10]; // c[200] = 1; try { System.out.println(inverse(5.0)); System.out.println(inverse(10.0)); // will throw an exception that we throw in inverse() // System.out.println(inverse(0.0)); System.out.println(inverse(3.0)); System.out.println(inverse(-1.0)); System.out.println(squareroot(16.0)); // will throw an exception that we throw in squareroot() //System.out.println(squareroot(-4.0)); System.out.println(squareroot(100.0)); // and this is just an example of composition: System.out.println(squareroot(inverse(0.25))); // println(squareroot(inverse(0.25))) // println(squareroot(4.0)) // println(2.0) -> prints out 2 // solved } catch (Exception e) { System.out.println("something went wrong" + e.getMessage()); } // can do this outside the try block! System.out.println(absolutevalue(10.0)); System.out.println(absolutevalue(-5.0)); } }