COMP 121 Week 7: Object-Oriented Design and Efficiency of Algorithms Objectives Learn about the software life cycle Learn how to discover new classes and methods Understand the use of CRC cards for class discovery Be able to identify inheritance, aggregation, and dependency relationships between classes Objectives (cont’d) Learn to use UML class diagrams to describe class relationships Learn how to use object-oriented design to build complex programs Learn how to analyze the efficiency of an algorithm 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 Analysis Phase Defines what the project is suppose to do Not concerned with how the program will accomplish tasks What is the output of the Analysis Phase? Design Phase Plans system implementation Identifies Classes, Methods Needed What is the output of the Design phase? Implementation Phase Write and compile the code Implement classes and methods discovered in the design phase Output? Testing Phase Run tests to verify the program works correctly Output: a report of the tests and their results, test coverage Deployment Phase Software installed and used for its intended purpose The Waterfall Model The Waterfall Model Sequential process of analysis, design, implementation, testing, and deployment When rigidly applied, does not work well! 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 The Spiral Model Extreme Programming Strives for simplicity Removes formal structure Focuses on best practices Realistic planning Small releases Metaphor Simplicity Testing Re-factoring Extreme Programming (cont’d) Focuses on best practices – Pair programming – Collective ownership – Continuous integration – 40-hour week – On-site customer – Coding standards Test Driven Development Test Driven Development Typical Sequence of Steps: – 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 Test Driven Development Discovering Classes Class represents set of objects with the same behavior – Entities with multiple occurrences in problem description are good candidates for objects – Identify commonalities – 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? CRC Cards Class, Responsibility, Collaboration Used to start brainstorming about an object-oriented design Responsibilities are operations performed by the class Collaborators are other classes involved in performing these operations CRC Cards (cont’d) Relationships Between Classes Inheritance Aggregation Dependency Inheritance Is-a relationship Relationship between a more general class (superclass) and a more specialized class (subclass) Example: Every savings account is a bank account Can be overused: Should a Tire be a subclass of Circle? Design Principle: Favor composition over inheritance 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; } Aggregation (cont’d) Every car has a tire (in fact, it has multiple tires) class Car extends Vehicle { . . . private Tire[] tires; } UML Notation 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 UML Diagram Example Class Diagram - BlueJ 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 Discussion Questions? 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 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 CRC Cards Discover classes – Most, if not all, classes are already determined Discover responsibilities – Relate the method names to the verbs in the requirements Describe relationships – Consider whether there are other relationships between the classes besides those already established 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 Implementation Implement the code using test-driven development 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 Summary (cont’d) 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 relationship UML uses different notations for inheritance, interface implementation, aggregation, and dependency Use javadoc comments to document the behavior of classes Questions? 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 increases as a function of the size of the input (asymptotic performance) – Big-O notation Common Growth Rates Growth Rate Functions Growth Rate Table n 100 10000 106 109 101000 O(1) 1 1 1 1 1 O(log n) 2 4 6 9 1000 O(n) 100 10000 106 109 101000 O(n log n) 200 40000 6x106 9x109 101003 O(n2) 10000 108 1012 1018 102000 O(n3) 106 1012 1018 1027 103000 O(2n) 1030 2x103010 O(n!) 9x10157 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); 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); 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); 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); Questions?