// // test program for Request class and subclasses: build an assortment // of objects and, for each, (1) invoke its serviceRequest() // method and (2) print the result. // class TestRequest { // // ---- main --------------------------------------------------- // // no command-line arguments. // public static void main(String[] args) { Request[] reqs = new Request[] { new ShowLocalTime(), new FindRealRoots(1.0, 0.0, 1.0), new FindRealRoots(1.0, -2.0, 1.0), new FindRealRoots(1.0, 0.0, -1.0), new ShowLocalTime() } ; for (int i = 0 ; i < reqs.length ; i++) { System.out.println("request: " + reqs[i]) ; Object o = reqs[i].serviceRequest() ; if (reqs[i] instanceof ShowLocalTime) System.out.println("result: " + o) ; else if (reqs[i] instanceof FindRealRoots) { double[] roots = (double[]) o ; if (roots.length == 0) System.out.println("result: " + "no real roots") ; else if (roots.length == 1) System.out.println("result: " + "repeated root " + roots[0]) ; else System.out.println("result: " + "roots " + roots[0] + ", " + roots[1]) ; } } } }