1 / 47

Introduction to Object Oriented Design Version 1.2

Introduction to Object Oriented Design Version 1.2. Topics. Designing Your Own Classes Attributes and Behaviors Class Diagrams Sequence Diagrams. Objectives. At the completion of this topic, students should be able to:. Design classes for use in a C# program

rigg
Télécharger la présentation

Introduction to Object Oriented Design Version 1.2

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. Introduction toObject Oriented DesignVersion 1.2

  2. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams Sequence Diagrams

  3. Objectives At the completion of this topic, students should be able to: Design classes for use in a C# program Explain the difference between a class and an object Explain what attributes and behaviors are Explain the terms encapsulation and data hiding Create accurate class diagrams using UML Create a sequence diagram

  4. Motivation

  5. Bowling Team Program

  6. Bowling Team Program In these program we had a lot of data associated With a bowling team … * bowling scores * names of bowlers * number of people on the team And 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

  7. Bowling Team Program If you wrote any methods to do these operations, you probably found yourself passing lots of data around as parameters to the methods.

  8. Wouldn’t it be nice if we could keep the data for a bowling team all in one place, and make the methods that work on the data easier to write? We can, if we use objects!

  9. Objects

  10. Key Concept An object often models things in the real world Bowling Team

  11. 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 Average height Shirt color occupation

  12. Real world objects have attributes For our application, we are interested in The bowlers names Their bowling scores Number of team members

  13. An object also has behaviors behaviors define how you interact with the object Record their scores Record their names Find the highest/lowest/average score Sort the scores

  14. An Object’s Attributes and Behaviors Should Work Together this is called cohesion

  15. An Object’s Attributes and Behaviors Should Work Together Cohesion means that methods and data work together, for example, read bowling scores from a file.

  16. A Class is a blueprint that a program uses when it creates an object. A class reserves no space in memory When an object is created from the class blueprint, 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.

  17. A class is said to be an abstraction of the real world object that we are modeling.

  18. Encapsulation Bowling Team object readFile( ) member methods are declared as public calling method we should not allow code outside of the object to reach in and change the data directly. Instead, we call methods in the object to do it for us. names scores member data is declared as private publicandprivate are called access modifiers

  19. We use a UMLClass Diagram to document the data and methods contained in our class.

  20. A UML class diagramis used to describe a class in a very precise way. A class diagram is a rectangle. At the top of the rectangle is the class name. A line separates the class name from the rest of the diagram. Bowling Team class BowlingTeam { } Code represented by the UML diagram

  21. BowlingTeam Following the class name we write the data members of the class. A line separates the data members from the rest of the diagram. - numberOfBowlers: int access modifier: + public - private class BowlingTeam { private intnumberOfBowlers; } Code represented by the UML diagram data member name data type

  22. BowlingTeam Following the class name we write the data members of the class. A line separates the data members from the rest of the diagram. • numberOfBowlers: int • scores: int[ ] • names: string[ ] class BowlingTeam { private intnumberOfBowlers; private int[ ] scores; private string[ ] names; } Code represented by the UML diagram

  23. + ReadScores( ): void BowlingTeam Following the data members, we write the member methods. • numberOfBowlers: int • scores: int[ ] • names: string[ ] class BowlingTeam { private intnumberOfBowlers; private int[ ] scores; private string[ ] names; public void ReadScores( ){ } } Code represented by the UML diagram access modifier + public - private method name return type parameters

  24. + ReadScores( ): void + SortScores( ): void + DisplayScores( ): void BowlingTeam Following the data members, we write the member methods. • numberOfBowlers: int • scores: int[ ] • names: string[ ] class BowlingTeam { private intnumberOfBowlers; private int[ ] scores; private string[ ] names; public BowlingTeam(){ } public void ReadScores(){ } public void SortScores(){ } public void DisplayScores( ){ } } Code represented by the UML diagram

  25. It is important that class diagrams be drawn precisely and that they conform to the form shown in these examples.

  26. Class diagrams are static. They describe how a class looks. To show how objects interact with one another as a program executes, we use a Sequence Diagram.

  27. Consider an Application that contains Book objects And Order objects. They might interact as shown:

  28. aBook anOrder GetTitle GetPrice CalcSalesTax

  29. The boxes along the top are objects aBook anOrder GetTitle GetPrice CalcSalesTax

  30. These lines represent messages being sent from one object to another aBook anOrder GetTitle GetPrice CalcSalesTax

  31. These lines represent responses (return values) aBook anOrder GetTitle GetPrice CalcSalesTax

  32. This is a message that the object sends to itself aBook anOrder GetTitle GetPrice CalcSalesTax

  33. Practice

  34. Design a class that represents “Integer” objects. What are the data members of the class?

  35. 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

  36. Create the UML class diagram Integer

  37. Design a class that represents “StudentInfo” objects. You could use an object of this class to hold the student information you print out at the beginning of each of your programming projects. What are the data members of the class?

  38. 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

  39. Create the UML class diagram StudentInfo

  40. Design a class that represents a car. The important attributes of a car for this application 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 cary miles • report on how much gas is in the tank

  41. Car

  42. 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

  43. Create the UML class diagram Student

  44. Checking Account

  45. Create the UML class diagram Checking Account

  46. PayCheck

  47. Create the UML class diagram Paycheck

More Related