Chapter 3: 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. Shopping List
  2. Filter List
  3. Sort List
  4. Chessboard


  1. Shopping List
  2. This task is the expansion of the shopping list example from chapter 3.

    1. Declare a composition Item which can store the name of the item and its price.
    2. Initialize three items: item1{bread, 1.99}, item2{beer, 14.99}, item3{ham, 3.99}.
      Put all of these items into a single list and print the list on the console.
    3. Iterate over the list and compute the total price.
    4. Create a cheaperList, iterate over the normal list and append items with the price of under 10.0 to the cheaper list.

    The output of the program should look like this:

    Back to top



  3. Filter List
  4. Your are given a list with 10 random generated numbers between 0 and 10.
    Your task is to filter odd numbers out of this list and print the filtered list.

    The result could look like that:

    Hint: you can use removePosition to remove odd numbers or append even numbers to a temporary list.

    Back to top



  5. Sort List
  6. Your are given a list with 10 random generated numbers between 0 and 10.
    Your task is tothis list in ascending order and print the result.

    The result could look like that:

    Hint: you can compare pairs of neighbouring values and swap them if necessary (google: bubblesort).
    Alternatively, you can remove the smallest number from the original list and append it to a temporary list until the original list is empty.

    Back to top



  7. Chessboard
  8. This task is the expansion of the chessboard example from chapter 3.

    1. Declare an enueration PieceType with value literals: PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING.
    2. Declare an enueration Color with value literals: BLACK, WHITE.
    3. Declare a composition Piece which has a pieceType and a color.
    4. Declare a composition Field which has a color, a number, a character and a piece.
    5. In the main procedure, declare a chessboard as a two dimensional list of Fields.
      Initialize it as [8 ** [8 ** Field{}]].
    6. Iterate of this list, set the corresponding number and character coordinates as well as the correct color of each field.
    7. Print your chessboard on the console.

    The resulting output should look like in the image below.
    As you can see, a pawn is placed on every field, since it is a default value in this case.
    We will solve this issue in chapter 5.

    Back to top