Chapter 2: Excercises

This page contains a set of excercises for the corresponding chapter.
Read the task description, copy the code template if present and solve it in your Eclipse.
If you are stuck, you can show the solution for this task. Be careful not to spoil the solution for yourself unnecessarily.

Contents

  1. Absolute Value
  2. Factorial
  3. Greatest Common Divisor
  4. Number Guessing Game
  5. Regular Polygon
  6. Turtle Stair
  7. Caesar Encryption
  8. Colored Polygons
  9. Honeycomb


  1. Absolute Value
  2. Your first task is to compute the absolute value of a difference of two values using an if statement.
    Print the result on the console.
    You can subtract the smaller value from the bigger.
    Alternatively, you can compute a - b and negate the result if it is less than 0.

    Back to top



  3. Factorial
  4. Your task is to compute the factorial of an integer using a loop.
    5! = 5 * 4 * 3 * 2 * 1 = 120
    Print the result on the console.

    Back to top



  5. Greatest Common Divisor
  6. Your task is to compute the greates common divisor of two values.
    To do this, you can subtract the lesser value from the greater one until both values are equal.
    You need to overwrite old values with new ones, e.g. is a is lesser than b, you compute the new b as b - a.
    GCD of 21 and 14 is 7

    Back to top



  7. Number Guessing Game
  8. Your next task is to implement a simple number guessing game.
    The program must generate a random integer between 0 and 99, this can be done using the library operation Mathematics.randomInteger(0, 99).
    To use this operation, you have to import the Mathematics library first.

    The user has to enter a number.
    If the number equals the random number, the program is terminated and the user is given the number of attempts required to guess the number.
    If the guessed number is not equal, the program has to tell the user if it is greater or less than the random number.

    The entire interaction can look like this:

    Back to top



  9. Regular Polygon
  10. Your task is to draw a regular polygon using Turtle.
    As a reminder, in the first chapter we have drawn a Turtle square using the following code.

    As you can see, we repeating lines of code, which we can remove by using a loop.
    At first, optimize this example using a loop to draw a square.

    Once done, allow the user to enter the number of edges as well as their length to draw a generic regular polygon.
    You will have to dynamically compute the angle using the number of entered edges.
    For example, if the user enters 5 as the number of edges, the angle should be 72 degree, or 60 degree if the number of edges is 6.

    A polygon with the following parameters should look like in the image below.

    Back to top



  11. Turtle Stair
  12. Your task is to implement a stair pattern using Turtle and a loop.
    Start by drawing a single step of the stair.
    Once done, wrap it in into a loop to get the same result as in the image.

    Back to top



  13. Caesar Encryption
  14. Implement Caesar Encryption: https://en.wikipedia.org/wiki/Caesar_cipher.

    The output of the program should be: fdhvdu - caesar

    Back to top



  15. Colored Polygons
  16. Draw the image below, the colors of the polygon are randomized RGB values.

    Use Turtle.setColorRGB(Mathematics.randomInteger(0, 255), Mathematics.randomInteger(0, 255), Mathematics.randomInteger(0, 255)) to set random colors.

    Back to top



  17. Honeycomb
  18. Draw the image below.

    Use Turtle.startFilledPolygon(Turtle.Colors.YELLOW)) and Turtle.endFilledPolygon() to draw filled polygons.

    Back to top