User Tools

Site Tools


guess_my_rule

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
guess_my_rule [2020/05/30 15:55] frchrisguess_my_rule [2022/08/11 10:09] (current) frchris
Line 10: Line 10:
  
 ==== 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 ''main'' method.
 +<code java| GuessMyIntegerRule.java>/**
 + * Make up your own rule
 + 
 + * @author Chris Thiel, OFMCap
 + *
 + */
 +public class GuessMyIntegerRule 
 +{
 + //  Change secretRule to make your own rule
 +        private String secretRule = "It must be prime";
 +        
 + public boolean DoesThisIntFollowMyRule(int n) 
 + {
 + // change this code for checking if n follows your rule.
 + for(int i = 2; i < n; i++)
 + if (n % i == 0)
 + return false;
 + return true;
 + }
 +
 + public String IGiveUp()
 + {
 + return secretRule;
 + }
 +}
 +</code>
 +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>
 +/**
 + * Make up your own rule
 + 
 + * @author Chris Thiel, OFMCap
 + *
 + */
 +import java.util.Scanner;
  
 +public class GuessMyStringRule {
 + private String secretRule, hint;
 + private String[] correctGuess;
 +
 + public GuessMyStringRule()
 + {
 + // Change the following to make your own rule and hint:
 +
 +                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("Guess My Rule\nType a string and I will repond with true or false");
 + 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" + (11-attempt) + ": ");
 + String guess = kb.nextLine();
 +
 + if (rule.isCorrect(guess))
 + {
 + correct++;
 + for (String g:correctGuess)
 + if (guess.equals(g))
 + correct--;
 + correctGuess[10-attempt] = guess;
 + System.out.print("TRUE! ");
 + } else {
 + System.out.print("False. ");
 + }
 + attempt--;
 + }
 + System.out.println("Did you get the idea? The rule was " + rule.getSecretRule() );
 +
 +
 +
 + }
 + private String getHint() {
 + return hint;
 + }
 + private String getSecretRule() {
 + return secretRule;
 + }
 +        /**
 +        /* change the following to determine if your rule is followed
 +        */
 + public boolean isCorrect(String guess) {
 + if (guess.indexOf('s') < 0)
 + return false;
 +
 + return true;
 + }
 +
 +
 +}
 +</code>
 +====Rubric====
 +  - Name and seat in a comment?
 +  - Unique secret rule
 +  - Hint provided
 +  - Attempt to test if input follows the rule
 +  - Functions according to spec
 +  - Follows best practices (names of variables, indenting, etc)
guess_my_rule.1590868531.txt.gz · Last modified: 2020/05/30 15:55 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki