/* * LuckTester.java * * Created on January 7, 2003, 2:51 PM * * Modify this file, and turn in it. Do not change the package name, class name, * or any method signatures. */ package project0; //don't change /** Does the God/Godess of Luck know you? A class to help answer this urgent * question. * * @author dickey 143-03wi */ public class LuckTester { /** Creates a new instance of LuckTester */ public LuckTester() { } /** Check that a pair of initials is suitable for presentation to the * God/Goddess of Luck. * @param initials A person's initials: must be exactly two uppercase English letters. * @return true iff both initials are valid (thus avoiding the wrath of the gods). * */ public boolean checkInitials(String initials) { if (initials.length() != 2) { return false; } char firstI = initials.charAt(0); char secondI = initials.charAt(1); if (CharGenerator.isEnglishUpperCase(firstI) && CharGenerator.isEnglishUpperCase(secondI)) { return true; } return false; //end checkInitialsArg } /** Given a person's initials, how long does it take the God/Goddess of luck * to recognize that person? Algorithm: Generate pairs of characters randomly until the desired initials are observed. * To avoid infinite loops, at most 1,000,000 pairs are tested. * @param initials A string of exactly two uppercase alphabetic characters; invalid initials risk inciting the wrath of the gods. * @return the number of random pairs which were generated before the initials * were generated. The minimum possible value is 0, which would mean * the the very first pair generated was the pair requested. */ public int tryUntilSuccess(String initials) { assert initials.length() == 2; char char1 = initials.charAt(0); char char2 = initials.charAt(1); int tryCount = 0; final int maxTrials = 1000000; while (tryCount < maxTrials) {//remain in the loop until a break is executed char c1 = CharGenerator.randomUpperChar(); char c2 = CharGenerator.randomUpperChar(); if (c1 == initials.charAt(0) && c2 == initials.charAt(1)) { break; //have found the pair! } tryCount++; } return tryCount; //end tryUntilSuccess } /** Report on how lucky a result is. * The cutoff points for various messages are somewhat based on estimations * of how probable various result are, given that the size of the alphabet is 26. @param score a value representing the number of trials needed before the god or * goddess recognized someone's initials; these values are on the same * scale as those produced by the tryUntilSuccess method. * @return an English message explaining or evaluating the result. For example, * it could be "You are truly beloved of Fortuna" or "You need to increase * your offerings or disaster will befall", as appropriate. */ String interpret(double score) { final double aSize = 26.0; if (score <= (aSize/4) * (aSize/4)) { return "Incredible Luck! You are truly beloved of the gods."; } if (score < (aSize/2) * (aSize/2)) { return "The gods smile upon you."; } if (score <= aSize * (aSize/2)) { return "The gods of luck are just a tiny bit displeased."; } if (score <= aSize * aSize) { return "Caution -- luck does not favor you."; } else { return "EXTREME DANGER -- the gods are wrathful unto thee."; } //end interpret } //end class LuckTester }