Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 121 Week 7: Object-Oriented Design and Efficiency of Algorithms.

Similar presentations


Presentation on theme: "COMP 121 Week 7: Object-Oriented Design and Efficiency of Algorithms."— Presentation transcript:

1 COMP 121 Week 7: Object-Oriented Design and Efficiency of Algorithms

2 Objectives To learn about the software life cycle To learn how to discover new classes and methods To understand the use of CRC cards for class discovery To be able to identify inheritance, aggregation, and dependency relationships between classes

3 Objectives (continued) To learn to use UML class diagrams to describe class relationships To learn how to use object-oriented design to build complex programs To learn how to gauge the efficiency of an algorithm

4 The Software Life Cycle Encompasses all activities from initial analysis until obsolescence Formal process for software development  Describes phases of the development process  Gives guidelines for how to carry out the phases Development process  Analysis  Design  Implementation  Testing  Deployment Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

5 Analysis Defines what the project is suppose to do Is not concerned with how the program will accomplish tasks Output of the Analysis Phase:  Requirements Document Describe what program will do once completed Create user manual that tells how user will operate program Establish performance criteria Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

6 Design Plans the system implementation  Discover the structures that underlie problem to be solved  Decide what classes and methods you need  Output of the Design Phase: Description of classes and methods Diagrams showing the relationships among the classes Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

7 Implementation Write and compile the code  Code implements classes and methods discovered in the design phase Output of the Implementation Phase:  The completed program Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

8 Testing Run tests to verify the program works correctly Output of the Testing Phase:  A report of the tests and their results Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

9 Deployment Users install program Users use program for its intended purpose Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

10 The Waterfall Model Sequential process of analysis, design, implementation, testing, and deployment When rigidly applied, waterfall model does not work well Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

11 The Waterfall Model Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

12 The Spiral Model Breaks development process down into multiple phases Early phases focus on the construction of prototypes Lessons learned from development of one prototype can be applied to the next iteration Problem: can lead to many iterations, and process can take too long to complete Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

13 The Spiral Model Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

14 Extreme Programming Strives for simplicity Removes formal structure Focuses on best practices  Realistic planning  Small releases  Metaphor  Simplicity  Testing  Refactoring Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

15 Extreme Programming Focuses on best practices (continued)  Pair programming  Collective ownership  Continuous integration  40-hour week  On-site customer  Coding standards Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

16 Test-Driven Development

17 The specific steps in test-driven development are:  Create a new test case  Write enough code to fail the test  Run the tests and watch the new test fail  Write the simplest code that will pass the new test  Run the tests and watch all the tests pass  Remove duplication  Rerun the tests

18 Test-Driven Development Detail

19 Object-Oriented Design 1. Discover classes 2. Determine responsibilities of each class 3. Describe relationships between the classes Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

20 Discovering Classes A class represents some useful concept Concrete entities: bank accounts, ellipses, products, etc. Abstract concepts: streams, windows, etc. Find classes by looking for nouns in the task description Define the behavior for each class Find methods by looking for verbs in the task description Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

21 Discovering Classes Keep the following points in mind:  Class represents set of objects with the same behavior Entities with multiple occurrences in problem description are good candidates for objects Find out what they have in common Design classes to capture commonalities  Represent some entities as objects, others as primitive types Should we make a class Address or use a String?  Not all classes can be discovered in analysis phase  Some classes may already exist Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

22 CRC Card Describes a class, its responsibilities, and its collaborators Use an index card for each class Pick the class that should be responsible for each method (verb) Write the responsibility onto the class card Indicate what other classes are needed to fulfill responsibility (collaborators) Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

23 CRC Card Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

24 Relationships Between Classes Inheritance Aggregation Dependency Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

25 Inheritance Is-a relationship Relationship between a more general class (superclass) and a more specialized class (subclass) Every savings account is a bank account Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

26 Inheritance Every circle is an ellipse (with equal width and height) It is sometimes abused  Should the class Tire be a subclass of a class Circle ?  The has-a relationship would be more appropriate Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

27 Aggregation Has-a relationship Objects of one class contain references to objects of another class Use an instance variable  A tire has a circle as its boundary: class Tire {... private String rating; private Circle boundary; } Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

28 Example class Car extends Vehicle {... private Tire[] tires; } Every car has a tire (in fact, it has multiple tires)

29 UML Notation for Inheritance and Aggregation Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

30 Dependency Uses relationship Example: Many applications depend on the Scanner class to read input Aggregation is a stronger form of Dependency BlueJ only shows “Uses” relationships Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

31 UML Relationship Symbols RelationshipSymbolLine StyleArrow Tip InheritanceSolidTriangle Interface ImplementationDottedTriangle AggregationSolidDiamond DependencyDottedOpen Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

32 Class Diagram in BlueJ

33 Advanced Topic: Attributes and Methods in UML Diagrams Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

34 Aggregation and Association Association is a more general relationship between classes Used early in the design phase A class is associated with another if you can navigate from objects of one class to objects of the other Given a Bank object, you can “navigate” to Customer objects Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

35 Aggregation and Association Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

36 Five-Part Development Process Gather requirements Use CRC cards to find classes, responsibilities, and collaborators Use UML diagrams to record class relationships Use javadoc to document method behavior Implement your classes Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

37 Discussion Question How does this development process apply to your lab assignments?  Gather requirements  Use CRC cards to find classes, responsibilities, and collaborators  Use UML diagrams to record class relationships  Use javadoc to document method behavior  Implement your code

38 Requirements The requirements are defined in the lab write-up that is handed out  If they are unclear, you should get clarification from the customer (me!) From the requirements, begin thinking about the classes, responsibilities, and relationships  It may be helpful to do this BEFORE you look at the code that was provided

39 CRC Cards Discover classes  Most, if not all, classes are already determined  Examine the classes that are supplied, and make sure you understand how each relates to the nouns in the requirements Discover responsibilities  Methods are included in the classes  Relate the method names to the verbs in the requirements Describe relationships  Some inheritance and dependency relationships are already established  Consider whether there are other relationships between the classes besides those already established

40 UML (In BlueJ)

41 UML Diagrams Examine the inheritance relationships  Think about why that relationship exists  Consider whether there are other inheritance relationships that need to be added Examine the dependency relationships  Think about why that relationship exists (looking at the code can help)  Consider whether there are other dependency relationships that need to be added Determine whether each relationship is an aggregation (collaborator is stored as an object reference) or a simple dependency (collaborator is a method parameter or a return value)  Consider whether there are multiplicities which may indicate the use of an array or other data structure

42 Javadoc Examine the javadoc that is included with the lab  Consider how the methods relate to the responsibilities on the CRC cards  Consider whether there are other methods that should be included

43 Implementation Implement the code using test-driven development

44 Summary The life cycle of software encompasses all activities from initial analysis until obsolescence The waterfall model describes a sequential process of analysis, design, implementation, testing, and deployment The spiral method describes an iterative process in which design and implementation are repeated Extreme Programming is a methodology that strives for simplification and focuses on best practices In object-oriented design, you discover classes, determine the responsibilities of the classes, and describe the relationships between classes A CRC card describes a class, its responsibilities, and its collaborating classes Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

45 Summary (continued) Inheritance (the is-a relationship) is sometimes inappropriately used when the has-a relationship would be more appropriate Aggregation (the has-a relationship) denotes that objects of one class contain references to objects of another class Dependency is another name for the “uses” realtionship UML uses different notations for inheritance, interface implementation, aggregation, and dependency Use javadoc comments to document the behavior of classes Horstmann, C. (2008). Big Java (3 rd ed.). New York: John Wiley & Sons.

46 Any Questions?

47 Efficiency of Algorithms Difficult to get a precise measure of the performance of an algorithm or program Can characterize an algorithm by how the execution time or memory requirements increase as a function of the number of data items that the algorithm processes  Big-O notation A simple way to determine the big-O of an algorithm or program is to look at the loops and to see whether the loops are nested Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

48 Common Growth Rates Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

49 Growth Rates Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

50 How n affects Big-O

51 What is the big-O of this algorithm? for (int i=0; i<n; i++) for (int j=0; j<n; j++) System.out.println(i + “ “ + j); Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

52 What is the big-O of this algorithm? for (int i=0; i<n; i++) for (int j=0; j<2; j++) System.out.println(i + “ “ + j); Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

53 What is the big-O of this algorithm? for (int i=0; i<n; i++) for (int j=n-1; j>=i; j--) System.out.println(i + “ “ + j); Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

54 What is the big-O of this algorithm? for (int i=1; i<n; i++) for (int j=0; j<i; j++) if (j%i == 0) System.out.println(i + “ “ + j); Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

55 Efficiency of Sorts and Searches Big O is commonly used to characterize sort and search algorithms  Linear Search  Binary Search  Bubble Sort  Selection Sort  Insertion Sort  Merge Sort

56 Any Questions?


Download ppt "COMP 121 Week 7: Object-Oriented Design and Efficiency of Algorithms."

Similar presentations


Ads by Google