The Program Life Cycle.

Download Report

Transcript The Program Life Cycle.

Day 3.
1. Assignment 1.
2. Object-oriented design.
3. Code reusability (advanced).
4. Composition and inheritance.
5. Composition in depth.
6. Brief Lab.
Assignment 1: hints.
 Start by creating the Visit class file.
 public class Visit {
 // data members:

}
// member methods:
Assignment 1: hints.
 What private data members are needed to
solve the problem?
 What does the client input?
 What is calculated?
 What does the class need to send back to the
client?
Assignment 1: hints.




What public member methods are needed?
At a minimum, you need:
1) A constructor.
2) A Set method, sending how many parameters
from client to class?
 3) A Calculate / Transform method.
 4) A Get method, sending how many parameters
from class to client?
Assignment 1: hints.
 Then, complete the client driver file.
 1) instantiate the class as an object

Visit V;
 2) declare variables for the client’s version of the
data—not the same as the object’s member data;
 3) Use the object’s methods to solve the problem.
Another example.
 Test_Class.cs: defines a service class that
computes the mid-term grade based on the average
of 2 mid-term examinations.

Note the reasonableness check in the Set_Data method.
 Test_Driver.cs: uses the service class by
instantiating a mid-term object, and using its
methods.

Note the control abstraction.
2. Object-oriented design.
 There are 2 major modern programming
paradigms:
 (1) procedural programming and
 (2) object oriented programming.
Procedural programming solves problems
by specifying a procedure using imperative
VERBS, whereas OOP tries to identify
active things / agents / objects / NOUNS.
Analogy.
 Compare this with the game of chess.
 Chess can be specified simply by the rules
(procedural)
 Or by the properties of each of the pieces
(an object-oriented approach),
 E.g. a knight is the unique piece that can
make L-shaped moves.
Structured design.
 Associated with each programming paradigm are
different methods of design:
 With procedural programming, the method is
“structured design” (SD).
 SD aims to reduce the problem to the 4 basic
control structures, sequence, selection, repetition,
and subprogram. The emphasis is on how a
problem is solved (flow of control).
Structured design (cont.).
 This is good where the complexity is
primarily in the algorithm / instructions,
but the data is relatively simple.
 E.g. complex mathematical formulae for
manipulating a few numbers.
Limitation of structured design.
 Where it begins to break down, however, is
where the data itself is complex, e.g.
meteorology, medical diagnosis….
 This is also the case with stacks, queues,
lists and trees (data structures). At that
point it is better to analyze the data structure
(thing/noun) into the container(s),
observers and access method(s).
Object-oriented design.
 This requires object-oriented design (OOD) that
focuses on the things or “players.”
 Consider the problem of drawing shapes.
 From an SD perspective: find the right draw, paint
and fill instructions (verbs) to do it.
 From an OOD perspective: create shape objects
that contain key data (e.g. X, Y of the centre) and
methods, so that the object draws itself.
Object-oriented design.
 How to think in OOD terms.
 1. Identify the main agents / players things
e.g. stack / queue / window.
 2. For each object, define:
 (a) how its data is organized and accessed;
 (b) the operations done by / to the thing.
Example of Object-oriented design.
 For example:
 In any problem requiring us to remember data in
reverse order, we can use a stack object.
 The stack container is organized in LIFO fashion
and can be accessed only at the top.
 Stack operations include:
 (1) “iterators” like push and pop and
 (2) “observers” like empty and full.
Implementation of Object-oriented
design.
 To implement this:
 (1) Create a stack class (STACK.CS)
including its data container and member
methods.
 (2) Create a driver containing the top-level
algorithm which uses the object(s) to solve
a specific problem (STAKDRIV.CS).
3. Code reusability (advanced).
 Classes also enhance code reusability by
providing services, which can be used by
many client programs.
 More than that, classes can be re-used by
other classes, in various ways.
 Three examples are:
 (1) composition, (2) inheritance and
 (3) generic / template classes.
Why do this?
 Some classes need components that have already
been defined, or are really only special cases of a
more generic class.
 The idea is to develop a set of very flexible,
generic base classes, test these rigorously, and
then define other classes from these. This is
quicker, and should result in more reliable,
standardized products.
4. Composition and Inheritance.
 A. Composition.
 The idea of composition is that one class
takes another class as a component.
 This is a “__ HAS A ___” relationship.
 Compare: a library (class) has a book case
(object), a restaurant has a soup-Nazi, a
bicycle has 2 wheels (2 wheel objects).
Composition and inheritance (cont.).
 For example, suppose we have a Date object
which includes member methods of SetDate,
UpdateDate, GetDate, etc. and data members of
month, day and year. Another class, e.g.
SalesTransaction, may need to have a date object
as a data member.
 Likewise an Employee class could have an
Address object as a member, or a FinalGrade class
could have a mid-term object as a member.
Composition and Inheritance (cont.).
 B. Inheritance.
 Here a generic base class is defined, and other
descendant classes are defined by inheritance. It is
an “___ IS A ___” relation, where the descendant
class is defined as a more specific instance of the
base class.
 Thus a whale is a mammal, a triangle is a shape, a
credit card purchase is a transaction, an hourly
worker is an employee.
Composition and Inheritance (cont.).
 In the Employee case, we would start with
generic data (Name, ID#, Address) and
methods (Set / Get) true of all employees in
the base class.
 We could then define more specific types of
employee like Hourly, Salaried and
Contract by inheritance.
 More on this next time.
5. Composition in depth.
 Suppose we already have a class for computing
the mid-term grade, Test_Class.cs.
 Obviously the mid-term grade is a component of
the final grade, so the Final_Grade_Class HAS A
mid-term object (instance of the Mid Term
class) as a data member.
 This yields the 3 file suite (hand-out):
 Test_Class.cs, Final_Grade.cs, and
 Final_Driver.cs.
Composition in depth (cont).
 Work through the code in detail.
 Note:
 1) Test_Class.cs is exactly the same as in
the previous 2-file suite.
 2) The Final_Grade_Class.cs, has an object
of the Mid_Term_Grade class as a member.
Composition in depth (cont).
 3) The Final_Grade_Class also adds its own
Set, Get and Calculate methods, which
extend the Mid Term code.
 Note the way the Set_Data and
Find_Final_Score methods use Mid_Term
methods and then extend them.
The driver file.
 Notice that the driver file only needs to
instantiate an object of the Final_Grade
class. Why doesn’t it need a Mid-term
object?
 Another example of data abstraction.
6. Brief lab exercise.




1) First, download the following 2 files:
Test_Class.txt
Test_Driver.txt
2) Include both as references in a project
and change the extension to .cs.
Brief lab exercise.
 2) Then, (a) remove Test_Driver.cs and
download and (b) download and include in
the project source folder:
Final_Grade_Class.txt and Final_Driver.txt
 This shows how we can incorporate a midterm Test object into a Final class to
determine the final grade (composition).