Excercises for the course "An Introduction to Procedural and Object-oriented Programming (Object Rexx)"


For the following excercises there is no single correct solution, instead there are many alternative solutions possible. One of these is given.

An overview of this course can usually be found at starting out of: http://wwwi.wu-wien.ac.at/Studium/.

Rony G. Flatscher, As of: 2004-10-26


Excercises (Loops)

Excercise 1: Convert Celsius to Fahrenheit

Create a program which converts Celsius to Fahrenheit, starting with 0° up to and including 100° in steps of 5°.

Hint: The formula for converting Celsius to Fahrenheit is: Fahrenheit = Celsius*(9/5)+32

A possible solution:
solutions/solution_01.rex


Excercise 2: Convert Fahrenheit to Celsius

Create a program which converts Fahrenheit to Celsius, starting with 0° up to and including 100° in steps of 4°.

Hint: The formula for converting Celsius to Fahrenheit is: Celsius = (Fahrenheit-32)*(5/9).

A possible solution:
solutions/solution_02.rex


Excercises (Functions)

Excercise 3: Convert Celsius to Fahrenheit

Create a program which converts Celsius to Fahrenheit, starting with 0° up to and including 100° in steps of 5°. Define a function which expects as the single argument the centigrade value to be converted and returns the conversion's result as its function value.

Hint: The formula for converting Celsius to Fahrenheit is: Fahrenheit = Celsius*(9/5)+32

A possible solution:
solutions/solution_03.rex


Excercise 4: Convert Fahrenheit to Celsius

Create a program which converts Fahrenheit to Celsius, starting with 0° up to and including 100° in steps of 4°. Define a function which expects as the single argument the Fahrenheit value to be converted and returns the conversion's result as its function value.

Hint: The formula for converting Celsius to Fahrenheit is: Celsius = (Fahrenheit-32)*(5/9).

A possible solution:
solutions/solution_04.rex


Excercise 5: Convert Celsius to Fahrenheit (Supplying Argument at the Command Line)

Extend
excercise 3 such that it becomes possible for the user to supply the centigrade to be converted at the command line. If no argument is given, then a small conversion table like the one in excercise 1 should be displayed instead.

A possible solution:
solutions/solution_05.rex


Excercise 6: Convert Fahrenheit to Celsius (Supplying Argument at the Command Line)

Extend
excercise 4 such that it becomes possible for the user to supply the grade Fahrenheit to be converted at the command line. If no argument is given, then a small conversion table like the one in excercise 2 should be displayed instead.

A possible solution:
solutions/solution_06.rex


Excercise 7: Euro Calculator

Devise a program which is capable of converting between any Euro currencies.

E.g. the invocation euro Ats 65 Dem should convert 65 Austrian Schillings into Deutsche Mark. If no target currency is given, then the amount should be converted to all Euro currencies, including the Euro itself.

Hint: starting with 1999-01-01 all Euro currencies are set to a fixed exchange rate with respect to one Euro (in 2001-01-01 the Greek Drachmen joined the Euro currency system) as follows:

1 EUR 13,760300 ATS
1 EUR 40,339900 BEF
1 EUR 1,955830 DEM
1 EUR 5,945730 FIM
1 EUR 6,559570 FRF
1 EUR 0,787564 IEP
1 EUR 1.936,270000 ITL
1 EUR 40,339900 LUF
1 EUR 2,203710 NLG
1 EUR 200,482000 PTE
1 EUR 166,386000 ESP
1 EUR 340,750000 GRD

A possible solution:
solutions/solution_07.rex


Excercises (Routines)

Excercise 8: Convert Celsius to Fahrenheit

Change the function in
excercise 3 to a routine.

A possible solution:
solutions/solution_08.rex


Excercise 9: Convert Fahrenheit to Celsius

Change the function in
excercise 4 to a routine.

A possible solution:
solutions/solution_09.rex


Excercise 10: Convert Celsius to Fahrenheit (Supplying Argument at the Command Line)

Change the function in
excercise 5 to a routine.

A possible solution:
solutions/solution_10.rex


Excercise 11: Convert Fahrenheit to Celsius (Supplying Argument at the Command Line)

Change the function in
excercise 6 to a routine.

A possible solution:
solutions/solution_11.rex


Excercise 12: Euro Calculator

Change the program in excercise 7 to a routine.

In addition:

A possible solution:
solutions/solution_12.rex


Excercises (ADT)

Excercise 13: Define and Implement a "Euro" Abstract Datatype ("EURO" ADT)

Define an ADT EURO which has the following attributes and functions/procedures, examples relate to the Austrian currency "Schilling":

With the help of initialisation of new objects, the attributes denotation, exchangeRate, country and currency should get their values assigned to.

After creating such Euro objects one can use them to carry out the conversion calculations.

The following statements:

    /* arguments: "currency", "country", "denotation" and "exchangeRate" */
    o = .euro ~ NEW("ATS", "Austria", "Schilling", 13.7603)

    SAY o~denotation o~exchangeRate o~country o~currency
    SAY
    SAY "100" o~currency "are:" o~currency2euro(100) "EUR"
    SAY "100 EUR are:"          o~euro2currency(100) o~currency
yield the following output:
    Schilling 13.7603 Austria ATS

    100 ATS are: 7.26728342 EUR
    100 EUR are: 1376.0300 ATS

A possible solution:
solutions/solution_13.rex
solutions/solution_13.cls


Excercise 14: Define and Implement a "Euro" Abstract Datatype ("EURO" ADT)

Changes to
excercise 13:

With the help of initialisation of new objects, the attributes denotation, exchangeRate, country, currency and, optionally, amoutn should get their values assigned to.

After creating such Euro objects one can use them to carry out the conversion calculations, by merely setting the value of the attribute amount or euro_amount and afterwards getting the value of the other attribute.

The following statements:

    /* arguments: "currency", "country", "denotation" and "exchangeRate" */
    o = .euro ~ NEW("ATS", "Austria", "Schilling", 13.7603)

    SAY o~denotation o~exchangeRate o~country o~amount o~currency  o~euro_amount "EUR"
    SAY
    o~amount = 100       -- set currency amount
    SAY o~amount o~currency "are:" o~euro_amount "EUR"
    o~euro_amount = 100  -- set euro amount
    SAY o~euro_amount   "EUR are:" o~amount o~currency
yield the following output:
    Schilling 13.7603 Austria 0 ATS 0 EUR

    100 ATS are: 7.26728342 EUR
    100 EUR are: 1376.0300 ATS

A possible solution:
solutions/solution_14.rex
solutions/solution_14.cls


Excercises (Collection Classes)

Excercise 15: Fruit Bag # 1

For your hotel you are ordering the following fruits: apple, banana, pear and plum. Your supplier sometimes delivers you a bag which may contain fruits that you did not order! Therefore, you must always sort out the content of the fruit bag.

After sorting out the bag, you should be able to tell how many fruits there were in the bag, and how many pieces there are for each kind of fruit.

For our excercise the content of the bag should consists exactly of:

"apple", "apple", "pear", "strawberry", "apple", "banana", "plum", "plum", "banana", "apple", "pear", "melon", "peanut", "peanut", "peanut", "peanut", "peanut", "apple", "peanut", "pineapple", "banana", "plum", "pear", "pear", "plum", "plum", "banana", "apple", "pear", "melon", "peanut", "peanut", "peanut", "apple", "peanut", "pineapple", "banana", "peanut", "peanut", "peanut", "peanut", "peanut", "apple", "peanut", "pineapple", "banana", "peanut", "melon", "mango", "peanut", "peanut", "apple", "peanut", "pineapple", "banana", "pear"

According to this content the output should look like:

Total of fruits in bag: 56

consisting of:
                 plum:   5
           strawberry:   1
                 pear:   6
                melon:   3
               banana:   7
                mango:   1
               peanut:  20
            pineapple:   4
                apple:   9

Devise a program which solves this problem.

A possible solution:
solutions/solution_14.rex


Excercise 16: Fruit Bag # 2

Changes to
excercise 15:

According to the content as given in excercise 16 the output now should look like as follows:

Received Fruits (ordered)
                 plum
                 pear
                apple
               banana

Received Fruits (not ordered)
               peanut
           strawberry
                melon
                mango
            pineapple

Received Fruits (ordered)
                apple:   9
               banana:   7
                 pear:   6
                 plum:   5

Received Fruits (not ordered)
               peanut:  20
           strawberry:   1
                melon:   3
                mango:   1
            pineapple:   4

Devise a program which solves this problem.

A possible solution:
solutions/solution_16.rex


Excercise(s) (Advanced)

Exercise 17: Metaclass for Implementing the Pattern "Manager"

You have the task of creating a metaclass, which implements the following pattern "Manager":

An example program, which uses this needed metaclass:

   /* solution_17.rex */

   .test~~new("Anton") ~~new("Berta") ~~new("Caesar") -- create three instances

   SAY "Before synchroneous broadcast..."
   .test~broadcast( .message~new(.nil, "say_info"), .true )
   SAY "after synchroneous broadcast."
   SAY

   SAY "Before Asynchroneous broadcast..."
   .test~broadcast( .message~new(.nil, "say_info"))
   SAY "after asynchroneous broadcast."

   ::REQUIRES "solution_17.cls"

   /* --------------------------- Test-Class -------------------------------- */
   ::CLASS test            METACLASS Manager 

   /* --------------------------- Class Methods ----------------------------- */
   ::METHOD Init           CLASS 
     EXPOSE counter
     counter=0
     self~init:super

   ::METHOD count          CLASS 
     EXPOSE counter
     counter=counter+1
     RETURN counter

   /* --------------------------- Instance Methods -------------------------- */
   ::METHOD Init
     EXPOSE  name number
     USE ARG name
     number=self~class~count
     self~init:super

   ::METHOD name                 ATTRIBUTE

   ::METHOD say_info 
     EXPOSE number
     SAY " SAY_INFO - name:" self~name "created as #" number

A possible output:

   Before synchroneous broadcast...
   Manager.Broadcast: invoking messages synchroneously...
    SAY_INFO - name: Caesar created as # 3
    SAY_INFO - name: Berta created as # 2
    SAY_INFO - name: Anton created as # 1
   after synchroneous broadcast.

   Before Asynchroneous broadcast...
   Manager.Broadcast: invoking messages asynchroneously...
   after asynchroneous broadcast.

    SAY_INFO - name: Caesar created as # 3
    SAY_INFO - name: Berta created as # 2
    SAY_INFO - name: Anton created as # 1

Devise the needed metaclass.

A possible solution:
solutions/solution_14.rex
solutions/solution_14.cls


Hints for OODIALOG Rexx Programs

oodialog/info.htm contains a Windows Object Rexx program realizing the Euro converter using the graphical Windows application programming interfaces (API) as made available via the OODIALOG classes.