Lesson Object Oriented Programming Revision Notes-Good For Revision
Lesson Object Oriented Programming Revision Notes-Good For Revision
Java classes contain fields and methods. A field is like a C++ data member, and a method
is like a C++ member function. In Java, each class will be in its own .java file.
5. During an objected oriented class students were asked to implement different access level
in fields and methods in there object oriented practical quiz. Explain these access levels
that the students were required to implement. (3 marks)
b. Explain how OCL might be used in the design of an object oriented program. ( 2 marks)
Answers
It is good practice to produce a design for a program before coding it. Typically in object
oriented programming a design might be realised by a set of Unified Modelling Language
diagrams. The class diagrams would set out the names of the classes, the names of the variables
and the names of the methods etc. Other diagrams would indicate other aspects of the behaviour
of the system. Many of these diagrams are able to capture some aspects of the constraints which
apply to the system. UML diagrams, however, are not sufficient to express all possible
constraints nor are they able to precisely state the constraints they can represent. OCL can be
used to provide further details of the requirements of system which goes beyond what can be
achieved in a UML diagram and allows those details can be represented in a very precise way.
c. In the context of the Object Constraint Language (OCL) define the following: ( 3 marks)
Answers
a) An invariant is a condition that can be relied upon to be true during the execution of a
program, or during some portion of it. It is a logical assertion that is held to always be
true during a certain phase of execution.
b) A precondition is a condition or predicate that must always be true just prior to the
execution of some section of code or before an operation in a formal specification. If a
precondition is violated, the effect of the section of code becomes undefined and thus
may or may not carry out its intended work.
c) A postcondition is a condition or predicate that must always be true just after the
execution of some section of code or after an operation in a formal specification.
a) Discuss any TWO purpose of Object Constraint Language (OCL) that put it ahead of
Unified Modelling Language (UML). (4 marks)
It is good practice to produce a design for a program before coding it. Typically in object
oriented programming a design might be realised by a set of Unified Modelling Language
diagrams. The class diagrams would set out the names of the classes, the names of the variables
and the names of the methods etc. Other diagrams would indicate other aspects of the behaviour
of the system. Many of these diagrams are able to capture some aspects of the constraints which
apply to the system. UML diagrams, however, are not sufficient to express all possible
constraints nor are they able to precisely state the constraints they can represent. OCL can be
used to provide further details of the requirements of system which goes beyond what can be
achieved in a UML diagram and allows those details can be represented in a very precise way.
b) Once object oriented design and analysis is done, there is coding and testing of the code.
Narrate how object oriented code can be tested and any possible UML diagrams that can
be used to aid testing of the code
An open-ended question. The Candidate may look at different approaches, for example:
Fault based testing
Scenario based testing
Sequence Diagrams
Or may discuss black-box and white-box testing with respect to OO programming.
The candidate must discuss which UML diagrams can be used for testing, e.g., Use Case
scenarios
The chain-of-responsibility pattern is a design pattern consisting of a source of command objects and
a series of processing objects.
• Each processing object contains logic that defines the types of command objects tha t it can handle;
the rest are passed to the next processing object in the chain. A mechanism also exists for adding
new processing objects to the end of this chain.
• Primary motivation is the need for a platform-independent (that is, architecture-
neutral)language that could be used to create software to be embedded in various consumer
electronic devices, such as microwave ovens and remote controls.
• Objects with clear responsibilities
• Each class should have a clear responsibility.
• If you can't state the purpose of a class in a single, clear sentence, then perhaps your class
structure needs some thought.
• In object-oriented programming, the single responsibility principle states that every
d) Using diagrams, explain the following terms and how they are used in UML. (8 marks)
Answer
1. Activity diagrams, which show the activities involved in a process or in data processing.
2. Use case diagrams, which show the interactions between a system and its environment.
3. Sequence diagrams, which show interactions between actors and the system and between
system components.
4. Class diagrams, which show the object classes in the system and the associations between
these classes.
5. State diagrams, which show how the system reacts to internal and external event
1) Describe the way in which the Unified Modelling Language (UML) can be used to design
an object oriented program. (5 marks)
Answers
1 An external perspective, where you model the context or environment of the system.
2. An interaction perspective where you model the interactions between a system
and its environment or between the components of a system.
3. A structural perspective, where you model the organization of a system or the
structure of the data that is processed by the system.
4. A behavioral perspective, where you model the dynamic behavior of the system
and how it responds to events.
2) Construct a UML class diagram showing the structure of a professional society, wherein
members pay an annual fee. Your class diagram should incorporate the following 6
classes: member, studentMember, standardMember, seniorMember, society, and
governingCommittee, which should be connected with appropriate relationships, and be
populated with appropriate instance variables and methods to enable the names, addresses
and fees of members to be stored, along with the management committee members, and
the name and HQ address of the society. The governing committee will comprise a
number of senior members.(8 marks)Answers:
Above is shown valid basic structure for the class diagram; any reasonable variant would also be
accepted. If the candidate correctly assembles a member inheritance hierarchy and uses the
correct symbols for inheritance, up to 3 marks were awarded. If the candidate has other
appropriate relationships connecting the member hierarchy to the other classes, including their
multiplicity, up to 3 marks were awarded. If the candidate peppers the classes with appropriate
instance variables and methods, and correctly designates them as private, public or protected,
up to 2 marks were awarded.
3) Bundling code into individual software objects provides a number of benefits. Explain
these benefits
Modularity: The source code for an object can be written and maintained independently of the
source code for other objects. Once created, an object can be easily passed around inside the
system.
• Information-hiding: By interacting only with an object's methods, the details of its internal
implementation remain hidden from the outside world.
• Code re-use: If an object already exists (perhaps written by another software developer),you
can use that object in your program. This allows specialists to implement/test/debug complex,
task-specific objects, which you can then trust to run in your own code.
• Pluggability and debugging ease: If a particular object turns out to be problematic, you can
simply remove it from your application and plug in a different object as its replacement. This is
analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not
the entire machine.
An instance or an object for a class is created in the following way <class name>
<object name>=new <constructor>();
Encapsulation:Encapsulation is the mechanism that binds together code and the data it
manipulates, and keeps both safe from outside interference and misuse.
One way to think about encapsulation is as a protective wrapper that prevents the code and data
from being arbitrarily accessed by other code defined outside the wrapper.
• Access to the code and data inside the wrapper is tightly controlled through a well defined
interface.
• To relate this to the real world, consider the automatic transmission on an automobile.
• It encapsulates hundreds of bits of information about your engine, such as how much we are
accelerating, the pitch of the surface we are on, and the position of the shift.
• The power of encapsulated code is that everyone knows how to access it and thus can use it
regardless of the implementation details—and without fear of unexpected side effects.
Polymorphism:
Polymorphism (from the Greek, meaning ―many forms‖) is a feature that allows one
interface to be used for a general class of actions.
• The specific action is determined by the exact nature of the situation. Consider a stack
(which is a last-in, first-out list). We might have a program that requires three types of
stacks. One stack is used for integer values, one for floating-point values, and one for
characters. The algorithm that implements each stack is the same, even though the data
being stored differs.
• In Java we can specify a general set of stack routines that all share the same names.
More generally, the concept of polymorphism is often expressed by the phrase ―one
interface, multiple methods.‖This means that it is possible to design a generic interface to
a group of related activities.
• This helps reduce complexity by allowing the same interface to be used to specify a
general class of action.
• Polymorphism allows us to create clean, sensible, readable, and resilient code.
class Hierarchies (Inheritance):
• Object-oriented programming allows classes to inherit commonly used state and behavior
from other classes. Different kinds of objects often have a certain amount in common with each
other.
• In the Java programming language, each class is allowed to have one direct superclass, and
each superclass has the potential for an unlimited number of subclasses:
• Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics
of bicycles (current speed, current pedal cadence, current gear). Yet each also defines
additional features that make them different: tandem bicycles have two seats and two sets
of handlebars; road bikes have drop handlebars; some mountain bikes have an additional
chain ring, giving them a lower gear ratio. In this example, Bicycle now becomes the
super class of Mountain Bike, Road Bike, and Tandem Bike.
• The syntax for creating a subclass is simple. At the beginning of your class declaration, use the
extends keyword, followed by the name of the class to inherit from:
class <sub class> extends <super class> {
// new fields and methods defining a sub class would go here
}
4. A colleague suggests that the member class is abstract. What do they mean by this, and
how is an abstract class represented in a UML class diagram? (3 marks)
Answers:
The member class is abstract because it is the super-class of an inheritance (generalization) for
specification hierarchy, giving rise to sub-classes standardMember and seniorMember. It will
never be instantiated, and is there to hold shared properties between standardMember and
seniorMember classes only, and potentially to define methods that must be implemented by those
sub-classes.
2. The Unified Modelling Language 2.0 (UML) comprises of 13 different
diagrams. These can be
broadly categorised as:
i) Behaviour diagrams
ii) Structure diagrams
iii) Interaction diagrams
(a) For each category, give a description of one diagram that falls into the category; include a
simple example of its use and explain when you would use it.
Answer
The following indicate examples of the types of diagrams which fall into each category:
Behavior Diagrams emphasize what must happen in the system being modeled:
Activity diagrams, which show the activities involved in a process or in data processing
State diagrams, which show how the system reacts to internal and external events
Use case diagrams, use case modeling is widely used to support requirements elicitation.A use
case can be taken as a simple scenario that describes what a user expects from a system.
Structure Diagrams emphasise what things must be in the system being modeled:
Class diagrams, which show the object classes in the system and the associations between these
classe
Component diagram
Composite structure diagram
Deployment diagram
Object diagram
Package diagram
Interaction Diagrams, a subset of behavior diagrams, emphasize the flow of control and data
among the things in the system being modeled:
Collaboration (UML 1.x)/Communication diagram (UML 2.0)
Interaction overview diagram (UML 2.0)
Sequence diagrams, which show interactions between actors and the system and
between system components
Use case diagrams, which show the interactions between a system and its environment and is
widely used to support requirements elicitation.A use case can be taken as a simple scenario that
describes what a user expects from a system.
UML Timing Diagram (UML 2.0)
For each category, the student should give a short description of one of the diagrams, include a
simple example and say when they should be used.
1. The Library of Macmillan is a public library that stores various items that can be borrowed,
including books, journals, music, photographs and films. The Library is open to both members
and non-members, but only members can borrow items. There is a limit of ten items in total that
can be borrowed. Members must join first, by providing proof that they live in the Birmingham
area. The library also provides facilities such as Wi-Fi and photocopiers, which both members
and non-members can apply to use. Books can be borrowed for two weeks and other items,
such as music and films for one week. Ifthe borrower keeps the item longer than this, they are
subjected to a fine, which is increased daily. When a member borrows an item, they provide
their libraryNo, if this is valid their loan details are checked to ensure that the items to be
borrowed will not take them over the maximum number of permitted items. A check is also
made to see if they have any fines. If they have a fine, then they cannot borrow any items until
the fine is paid. If all the checks are ok, then the item is issued to the member and the return
date is assigned to the loan. At this point the member can optionally ask for a printout, which
summarises all of the items they have on loan and when each item is due back. Library
members can reserve items that are currently out on loan. If an item proves to be very popular,
then the librarian will order a new copy, provided the cost does not exceed the budget.
Members can register for online facilities so that they can check their own loan details at any
time. Once registered a member can also renew their loans online, provided that the item has
not been reserved.
a) Draw a use case diagram for the library system
b) Write down a use case description of the way a member borrows a book. Your answer
should include a normal sequence and three alternative sequences( 3 marks)
answers
Sample use case description:
Pre-Condition
A member is registered with the library
Actor Action System Response
1. User requests to borrow a book 2. Check libaryId is valid and provides libraryId
3. Check user not exceeded their maximum limit
4. Check user has no fines
5. If ok, book is loaned to user and return date allocated
Alternative sequences
Step 2. Invalid card, loan refused.
Step 3. User has exceeded limit, loan refused.
Step 4. User has fine, loan refused.
Step 6. Request printout of loans
1. in the UML class diagram below for a university database answer the following questions
i. In ODL, define a class student (3 marks)
Answer:
class Student {
attribute string name;
attribute Date dateOfBirth;
attribute string address;
attribute string phone;
// plus relationship and operations . . .
};
ii. In ODL, define a class course (3 marks)
Answer:
class Course {
attribute string crse_code;
attribute string crse_title;
attribute short credit_hrs;
// plus relationships and operation . . .
}
iii. In ODL, define user structures by using the keyword struct (5 marks)
1. struct Address {
string street_address;
string city;
string state;
string zip;
}
2. struct Phone {
short area_code;
long personal_number;
}
3. attribute set _ phone _ phones
iv. In ODL,define the operations for class; student.(3 marks)
class Student {
attribute string name;
attribute Date dateOfBirth;
//user-defined structured attributes
attribute Address address;
attribute Phone phone;
//plus relationship
//operations
short age( );
float gpa( );
boolean register_for(string crse, short sec, string term);
};