Object-Oriented Programming with Java, part I – Exercise 105

This exercise implements a number guessing game were the user guesses a number between a lower and upper limit.

My Code

Main

public class Main {

    public static void main(String[] args) {
        // test your program here

        GuessingGame game = new GuessingGame();
        game.play(0, 100);
    }
}

GuessingGame


import java.util.Scanner;

public class GuessingGame {

    private Scanner reader;

    public GuessingGame() {
        // use only this scanner, othervise the tests do not work
        this.reader = new Scanner(System.in);
    }

    public void play(int lowerLimit, int upperLimit) {
        instructions(lowerLimit, upperLimit);

        // write the guessing logic here
        boolean loop = true;
        while (loop) {
            if (isGreaterThan(average(lowerLimit, upperLimit))) {
                lowerLimit = average(lowerLimit, upperLimit) +1;
            } else {
                upperLimit = average(lowerLimit, upperLimit);
            }
            if (lowerLimit == upperLimit) {
                loop = false;
            }
        }
        System.out.println("The number you're thinking of is " + upperLimit + ".");

    }

    // implement here the methods isGreaterThan and average
    public void instructions(int lowerLimit, int upperLimit) {
        int maxQuestions = howManyTimesHalvable(upperLimit - lowerLimit);

        System.out.println("Think of a number between " + lowerLimit + "..." + upperLimit + ".");

        System.out.println("I promise you that I can guess the number you are thinking with " + maxQuestions + " questions.");
        System.out.println("");
        System.out.println("Next I'll present you a series of questions. Answer them honestly.");
        System.out.println("");
    }

    // a helper method:
    public static int howManyTimesHalvable(int number) {
        // we create a base two logarithm  of the given value

        // Below we swap the base number to base two logarithms!
        return (int) (Math.log(number) / Math.log(2)) + 1;
    }

    public boolean isGreaterThan(int value) {
        System.out.println("Is your number greater than " + value + "? (y/n)");
        String answer = reader.nextLine();
        if (answer.contentEquals("y")) {
            return true;
        } else {
            return false;
        }
    }

    public int average(int firstNumber, int secondNumber) {
        return (firstNumber + secondNumber) / 2;
    }

}

Model Code

Main

public class Main {

    public static void main(String[] args) {
        // test your program here

        GuessingGame game = new GuessingGame();
        game.play(0, 100);
    }
}

GuessingGame

import java.util.Scanner;

 

public class GuessingGame {

 

    private Scanner reader;

 

    public GuessingGame() {

        // use only this scanner, othervise the tests do not work

        this.reader = new Scanner(System.in);

    }

 

    public void play(int lowerLimit, int upperLimit) {

        instructions(lowerLimit, upperLimit);

 

        // write the guessing logic here

        while (lowerLimit < upperLimit) {

            int middle = average(lowerLimit, upperLimit);

            if (isGreaterThan(middle)) {

                lowerLimit = middle + 1;

            } else {

                upperLimit = middle;

            }

        }

        System.out.println("The number you're thinking of is " + lowerLimit);

 

    }

 

    // implement here the methods isGreaterThan and average

    public boolean isGreaterThan(int luku) {

        System.out.println("Is your number greater than " + luku + "? (y/n)");

        String answer = this.reader.nextLine();

 

        return answer.equals("y");

    }

 

    public int average(int first, int second) {

        return (first + second) / 2;

    }

 

    public void instructions(int lowerLimit, int upperLimit) {

        int maxQuestions = howManyTimesHalvable(upperLimit - lowerLimit);

 

        System.out.println("Think of a number between " + lowerLimit + "..." + upperLimit + ".");

 

        System.out.println("I promise you that I can guess the number you are thinking with " + maxQuestions + " questions.");

        System.out.println("");

        System.out.println("Next I'll present you a series of questions. Answer them honestly.");

        System.out.println("");

    }

 

    // a helper method:

    public static int howManyTimesHalvable(int number) {

        // we create a base two logarithm  of the given value

 

        // Below we swap the base number to base two logarithms!

        return (int) (Math.log(number) / Math.log(2)) + 1;

    }

}

 

Comments

I think their solution is better but only because of it readability, not it’s functionality. Again I have an added variable to the model code.

Leave a comment