Introduction to Object Oriented Design Version 1.1.
-
Upload
kelly-morrison -
Category
Documents
-
view
220 -
download
1
Transcript of Introduction to Object Oriented Design Version 1.1.
![Page 1: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/1.jpg)
Introduction toObject Oriented
DesignVersion 1.1
![Page 2: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/2.jpg)
Topics
Designing Your Own ClassesAttributes and BehaviorsClass DiagramsSequence Diagrams
![Page 3: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/3.jpg)
Objectives
At the completion of this topic, students should be able to:
Design classes for use in a C# programExplain the difference between a class and an objectExplain what attributes and behaviors areExplain the terms encapsulation and data hidingCreate accurate class diagrams using UML Create a sequence diagram
![Page 4: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/4.jpg)
Motivation
![Page 5: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/5.jpg)
We have worked on a couple of Bowling Team Programs.
![Page 6: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/6.jpg)
We have worked on a couple of Bowling Team Programs.
In these program we had a lot of data associatedWith a bowling team … * bowling scores * names of bowlers * number of people on the teamAnd operations that we wanted to perform on the data * read scores from a file * sort the scores in order * find the average score * output the scores
![Page 7: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/7.jpg)
We have worked on a couple of Bowling Team Programs.
If you wrote any methods to do these operations, youprobably found yourself passing lots of data around asparameters to the methods.
![Page 8: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/8.jpg)
Wouldn’t it be nice if we could keep the data for abowling team all in one place, and make the methodsthat work on the data easier to write?
We can, if we use objects!
![Page 9: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/9.jpg)
Objects
![Page 10: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/10.jpg)
Key Concept
An object often models things in the real world
Bowling Team
![Page 11: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/11.jpg)
Real world objects have attributes
An object’s attributes describe its“state of being”
depending upon the application, some attributes are more important than others
occupation
Average
heightShirtcolor
![Page 12: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/12.jpg)
Real world objects have attributes
For our application, we are interested in
Number of team members
Their bowling scores
The bowlers names
![Page 13: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/13.jpg)
An object also has behaviors
behaviors define how you interact with the object
Find the highest/lowest/average score
Record their
names
Record their scores
Sort the scores
![Page 14: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/14.jpg)
An Object’s Attributes and Behaviors Should Work Together
this is called cohesion
![Page 15: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/15.jpg)
An Object’s Attributes and Behaviors Should Work Together
Cohesion means that methods and data worktogether, for example, read bowling scores from a file.
![Page 16: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/16.jpg)
A Class is a blueprint that a programuses when it creates an object.
A class reserves no space in memory
When an object is created from the classblueprint, memory is reserved to hold the
object’s attributes.
An object is known as an instance of the class.
Each object has it’s own space for data.
![Page 17: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/17.jpg)
A class is said to be an abstraction of thereal world object that we are modeling.
![Page 18: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/18.jpg)
EncapsulationBowling Team object
readFile( )
namesscores
calling method
we should not allow codeoutside of the object to reach in and change the data directly. Instead, we call methods in theobject to do it for us.
member data is declared as private
member methods are declared as public
public and private are called access modifiers
![Page 19: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/19.jpg)
We use a UML Class Diagram to documentthe data and methods contained in
our class.
![Page 20: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/20.jpg)
Bowling Team
A UML class diagram is usedto describe a class in a very preciseway.
A class diagram is a rectangle.
At the top of the rectangle is theclass name. A line separates theclass name from the rest of thediagram.
class BowlingTeam{}Code represented by the UML diagram
![Page 21: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/21.jpg)
BowlingTeam
- numberOfBowlers: intFollowing the class name we writethe data members of the class. Aline separates the data membersfrom the rest of the diagram.
access modifier:+ public- private
data member name
data type
class BowlingTeam{ private int numberOfBowlers;}Code represented by the UML diagram
![Page 22: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/22.jpg)
Following the class name we writethe data members of the class. Aline separates the data membersfrom the rest of the diagram.
class BowlingTeam{ private int numberOfBowlers; private int[ ] scores; private string[ ] names;}Code represented by the UML diagram
BowlingTeam
- numberOfBowlers: int- scores: int[ ]- names: string[ ]
![Page 23: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/23.jpg)
+ ReadScores( ): void
Following the data members, wewrite the member methods.
access modifier + public - private
method name
parameters
return type
class BowlingTeam{ private int numberOfBowlers; private int[ ] scores; private string[ ] names; public void ReadScores( ){ }}Code represented by the UML diagram
BowlingTeam
- numberOfBowlers: int- scores: int[ ]- names: string[ ]
![Page 24: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/24.jpg)
Following the data members, wewrite the member methods.
class BowlingTeam{ private int numberOfBowlers; private int[ ] scores; private string[ ] names; public BowlingTeam(){ } public void ReadScores(){ } public void SortScores(){ } public void DisplayScores( ){ }}Code represented by the UML diagram
+ ReadScores( ): void + SortScores( ): void + DisplayScores( ): void
BowlingTeam
- numberOfBowlers: int- scores: int[ ]- names: string[ ]
![Page 25: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/25.jpg)
It is important that class diagrams be drawnprecisely and that they conform to the form
shown in these examples.
![Page 26: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/26.jpg)
Class diagrams are static. They describe how a class looks.
To show how objects interact with oneanother as a program executes, we
use a Sequence Diagram.
![Page 27: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/27.jpg)
Consider an Application that contains Book objectsAnd Order objects. They might interact as shown:
![Page 28: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/28.jpg)
anOrder aBook
getTitle
getPrice
calcSalesTax
![Page 29: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/29.jpg)
anOrder aBook
getTitle
getPrice
calcSalesTax
The boxes along the top are objects
![Page 30: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/30.jpg)
anOrder aBook
getTitle
getPrice
calcSalesTax
These lines represent messages beingsent from one object to another
![Page 31: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/31.jpg)
anOrder aBook
getTitle
getPrice
calcSalesTax
These lines represent responses(return values)
![Page 32: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/32.jpg)
anOrder aBook
getTitle
getPrice
calcSalesTax
This is a message that the object sends to itself
![Page 33: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/33.jpg)
Practice
![Page 34: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/34.jpg)
Design a class that represents “Integer” objects.
What are the data members of the class?
![Page 35: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/35.jpg)
Design a class that represents “Integer” objects.
Suppose we want methods to set the integer value in the object retrieve the integer value in the object retrieve the reciprocal of the value in the object
![Page 36: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/36.jpg)
Create the UML class diagramInteger
![Page 37: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/37.jpg)
Design a class that represents “StudentInfo” objects.You could use an object of this class to hold thestudent information you print out at the beginningof each of your programming projects.
What are the data members of the class?
![Page 38: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/38.jpg)
Design a class that represents “StudentInfo” objects.
Suppose we want methods to set the name, course, and section values in the object retrieve the name, course and section values from the object output the data in the student object
![Page 39: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/39.jpg)
Create the UML class diagram
StudentInfo
![Page 40: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/40.jpg)
Design a class that represents a car.The important attributes of a car for thisapplication are how much gas it has in its tank,and what kind of mileage (mpg) it gets.
We need member methods (behaviors) that provide the following:- Create a Car object with a given mpg rating- add n gallons of gas to the tank- drive the car y miles- report on how much gas is in the tank
![Page 41: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/41.jpg)
Car
![Page 42: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/42.jpg)
Design a class that represents a student. The important properties of a student for this application are the student’s name, and the scores for two quizzes (10 pts possible on each) and two exams (100 pts possible on each).
We need member methods that• Create a student object – set all scores to zero• Save the score for quiz 1• Save the score for quiz 2• Save the score for exam 1• Save the score for exam 2• Calculates the student’s percent of points possible
![Page 43: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/43.jpg)
Create the UML class diagram
Student
![Page 44: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/44.jpg)
Checking Account
![Page 45: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/45.jpg)
Create the UML class diagram
Checking Account
![Page 46: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/46.jpg)
PayCheck
![Page 47: Introduction to Object Oriented Design Version 1.1.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649ead5503460f94bb46d8/html5/thumbnails/47.jpg)
Create the UML class diagram
Paycheck