The Program Life Cycle.
Download
Report
Transcript The Program Life Cycle.
Object Oriented
Programming 2.
1
0. Admin.
Take in any more program 6’s.
Questions on program 7.
Next Week:
Tuesday: Final Review.
Thursday: no class.
Final: Tuesday, December 11, 10:10am.
2
1.
Review: Advantages of OOP.
1) Coherent bundles of “nouns” and
“verbs.” Analogy: an athlete’s performance
data (nouns) and skill set (verbs).
2) Methods are grouped with the data they
apply to.
3) Methods in a class automatically “know”
about the data in the class, so except for Set
and Get, parameter passing is unnecessary.
3
Advantages of OOP.
4) Data is protected from the client (who
must use the member methods, rather than
directly accessing it) and from other classes.
5) Enhanced code re-use: big class libraries
can be instantiated by any program.
6) Simplifies applications by using client /
server model.
7) Classes can be customized.
4
Review: What is a class?
A class is a bundle, like a record except it
contains both data members and member
methods.
The member methods are usually “public”
(visible to the client program—the interface
/ communication mechanism).
The data members are usually “private”
(invisible to the client which can change the
5
data only via the member methods).
Class
Transform
Set
Get
Client
Program
6
Multi-file programming.
Classes can be stored in separate files from
program drivers (example last time).
Yet we know that there can only be one
executable target…so how is multi-file
programming possible?
Biblical Analogy (1 Cor 12: 12): “The body is a
unit, though it is made of many parts; and though
its parts are many, they form one body. So it is
with Christ.”
Do all the parts matter?
Yes v. 21: “The eye cannot say to the hand,
‘I don’t need you.’”
The driver needs the class because it is
instantiating the class as an object, and
using its methods.
Without the class, we would have a link
error (unresolved external reference).
Do all the parts matter?
On the other hand, the class is just a service,
like a fire extinguisher or a toolkit. It
doesn’t do anything unless there is a driver
specifying how the service is to be used
(how the member methods should be
applied).
The art of Object Oriented
Programming.
OOP changes the focus from solving every
problem within each program (which leads to
reinventing the wheel) to investing time on useful
reusable services.
From theology of glory (the master program is all)
to theology of the cross (servants making
themselves useful).
Programs must decrease that services increase.
The art of Object Oriented
Programming.
But there is a problem:
A good class is both useful and reusable.
It would be very useful to have a class that
does everything my application needs,
but…
It would be nice to have a class that can be
reused by everyone but….
Looking for the tipping point.
Engineering focuses on the best compromise of
multiple competing and maybe conflicting
constraints.
The best car has:
A) super performance;
B) excellent safety;
C) excellent fuel economy;
D) passenger comfort and excellent cargo
capacity.
But….?
Designing classes is similar.
A practical class will not be so tailored to
one application that it cannot be re-used by
any other applications (it only provides data
on underwater Serbo-Croatian basket
weaving), or so generic that it is useless (it
does “stuff”).
Some good examples of classes.
A date class for handling date information
(see text ch. 11).
A country class for storing info on GNP,
GDP, exchange rate, interest rate,
demographic information, religion, literacy,
crime rate.
An employee / student / athlete class etc.
What if there is a hierarchy?
E.g. some information is generic to all
employees: Name, Address, ID, etc. but
some is specific to the type of Employee
(Hourly, Salaried, Contract, Volunteer,
Indentured-Serf, Student. Etc.).
More specific classes can be defined by
inheritance from a base class (see CSC 300
and ch. 12).
Some useful methods for any class.
1) A constructor (a typeless method with the
same name as the class) can be used to initialize
the class data, ideally preventing the client from
misusing the class (GIGO).
In one methodology (Deitel and Deitel), the
constructor sets safe, generic values independently
of any application, while a Set function allows
each particular application to supply data. If the
Set function is not used, the constructor should
guarantee that no disaster occurs.
Some useful methods for any class.
2) A set method allows a user to override the
initial values of the constructor, but should employ
self-defense, only setting the class’s private data to
reasonable client values.
3) An observer: a boolean method that reports
back on the state of the data e.g. is a container full
or empty, is a date valid / invalid.
4) An accessor or Get method: returns a value to
the client e.g. future value of an investment.
Some useful methods for any class.
5) An iterator: a method which updates a value
e.g. increments or decrements a date or time, does
a “find next.”
6) A transformation method: maps data
members to other data members e.g. maps a
statistic to the scaled height of a bar-chart.
7) Private utility methods: a “hands off” method
that the client cannot invoke, but which the object
calls internally at the right time and in the right
way, e.g. to write data to a back-up file.
Object oriented Design.
2 programming design paradigms:
(1) Structured design (SD): focuses on procedural
programming (verb based):
HOW (by what sequence of commands) do we
solve the problem. What is the game plan?
(2) Object oriented design (OOD): focuses on
agents (noun based):
WHO are the agents involved? Who are the
major players?
Lab exercise.
Down-load the 2 separate files for the
integer division problem.
Change to .cs files.
In a console project, highlight the reference
folder and select Project/Add Existing Item
to add both files.
Build and execute.