CIT 591 Notes on Pig assignment
Fall 2005, David Matuszek

I should have given you specifics on how to create the scanner variable. You could probably figure out most of it, but the constructor takes a parameter, System.in, which would be pretty hard to guess.

You should create only one Scanner object. There are two ways to do this. You can declare and define scanner (as an instance variable) in a single declaration:

Scanner scanner = new Scanner(System.in);

or you can declare scanner as an instance variable:

Scanner scanner;

and then define it in a constructor for Human:

scanner = new Scanner(System.in);


Some people are confused about what the askUser method returns.

    boolean askUser(String question) {
        char answer = ' ';
        while (answer != 'y' && answer != 'n') {
            System.out.print(question + "  ");
            String line = scanner.nextLine();
            answer = Character.toLowerCase(line.charAt(0));
        }
        return answer == 'y';
    }

It returns a boolean, just as it claims to in the header line. answer == 'y' is an equality test, using == to compare the value in answer with the literal value 'y'. The result of the comparison is true if the two are equal and false if they are not.

To use this method, you would say something like:

playAgain = human.askUser("Would you like to play again?");

(where playAgain and human have been properly declared and are the appropriate types).