guess_my_rule
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
guess_my_rule [2020/05/30 15:50] – frchris | guess_my_rule [2022/08/03 13:02] – [Guess My String Rule] frchris | ||
---|---|---|---|
Line 4: | Line 4: | ||
For example, The secret rule might be that it is an integer that is a prime number. The rule-maker Starts with "2 follows my rule, but 4 does not" | For example, The secret rule might be that it is an integer that is a prime number. The rule-maker Starts with "2 follows my rule, but 4 does not" | ||
- | In our computer version, the user is given clues, asking | + | In our computer version, the user asks about a certain '' |
+ | |||
+ | To be fair, the secret rule cannot involve any random behavior. | ||
==== Guess My Integer Rule==== | ==== Guess My Integer Rule==== | ||
+ | This is best used with the BlueJ IDE where you can make an instance of a class without writing a '' | ||
+ | <code java| GuessMyIntegerRule.java>/ | ||
+ | * Make up your own rule | ||
+ | | ||
+ | * @author Chris Thiel, OFMCap | ||
+ | * | ||
+ | */ | ||
+ | public class GuessMyIntegerRule | ||
+ | { | ||
+ | private String secretRule = "It must be prime"; | ||
+ | public boolean DoesThisIntFollowMyRule(int n) | ||
+ | { | ||
+ | for(int i = 2; i < n; i++) | ||
+ | if (n % i == 0) | ||
+ | return false; | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | public String IGiveUp() | ||
+ | { | ||
+ | return secretRule; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | Now Try this: | ||
+ | - Change the code so it the number must me even | ||
+ | - Change the code so it must be odd | ||
+ | - Make up your own rule and try it out with your lab partner | ||
==== Guess My String Rule ==== | ==== Guess My String Rule ==== | ||
+ | This is best using the Eclipse IDE | ||
+ | <code java| GuessMyStringRule.java> | ||
+ | import java.util.Scanner; | ||
+ | |||
+ | public class GuessMyStringRule { | ||
+ | private String secretRule, hint; | ||
+ | private String[] correctGuess; | ||
+ | |||
+ | public GuessMyStringRule() | ||
+ | { | ||
+ | secretRule = "The String needed to contain a lower case s"; | ||
+ | hint = "Ben follows the rule, but bend does not"; | ||
+ | |||
+ | } | ||
+ | public static void main( String[] args ) { | ||
+ | GuessMyStringRule rule = new GuessMyStringRule(); | ||
+ | Scanner kb = new Scanner(System.in); | ||
+ | System.out.println(" | ||
+ | int attempt = 10; | ||
+ | int correct = 0; | ||
+ | String[] correctGuess = new String[10]; | ||
+ | System.out.println( rule.getHint() ); | ||
+ | while ( attempt > 0 ) | ||
+ | { | ||
+ | System.out.print (correct + " correct, " + attempt + " attempts left\n" | ||
+ | String guess = kb.nextLine(); | ||
+ | |||
+ | if (rule.isCorrect(guess)) | ||
+ | { | ||
+ | correct++; | ||
+ | for (String g: | ||
+ | if (guess.equals(g)) | ||
+ | correct--; | ||
+ | correctGuess[10-attempt] = guess; | ||
+ | System.out.print(" | ||
+ | } else { | ||
+ | System.out.print(" | ||
+ | } | ||
+ | attempt--; | ||
+ | } | ||
+ | System.out.println(" | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | private String getHint() { | ||
+ | return hint; | ||
+ | } | ||
+ | private String getSecretRule() { | ||
+ | return secretRule; | ||
+ | } | ||
+ | public boolean isCorrect(String guess) { | ||
+ | if (guess.indexOf(' | ||
+ | return false; | ||
+ | |||
+ | return true; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ |
guess_my_rule.txt · Last modified: 2022/08/11 10:09 by frchris