Chapter 4: 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. GCD and LCM
  2. Linear Function
  3. Polygons
  4. Chessboard


  1. GCD and LCM
    1. Implement a function gcd, which returns the greatest common divisor of two parameters.
      Feel free to reuse the previous solution from chapter 2, just wrapt it up in a function.
    2. Implement a function lcm, which returns the least common multiple of two parameters.

    3. Test your implementation.

    Just as an example: gcd(15, 21) = 3, lcm(15, 21) = 105.

    Back to top



  2. Linear Function
    1. Implement the linear function f(x) = 0.5*x + 1.
    2. Declare a composition Point, which stores x and y coordinates.
      Create a list of points and fill it with five points using the function f(x) with x ∈ [1, 2, 3, 4, 5].
    3. Implement a procedure drawLine(...) which accepts a list of points as a parameter and draws a line according to this list.
      Be aware, that you have to translate point coordinates into pixel coordinates before drawing.
      You can use the Turtle.moveTo(x, y) operation.

    Back to top



  3. Polygons
  4. Implement the procedures draw(...) and move(...) to get the following image:

    You can use the following operations:

    Back to top



  5. Chessboard
  6. Move parts of your previous chessboard implementation from chapter 3 into operations:

    Back to top