1 / 129

OOMPA Lecture 14: Exam Solutions & Together Demo

Introduction to C++ and Together, a model-building and UML diagram tool. Covers UML diagrams, synchronization with source code, GoF patterns. Available free at http://www.togethercommunity.com/demo.

herbertp
Download Presentation

OOMPA Lecture 14: Exam Solutions & Together Demo

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OOMPA Lecture 14 • Exam solutions • Together demo • Introduction to C++

  2. Together • Together ( http://www.togethersoft.com ) • Model-build-deploy tool • Creation, editing of UML diagrams • Synchronization between UML diagrams and source code • GoF patterns • JAVA, C++, C## • Free limited version available at http://www.togethercommunity.com/ Demo ..\..\..\..\Together5.5\bin\Together.exe

  3. Exam • Master solution available on course webpage • Evaluation scheme will be available • Exam results by November 2nd, 2001 • Grading • 22 out of 38 betyg 3 • 26 out of 38 betyg 4 • 30 out of 38 betyg 5

  4. Question 1 • Question 1: (1) Abstract methods have • No program code in the subclass FALSE • Program code in the defining class FALSE • Program code in the subclasses CORRECT • No program code in the defining class CORRECT • (b) and (c) • (c) and (d) • (a) and (b) Correct answer : f

  5. Question 2 • Question 2: (1) An association is • a.A straight line in a class diagram FALSE • b.Represented by a noun that describes the interaction FALSE • c.A relationship between instances of classes CORRECT • d.A relationship between classes FALSE • e.(a) and (d) • f.(b) and (c) • Correct answer: c

  6. Question 3 • Question 3: (1) Aggregation • a.Means an object is built of other objects FALSE • b.Means an object contains other objects CORRECT • c.Is always of multiplicity 1-* FALSE • d.Is “is a kind of” relationship FALSE • e.(b) and (c) • f.(a) and (d) • Correct answer: b

  7. Question 4 • Question 4: (1) Which of the following statements are true for statechart diagrams • a.An event is caused by a transition. FALSE • b.An object has to be in exactly one state at a time. CORRECT • c.A state is a condition needed for a transition to occur. FALSE • d.In a transition one or more attributes change their value. MAYBE • e.(a) and (c) • f.(b) and (d) • Correct answer: f , but b also counts as a correct answer since changing state does not necessarily means that the object changes its attributes, but for example it could simply change the control flow.

  8. Question 5 • Question 5: (1) Generalization is • a.building objects from parts FALSE • b.a “has a”-relationship FALSE • c.a “is a kind-of” relationship CORRECT • d.realized by means of inheritance CORRECT • e.realized by means of composition FALSE • f.(a) and (b) • g.(c) and (d) • h.(a) and (e) • i.(a), (b) and (e) • Correct answer: g

  9. Question 6 • Question 6: (1) Polymorphism • a.requires abstract classes FALSE • b.is realized by means of static binding FALSE • c.is realized by means of dynamic binding CORRECT • d.enables subclasses to override the method of a superclass CORRECT • e.(a),(c) and (d) • f.(a) and (c) • g.(c) and (d) • h.(b) and (d) • Correct answer: g

  10. Question 7 • Question 7: (4) Which of the following phrases best describe the design patterns in the list. Match 10 out of 14 phrases with the 10 concepts below. • a.Proxy- Provide a placeholder for another object to control access to it. • b.Adapter- Convert the interface of a class into another interface clients expect. • c.Low Coupling-Supports low dependency and low impact of change. • d.Composite - Lets clients treat individual and group of objects uniformly. • e.Factory Method -Lets a class defer instantiation to subclasses. • f.Abstract Factory -Provide an interface for creating families of related objects without specifying their concrete classes. • g.   Singleton - Provides global access to a unique instance

  11. Question 7 • h.Observer- When one object changes its state the state of dependant objects is synchronized. • i.Expert -A class that has the information necessary to fulfill a responsibility. • j.Pure Fabrication -Assign responsibilities to an artificial class to support high cohesion and low coupling. • k.Controller -An object responsible for receiving or handling system events. • l.Strategy -Define a family of algorithms and make them interchangeable. • m. Façade - Provide a unified interface to a set of interfaces in a subsystem. • n.   Mediator -An intermediate object that decouples other objects.

  12. Question 8 • Question 8: (2) Describe in two sentences what is the difference between object oriented analysis and design. • OO analysis is concerned with • finding and describing objects or concepts in the problem domain. • what needs to be done. • defining the problem/requirements (do the right thing) • OO design is concerned with • defining software objects and how they collaborate • how is it done • conceiving a solution to the problem (do the thing right)

  13. Question 9 • Question 9: (2) What is (usually) the multiplicity (1..*, *..*, etc.) of the following associations? • a.Brother is sibling of sister *-* • b.Child is a descendant of parent 1..*-2 • c.Person is married with person 0,1-0,1 • d.Twin is sibling of twin 1-1 • e.Student attends course *-* or *-1..* • f.Flight flies to airport *-1 or *-* • g.Order contains items 1-1..* • h.Customer purchases product *-* or 0,1-*

  14. Question 10 • Question 10: (2) Describe in two sentences the roles of inception and elaboration within the unified process. • Answer: Inception is a feasibility study which envisions the product scope, approximate vision and business case. Elaboration generates a refined vision, iterative implementation of core architecture, resolution of high risks, identification of most requirements and scope. (see Larman pages 35 and 109).

  15. Question 11 • Question 11: (1) In the unified process requirements are described by • a.Design model FALSE • b.Domain model FALSE • c.Use case model CORRECT • d.(a) and (b) • e.(a) and (c) • f.(b) and (c) • Correct answer: c

  16. Question 12 • Question 12: (1) Extreme Programming advocates • a.continuous integration CORRECT • b.a sequential, linear lifecycle FALSE • c.Refactoring CORRECT • d.(a) and (b) • e.(a) and (c) • f.(b) and (c) • g.(a), (b) and (c) • Correct answer: e

  17. Question 13 • Question 13: (8) A vending machine offers two different products A and B. Product A costs 4 SEK, product B 6 SEK. The customer inserts one coin at a time into the slot. The machine accepts coins in denominations of 1, 5 or 10 SEK. The machine displays the amount available for purchasing products. The machine has three buttons, button A to dispense product A, button B to dispense product B and button C to obtain change. At any time the customer can press button C to collect her change or amount paid, even if no purchase occurred previously. The customer can press buttons A and B at any time, but the machine only dispenses the corresponding product if the current amount paid is equal to or exceeds the product price. It is possible to make multiple purchases with one payment by using the remaining amount for another purchase (for example inserting a 10 SEK coin and purchasing product A twice and obtain 2 SEK change or for example inserting a 5 SEK coin, purchasing product A, inserting another 5 SEK coin and purchasing product B). • a.Draw a state diagram for the vending machine. • b.Draw a system sequence diagram for a scenario in which the customer inserts two 5 SEK coins, purchases product B and collects her change. Illustrate the information or product the vending machine shows or dispenses to the customer.

  18. Question 13 State-Diagram • States : Idle, Coins Inserted, Product A dispensable, Product A & B dispensable • Events : insert coin, button A, button B, return button • Actions : dispense A, dispense B, return change

  19. Question 13

  20. Question 13

  21. Question 13

  22. Question 14 • Question 14: (4) Draw the collaboration diagram that corresponds to the following sequence diagram

  23. Question 14

  24. Question 15  Question 15: (8) Draw a domain model as a UML class diagram, containing concepts, generalizations, named associations with multiplicity (composition and aggregation where applicable) and attributes for the following domain. The task is to design a software that helps you keep track of your collection of audio CDs. One distinguishes between two different types of releases, namely maxi-CDs and album CDs. Each release (album or maxi-CD) contains one or several recordings, a recording is a particular song performed by a particular artist, for example the album “Hot Rocks” by the Rolling Stones contains recordings of the songs “Time is on my side”, “Heart of Stone”, “Play with Fire” etc. Each recording has a playing time, for example “Time is on my side” has a playing time of 5:23. The same recording might occur on different releases, for example the original studio album and a “best-of” album..

  25. Question 15 The same or a different artist might perform the same song in different recordings, for example the song “American Pie” by Don Mc Lean and Madonna or “Sympathy for the Devil” in a live and a studio version by the Rolling Stones. One distinguishes between solo artists (e.g. Madonna) and bands composed of artists, for example Paul McCartney, John Lennon, Ringo Starr and George Harrison are members of the Beatles. An artist can occur on different recordings in different roles, either as a solo artist or a band member, for example Sting as a solo artist or as a member of the band Police. A song has a title and is composed by one or several composers, for example the song “Hey Jude” is composed by Paul Mc Cartney and John Lennon. Each release has a title, release date, music label and a playing time that is calculated as the accumulated playing time of the recordings on the release. A release is usually associated with one particular solo artist or band, for example the album “Abbey Road” with the Beatles. Still, this is not always the case for example in case of movie sound tracks or events such as Woodstock, which feature a number of different artists on the same release

  26. Question 15 Concepts: composer, song, recording, release, artist, solo artist, band, album, maxi-CD

  27. Differences between JAVA and C++ • C++ uses pointers instead or in addition to JAVA references • C++ also uses references but in a different way than JAVA • C++ has no garbage collection, which means that objects created with new have to be destroyed with delete in order to avoid memory leaks • JAVA is an interpreter language (virtual machine), portable across platforms (byte code is somehow between assembler code and source code) • C++ programs need to be compiled separately for each target platform.

  28. Additonal Features of C++ • Pointers • Operator overloading • Template methods and classes • Multiple inheritance • Standard Template Library (container classes)

  29. Hello World #include <iostream> // input-output library int main() // function main { cout << ”Hello World” << endl; // standard output stream }

  30. Output Using Cout • The identifier cout is actually an object. It is predefined • in C++ and corresponds to the standard output stream. • A stream is an abstraction that refers to a flow of data. • The operator << is called the insertion or put operator • and can be cascaded (like arithmetic operators +,-,*,/) • It directs the contents of the variable to the right to • the object to the left. string str=”Hello world”; int i=8; cout << str << endl; // endl inserts a new line cout << ”i=” << i << endl; // cascade operator << variable/ constant standard output device cout <<

  31. Input Using Cin • The object cin is predefined in C++ and corresponds • to the standard input stream. • The >> operator is called the extraction or get operator • and takes the value from the stream object to the left • and places it in the variable on its right. • The stream represents data coming from the keyboard int temperature; cout << ”Enter temperature in Celsius: ”; cin >> temperature; standard input device cin variable >>

  32. Library Functions • Many activities in C/C++ are carried out by library functions. • These functions perform file access, data conversion and • mathematical computations. • #include <math> // includes the declaration file for • // mathematical functions • int main() • { • double x=3.14; • cout << ”sin(3.14)= ” << sin(x) << endl; • }

  33. Header Files • a header file contains the declaration of functions • you want to use in your code • the preprocessor directive #include takes care of • incorporating a header file into your source file • example: • #include <math> • #include ”myprog.h” • the brackets <> indicate that the compiler first searches • the standard include directory which contains the • standard C/C++ header files first • the quotation marks indicate that the compiler first searches • for header files in the local directory • if you do not include the appropriate header file • you get an error message from the compiler

  34. Header and Library Files library header file #include <somelib> myprog.C somelib.h user header file #include ”myprog.h” myprog.h compiler object file library file myprog.o libm.a linker executable file myprog

  35. Makefile • A Makefile is a recipe for how to ”cook” a product • The necessary operations are divided into single steps which partially depend on each other • Example: Change a flat tire on a car Actions: get_jack, get_spare_tire, lift_car, remove_flat_tire, attach_spare_tire, lower_car, stow_away_jack, stow_away_flat_tire, drive_away Dependencies: lift_car : get_jack remove_flat_tire : lift_car attach_spare_tire : get_spare_tire stow_away_flat_tire : remove_flat_tire lower_car : attach_spare_tire stow_away_jack : lower_car drive_away : stow_away_flat_tire stow_away_jack

  36. Makefile user header file mat.C lab1.C mat.h source file #include ”mat.h” g++ –c mat.C compiler compiler g++ –c lab1.C mat.o lab1.o object file linker libm.a g++ lab1.o mat.o -lm executable file a.out math library file

  37. Makefile • Assume you have two source files mat.C and lab1.C from which you are supposed to create a program a.out all: a.out # lab1.o depends on lab1.C and mat.h lab1.o: lab1.C mat.h g++ -c lab1.C #mat.o depend on mat.C and mat.h mat.o: mat.C mat.h g++ -c mat.C # a.out depends on lab1.o and mat.o a.out: lab1.o mat.o g++ mat.o lab1.o -lm

  38. Makefile # define a variable CC for the name of the compiler CC=g++ # define compiler flags for warnings and debugging CCFLAGS=-Wall -g # define a variable OBJS for the objects needed to build the program OBJS=mat.o lab1.o # overall target all: a.out # explain for all cc source files how to build an object file %.o: %.C $(CC) $(CCFLAGS) -c $< a.out: $(OBJS) g++ $(OBJS) -lm

  39. Passing by Value • when passing arguments by value, the function creates • new local variables to hold the values of the variable • argument • the value of the original variable are not changed • void f(int val) // the parameter val holds a local copy of the • // variable argument • { • val++; • } • int x=4; • f(x); // call function f passing x by value • cout << x; // x still has the value 4

  40. Passing by Reference • a reference provides an alias –a different name– for a variable • when passing arguments by reference the local variable • is an alias for the original variable • the memory address of the variable is passed such that the • function can access the actual variable in the calling program • void swap (int & a, int& b) // call by reference a,b are aliases • { • int tmp; • tmp = a; • a = b; • b = tmp; • } • int x=3; int y=5; • swap(x,y); // call by reference : a,b are aliases for x,y

  41. Const Parameters/Variables • passing by reference is efficient when passing large data • structures as it avoids copying the variable • a const reference guarantees that the function cannot • modify the value of the passed argument • an attempt to change the value of a const variable or • parameter is caught at compile time • void f( int& a, const int& b ) // argument b is constant • { • const int c=8; // defines variable c as a constant • a=5; // ok • b=7; // error: cannot modify const parameter • c=7; // error: cannot modify const variable • }

  42. Class Definition (Interface) class Date // declares class name { private: // not visible outside the class int day, month, year; // member data public: // visible interface Date(int d, int m, int y); // constructor void add_year(int n); // add n years void add_month(int n); // add n months void add_day(int n); // add n days void show_date(); // displays date }; // do not forget the ; here !!!

  43. Class Implementation void Date::Date(int d, int m, int y) { day=d; month=m; year = y; } void Date::add_month(int n) { month+=n; year+= (month-1)/12; month = (month-1) % 12 + 1; }

  44. Class Implementation #include <iostream> void Date::show_date() // Date:: specifies that show_date is a // member function of class Date { cout << day << ”.” << m << ”.” << y << endl; } void Date::add_year(int n) { year+=y; } void Date::add_day(int n) { …. }

  45. Member Initialization • two types of member initialization in class constructor • by initialization : members are initialized before the constructor is executed (necessary for data members tha have no default constructor) • by assignment : members are created per default constructor first and then a value is assigned to them Date::Date(int d, int m, int y) : day(d), month(m), year(y) { } Date::Date(int d, int m, int y) // assignment initialization { day=d; month=m; year=y; }

  46. Constant Member Functions • A member function that is declared as constant does not modify the data of the object class Date { int month() const; // const month() does not modify attributes of Date void add_year(int n); // non-const add_year() modifies attributes of Date }; int Date::month() const // defined as const { return month; } int Date::add_year(int n) { year+=n; }

  47. Constant Member Functions • A const member function can be invoked for const and non-const objects, whereas a non-const member function can only be invoked for non-const objects Date somedate(12,17,1999); const Date christmas(24,12,2001); int i=somedate.month(); // ok somedate.add_year(2); // ok int j=christmas.month(); // ok month const member function christmas.add_year(3); // error add_year non-const member

  48. Lecture 15 • C++ • Multiple Inheritance • Operator overloading • Templates • Pointers • Dynamic memory allocation • Standard Template Library • Containers • Iterators • Algorithms

  49. Lecture Schedule • Mon 5/11/01 (Bjoern) • VisualWorks/Smalltalk • Mon 12/11/01 (Frank) • C++ • Game playing (lab4) • Course evaluation • Tue 20/11/01 (Bjoern) • VisualWorks/Smalltalk

  50. Inheritance subclass super class Feature A Feature A Feature B Feature B Feature C Feature D

More Related