1.1-Introductie-Programmeren

Problem Solving

Competence: I can translate a basic problem into Java code

Specific learning goals:

The exercises can be found in the different sub-modules.

Summary

Big problems can be solved by splitting them up into smaller problems. The problem “Create a number guessing game” is too big to implement at once. First you need to think of the smaller steps that are necessary. You can do that just in plain English (or your native tounge). It starts with good reading (or watching the video) about the problem that you must solve. Then write it in your own words and try to split it in smaller steps again and again. Until you reach steps that you can easily implement in Java (like in one, two or a few lines of code).

Explanation

Problem Solving is, quite obviously, about solving problems. And as a programmer we’d like to solve problems with code. In this case Java code. Programming is about two things:

  1. Splitting a (bigger) problem into smaller steps (called decomposition)
  2. Translating that steps into Java code.

It is good practice to consider these both steps seperately when you work on a program.

Step 1: Splitting a (bigger) problem into smaller steps

Most people find this the hardest part of programming. Because it urges you to look very closely to the problem and to think in really small steps. This requires practice and I’ll try to help you with that in this paragraph. A good and small enough step is so small that you can program it very easily (like printing a line, or writing an if statement, and so on…).

1. Describe the problem accurately in your own words.

Let’s assume that we want to create a number guessing game, like you did before. The first thing to do is to describe it as accurate a possible in a language you like (we use English here, but might be your own language). For example, I came up with

– I want that the user must guess a number between 1 and 100 and that the program says if the correct number is higher or lower than the guess. The program must run until the user has guessed the number. –

2. Identify parts you already know

After that it can be helpfull to identify parts that you recognize as something you did before, or what is very easy in Java. For example might know how to “pick a number between 1 and 100”, you might know how to check if a number is higher than another number and so on. You might also come up with the idea that until most of the time means that a while loop is involved. The last thing that you might be able to recognize is that an user input is required. You would then come up with that SaxionApp.read...() is necessary.

3. Write down small steps in plain English (or you own language)

Now that you have done a small analyses on the problem. You can go on by trying to write it down step by step. I recommend you to do that in your Java program already as code comment. Like the following:

public class Application implements Runnable {

    public static void main(String[] args) {
        SaxionApp.start(new Application(), 1024, 768);
    }

    public void run() {
        //Pick a random number

        //Ask the user for a number

        //Check if the number is bigger or smaller and print that to the user

        //When the user guesses the correct number, end the game
    }
}

4. Reflect on your steps

Ok. Well done! Maybe now you find out that there must be some loop in the program, because we want ask until the user guess the number. So, we can also add a bit more details and some nesting to our pseudo code.

So, let’s add some nesting and a bit more detail:

public class Application implements Runnable {

    public static void main(String[] args) {
        SaxionApp.start(new Application(), 1024, 768);
    }

    public void run() {
        //Pick a random number between 1 and 100

        //As long as the number is not guessed...
            //Print "Type a number" and ask the user for a number

            //Check if the number is bigger or smaller and print that to the user

            //When the user guesses the correct number, Congratulate the user and end the game

    }
}

You can see that this looks a bit more like a real Java program. The idea is that you do this as long as possible, until you got a list of steps where you know for each step how you should implement it in Java. I think the above steps are good enough, because I am quite sure you know to pick a number, how to translate as long as the number not is guessed to a while (I think the best option is create a boolean named numberGuessed and put that default to false. Then add while (!numberGuessed) {. And so on.

Step 2: Translate the steps into Java code

As soon as you have defined steps that you think are small enough that you can implement them in Java. Just add the code below the comments you have added.

It’s good practice to run the program plenty of times. Everytime you add a small piece of functionality test that small part. Sometimes you might find out that it’s easier to first implement a bit of code that’s later in the program (For example, in this small program you can choose to first create the comparison between numbers and test it with hardcoded integers).

Another thing is that you might find out that your step needs a bit more code than you expected. Than it might be helpfull to create a method for that part of functionality (read the methods documentation for details). The nice thing with methods is that you can always test them separately by calling them from your run method.

If you’re stuck, try to go back to the last time that the program actually worked (control-Z is your friend).

Good luck! Below is the implementation of the number guessing game described above. I know this is quite a small problem, but bigger problems can be solved in the same way.

public class Application implements Runnable {

    public static void main(String[] args) {
        SaxionApp.start(new Application(), 1024, 768);
    }

    public void run() {
        //Pick a random number between 1 and 100
        int number = SaxionApp.getRandomValueBetween(1, 101);

        //As long as the number is not guessed...
        boolean numberGuessed = false;
        while (!numberGuessed) {
            //Print "Type a number" and ask the user for a number
            SaxionApp.printLine("Type a number: ");
            int userInput = SaxionApp.readInt();

            //Check if the number is bigger or smaller and print that to the user
            if (userInput < number) {
                SaxionApp.printLine("Too low. Try again!");
            } else if (userInput > number) {
                SaxionApp.printLine("Too high. Try again!");
            } else {
                //When the user guesses the correct number, Congratulate the user and end the game
                //(Please note: When it's not higher, and not lower than it is the correct number
                SaxionApp.printLine("Congratulations");
                numberGuessed = true;
            }

        }
        
    }
}