Monday, September 9, 2013

JAVA Case Study #3: Magic Cue Ball

For this case study, we will need to have a random number generator -- simply put, a way to randomly pick a "No" or a "Yes" (a 0 or a 1).  Here's a sample code which generates a random number:
import java.util.Random;
class RandomNumber {
    public static void main(String args[]) {
        Random myRandom = new Random();
        int randomNumber;
        randomNumber = myRandom.nextInt(10) + 1;
        System.out.println(randomNumber);
       }

CASE STUDY #3:  Magic Cue Ball

For case number 3 we have "The Magic CueBall" -- okay, so it's ripped off... but it works... sort of:
import java.util.Scanner;
import java.util.Random;

class MagicCueBall {

    public static void main(String args[]) {
        Scanner myScanner = new Scanner(System.in);
        Random myRandom = new Random();
        int randomNumber;
        System.out.print("Type a Yes or No question: ");
        myScanner.nextLine();
        randomNumber = myRandom.nextInt(10) + 1;
        if (randomNumber > 5) {
            System.out.println("Yes. Isn’t it obvious?");
        } else {
            System.out.println("No, and don’t ask again.");
        }
    }
}
 
Let's do some playing with the code again:
  • Again, begin by adding comments/labels to the code.
  • What Java utils are we bringing in this time?
  • Before the "if" statement, randomNumber can be what possible numbers?  Why is there a +1 at the end of the line?
  • Add some variation to the possible responses:
    • 4 different "Yes" answers
    • 4 different "No" answers
    • 2 different "Ask Again" answers
  •  At the beginning of the program (before asking the user to type a yes or no question), add a title text graphic.  For example:
    • *********************************
    • * Magic Cue Ball 1.0 ~ by Your Name  *
    • *********************************
  •  What else could we add to make this application more usable?

No comments:

Post a Comment