/* * P0Main.java * P1Main.java derived from P0Main.java * * Created on January 6, 2003, 9:22 PM */ package project1; /** * CSE143 03wi Project 1: "Which Oracle Do You Believe?". * Driver class. Your program will be tested with this, but you won't be * able to turn in a modified version. * [Derived from CSE143 03wi Project 0: "Does The God of Luck Know You?"]. * */ public class P1Main { /** Number of individual trials to run, per oracle. 3 is chosen as being a mystical number.*/ public static final int trialsToRun = 3; /** Default value in case an argument is not supplied on the command line. */ public static final String defaultrequest = "OF"; /** Driver program for the application; not to be changed! * This main should be invoked from the command line, with a single string argument. in Bluej, it can * be run directly and an argument string typed into the dialog box. */ public static void main (String[] args) { System.out.println("\nThis program will invoke various oracles " + "and determine how well the deities know and love you."); if (args.length != 1) { System.out.println("Next time, please supply exactly one argument " + "instead of " + args.length); args = new String[] {defaultrequest}; //dummy up the argument array System.out.println("Continuing with the default argument " + args[0]); } String yourrequest = args[0]; IOracle anOracle; anOracle = new LuckTester(); System.out.println("\n******** First Oracle: " + anOracle.getName() + " ***********"); invokeTheOracle(anOracle, yourrequest); anOracle = new LuckyOccurrenceCounter(); System.out.println("\n******** Second Oracle: " + anOracle.getName() + " ***********"); invokeTheOracle(anOracle, yourrequest); anOracle = new TextSearcher(); System.out.println("\n******** Third Oracle: " + anOracle.getName() + " ***********"); invokeTheOracle(anOracle, yourrequest); //end main } /** Invoke an oracle and report the results. * @param tester the oracle to be consulted; must not be null. * @param request a string to be examined by the oracle. */ private static void invokeTheOracle(IOracle tester, String request) { System.out.println("You may enter the Temple of " + tester.getName() + " with your question about " + request); boolean requestAreOK = tester.checkRequestData(request); if (!requestAreOK) { System.out.println("Violation! " + request + " is not a suitable request. You have insulted the deities."); return; } System.out.println("O great " + tester.getName() + ", we ask you " + trialsToRun + " times: what can you tell us about " + request + "?"); IOmen[] omenResults = new IOmen[trialsToRun]; int trialsSum = 0; //total number of invocations (trials) so far for (int t = 1; t <= trialsToRun; t++) { IOmen result = tester.searchEntrailsForOmen(request); //LuckTesterOmen result = (LuckTesterOmen) resultingOmen; if (result == null) { System.out.println("Trial #" + t + " produced no result."); } else { System.out.println("Trial #" + t + " produced a result" + " which is briefly interpreted as: " + result.interpretBriefly()); // original line, in error: [trialsToRun-1] = result; omenResults[t-1] = result; } } IOmen bestOmen = Utilities.chooseBestOmen(omenResults); if (bestOmen == null) { System.out.println("The oracle was displeased with thee. " + "Increase thy offering four-fold for 10 years, then try again."); } else { System.out.println("\nOf the " + trialsToRun + " invocations of " + tester.getName() + ", the best result was this:\n\t" + bestOmen.interpretInDetail()); } System.out.println("The session concerning " + request + " is over." + " Exit temple on the left."); //end invokeTheOracle } //end class }