Chapter 5: 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. Swap Values with References
  2. Course Management


  1. Swap Values with References
  2. Your task is to:

    1. Declare two variables as integer references in the main procedure, assign two different values to these variables.
      Afterwards print the values of the variables.
    2. Implement a procedure swap which accepts two integer reference parameters.
      Swap the values of these parameters inside the operation.
    3. Invoke the swap procedure from the main procudre and pass the previously declare variables as parameters.
      Print the values of the variables again, they should now be swapped.

    The output of the program could look like this:

    Back to top



  3. Course Management
  4. Type Util

    Declare a compostion called Util.
    This composition should have a hidden integer attribute counter.
    Futhermore, it should have a visible operation generateNumber() which does not accept any parameters and returns an integer.
    Each time the operation is called, the value of counter should be incremented by 1 and then returned.

    Test your implementation of Util, it should look like this:

    Types Person and Docent

    Declare an abstract compostion called Person.
    A person has a hidden string attribute name, which should be still visible in its subtypes.
    Implement an operation getName() which returns the name of the person.
    This way we can not change the name of a person after its instantiation but have still reading access to it.

    Now create a non-abstract type Docent as a subtype of Person

    Test your implementation of Person and Docent, it should look like this:

    Type Student

    Declare a non-abstract compostion called Student which is also a subtype of Person.
    In addition to the inherited features, each student has a immatriculationNumber,
    which is a hidden integer attribute, but we should still have reading access to it similar to the attribute name.
    Use the Util operation generateNumber() to generate immatriculation numbers.

    Test your implementation of Student, it should look like this:

    Type Course

    Declare a non-abstract compostion called Course, all attributes should be hidden.

    Each course has following features:

    The test program:

    The output of the program could look like this:

    Back to top