Tik-76.119 Ohjelmistometodiikka - tentti 27.5.1994

Tik-76.119 Programming methodology - examination 27 May 1994

Merkitse koepaperiin kurssin nimi, päivämäärä, koko nimesi, koulutusohjelma ja opintokirjan numero. [Write the following information on your paper.- The name of the course, date, your full name, your study programme and your study book number)

1/4. Selitä lyhyesti (korkeintaan viisi riviä) seuraavat käsitteet: [Explain briefly (at most five Iines) the following concepts:) (max 6 p.)

(a) Automaattinen muistinhallinta eli roskankeruu [Automatic memory management or garbage collectionl

(b) Abstrakti kantaluokka [Abstract base class]

(e) Nimikonflikti moniperinnässä [Name conflict in multiple inheritance)

(d) Funktiokutsujen dynaaminen sidonta [Dynamic binding of function calls)

(e) Tiedon piilottaminen [Information hiding)

(f) Funktion ylikuormitus [Function overloading)

2/4. Tässä tehtävässä vastaukset ovat huomattavasti lyhyempiä kuin kysymykset. [The answers to these questions are considerable shorter than the questions. ) (max 6 p.)

(a) C++-luokassa halutaan määritellä funktio, jota voidaan kutsua vain luokan jäsenfunktioista ja tästä luokasta johdettujen luokkien jäsenfunktioista. Millä tavalla funktio on määriteltävä? [You are defining a function in a C++ class and you want that the function can only be called from the memberfunctions of the class itself and from the memberfunctions of classes derived from this class. How do you define the function?)

(b) C++-luokkien A ja Bvälillä ei ole perintäsuhdetta. LuokanA jäsenfunktioista halutaan kuitenkin viitata luokan B yksityisiin jäseniin. Kuinka tämä saadaan aikaan? Kumpaa luokkaa(vai molempia) pitää muuttaa? [There is no inheritance between classes A and B. Nevertheless the member functions of class A should be able to acces the private members of classB. How can this be accomplished? Which class(es) must be modified?)

(c) SeuraavaC++-luokka esittää kokonaislukupinoa: [The followingC++ class represents an intege rstack.)

class IntStack

{

public:

IntStack();

void push (int);

int top();

...

};

Vastaavasti voidaan määtitellä liukulukupino: [A floating point stack can be defined in a similar way:)

class DoubleStack

{

public:

DoubleStack();

void push (double);

double top();

...

};

Määri ttele yleinen rakenne siten, että jokaista pinon alkiotyyppiä varten ei tarvitse erikseen määritellä luokkaa. (Huom! Tämä tarkoittaa, että perintään perustuva ratkaisu ei kelpaa.) [Define a general structure so that it is no longer necessary to define a separate class for each stack element type. Note that this means that you cannot use inheritance. )

(d) Ikkunointijärjestelmän ikkunoita esitetään luokalla Window. Jokaisella ikkunalla on isäikkuna,joka annetaan uutta ikkunaa luotaessa argumenttina ikkunaluokan konstruktorille. Ikkunahierarkian juurena on valmiiksi luotu juuri-ikkuna. Funktion window: : RootWindow halutaan palauttavan osoittimen tähän ikkunaan. Ongelmana on kuitenkin se, että Window-luokkaa käyttävän ohjelman luodessa ensimmäistä ikkunaa tälle ikkunalle pitäisi antaa isäksi RootWindow-funktion palauttama osoitin juuri-ikkunaan, mutta ohjelman alussa ei ole vielä luotu yhtään ikkunaolioita, jolle RootWindow-jäsenfunktiota voisi kutsua. Ratkaisusi? [A windowing system represents windows with the class window. Each window has a parent window, which is given as an argument to the constructor of the windowclass. The window hierarchy has a pre-defined root window. TheWindow::RootWindow function should retum a pointer to this window. There is, however a problem. When a program that uses the window class creates its first window, the pointer returned by the RootWi ndow function should be given as an argument to the constructor. There is, however no window object for which the RootWindow memberfunction could be called. Your solution?)

class Window

{

public:

Window (Window *aParent);

Window *Parent ();

// Window *RootWindow (); problem here

...

};

(e) C++-luokka IntStack sisältää mm.seuraavan unktion, jolla pinoon lisätään uusi alkio.

(Ylivuototarkistus puuttuu.) [The following C++ class defines a function which inserts an element at the top ofa stack. (The check for stack overflow is missing.)]

class IntStack

{

public:

void push (int i);

private:

int *data;

int topIndex;

}

void IntStack::push (int I)

{

opIndex++;

data[topIndexl = I;

}

Muuta luokkaa siten,e ttä alkion voil isätä seuraavasti: [Modify the class so that an element can be added to the stack in thefollowing way:l

IntStack s;

int I;

...

s << i;

(f) C++ -ohjelmaan suunnitellaan luokkahierarkiaa graafisten olioiden esittämiseen. Luokkahierarkian juurena on luokka shape, josta johdetaan luokkia eri kuvioita varten (esim. kolmioluokka). Luokassa Shape halutaan kertoa, että jokaiselle kuviolle on olemassa funktio area, joka palauttaa kuvion pinta-alan. Shape ei kuitenkaan pysty määrittelemään funktion toteutusta, vaan jokaisen shape-luokasta johdetun luokan on määriteltävä sopiva toteutus. Kirjoita Shape-luokkaan tuleva area-funktion esittely. [A C++ class hierarchy is being designed for the representation of geometrical objects. The hierarchy has the class Shape as its root, and classes for different shapes are derived from this class (e.g., a triangle class). The shape class should define that all shapes include a function area, which retums the area ofthe shape. The Shape class, however cannot implement the function. Each class derived from Shapemust therefore provide aproper implementation. Write the definition of the area function in the shape class.)

3/4. Piirrä oliomalli (hyvin paljon yksinkertaistetusta) kirjaston tietojärjestelmästä. Kaaviossa pitää esittää

seuraavat asiat: (Draw an object model for the following (very heavily simplified) libraryinformation system.)

(max 6 p.)

(Tämä sama tehtävä oli tentissä 31.1.1995. Tietojärjestelmän kuvaus löytyy ko.tentistä.

This same question was in exam on 31 Jan 1995. Information system is described there, in Finnish)

4/4. Piirrä tilakaavio seuraavassa kuvattavan sähköherätyskellon toiminnasta. Käytä sisäkkäisiä tilakaavioita. [Draw a state chart for the follwing electronic alarm clock. Use nested state charts.) (max6p.)

Aluksi kello on pois päältä, pariston paikalleen paneminen käynnistää kellon, pariston poistaminen

luonnollisesti pysäyttää kellon. [Initially the clock is off, the clock is automatically switched on when a battery is inserted)

Kun kello tulee päälle, se on normaalissa käyntitilassa. Oikean ajan asettamista varten painetaan "set time" nappia, jolloin kello siirtyy ajanasetustilaan. Tässä tilassa on erikseen alitilat tuntien, minuuttien ja sekuntien asettamista varten. Aluksi ollaan tuntien asetustilassa, tilojen välillä siirrytään nappien "hour", "min" ja "sec" avulla. (Kussakin tilassa muutetaan kyseistä arvoa (h, min, tai s) up- ja down-nappien avulla. Up- ja down nappien toimintaa ei kuitenkaan tarvitse kuvata kaaviossa.) [When the clock is switched on, it is in the normal running state. To set the correct time, you press the "set time " button, which makes the clock enter the time setting state. This state has separate substates for setting hours, minutes and seconds. Initially the clock is ready for setting hours, to move between states, use the "hour", "min " and "sec " buttons. (In each state, the corresponding value (h, min or s) is changed with the "up " and "down " buttons. However you need not model these buttons in our answer))

Ajan asettamisen jälkeen palataan "run"-napin painalluksella takaisin käyntitilaan. Herätysajan asetustilaan päästään vastaavalla tavalla "set alarm"-napin painalluksella. Ajanasetuksen ja herätysajan asetuksen välillä voi siirtyä suoraan ilman käyntitilaa. [After the correct time has been set, use the "run " button to return to the normal state. The alarm time is set similarly with the "set alarm " button. It is possibly to change between setting the correct time and the alarm time without pushing the "run" button.)