Comparison of Design Styles (Part I) Case Study: 2D Geometry Solver COP 4331 OO Processes for Software Development © Dr. David A. Workman School of Computer.

Slides:



Advertisements
Similar presentations
Ch:8 Design Concepts S.W Design should have following quality attribute: Functionality Usability Reliability Performance Supportability (extensibility,
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Coordinatate systems are used to assign numeric values to locations with respect to a particular frame of reference commonly referred to as the origin.
Chapter 3 Vectors.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 1 Principles of Programming and Software Engineering.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Introduction To System Analysis and design
Chapter 3 Vectors.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 3 Vectors Coordinate Systems Used to describe the position of a point in space Coordinate system consists of A fixed reference point called.
Introduction and Vectors
Adding Vectors, Rules When two vectors are added, the sum is independent of the order of the addition. This is the Commutative Law of Addition.
KINEMATICS OF PARTICLES Kinematics of Particles This lecture introduces Newtonian (or classical) Mechanics. It concentrates on a body that can be considered.
Chapter 10 Rotation of a Rigid Object about a Fixed Axis.
Chapter 3 Vectors. Coordinate Systems Used to describe the position of a point in space Coordinate system consists of a fixed reference point called the.
Vectors and the Geometry of Space 9. Vectors 9.2.
Chapter 3 Vectors. Coordinate Systems Used to describe the position of a point in space Coordinate system consists of a fixed reference point called the.
Types of Coordinate Systems
Vector Application : A UML Example © Dr. David A. Workman School of EE and CS University of Central Florida Feb. 8, 2001.
Starter If you are in a large field, what two pieces of information are required for you to locate an object in that field?
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Software Development. Software Developers Refresher A person or organization that designs software and writes the programs. Software development is the.
File I/O ifstreams and ofstreams Sections 11.1 &
Lab2 C++ Warmup: Pet Applicatioin Cop 4331 and EEL4884 © Dr. David A. Workman School of EE and Computer Science University of Central Florida February.
Chapter 10 Rotational Motion.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Chapter 3 Vectors. Vector quantities  Physical quantities that have both numerical and directional properties Mathematical operations of vectors in this.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Midterm Study Guide COP 4331 and EEL4884 OO Processes for Software Development © Dr. David A. Workman School of EE and Computer Science University of Central.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Data Structures Using C++ 2E
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
CMSC 345 Fall 2000 OO Design. Characteristics of OOD Objects are abstractions of real-world or system entities and manage themselves Objects are independent.
Introduction to the Object-oriented Data Protocol © Dr. David Workman COP4331 School of EE and CS February 4, 2010.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Comparison of Design Styles (Part II) Case Study: 2D Geometry Solver COP4331 OO Processes for Software Development © Dr. David A. Workman March 17, 2009.
Chapter 3 Lecture 5: Vectors HW1 (problems): 1.18, 1.27, 2.11, 2.17, 2.21, 2.35, 2.51, 2.67 Due Thursday, Feb. 11.
Coordinatate systems are used to assign numeric values to locations with respect to a particular frame of reference commonly referred to as the origin.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Chapter 3 Vectors. Vector quantities  Physical quantities that have both numerical and directional properties Mathematical operations of vectors in this.
Design Engineering 1. Analysis  Design 2 Characteristics of good design 3 The design must implement all of the explicit requirements contained in the.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Principles of Programming and Software Engineering
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Object-Orientated Programming
Chapter 5 - Functions Outline 5.1 Introduction
C H A P T E R 3 Vectors in 2-Space and 3-Space
Chapter 3 Vectors.
Chapter 20 Object-Oriented Analysis and Design
CS150 Introduction to Computer Science 1
Introduction to Data Structure
Introduction to Classes and Objects
Presentation transcript:

Comparison of Design Styles (Part I) Case Study: 2D Geometry Solver COP 4331 OO Processes for Software Development © Dr. David A. Workman School of Computer Science and Electrical Engineering March 17, 2009

COP 4232 (C) Dr. David A. Workman2 Problem Statement Vector Application The system shall provide the capability to: 1.Read one or more instances of the problem data from an external ascii file specified by the user at runtime. An instance of the problem data shall include the description of two lines and a point in Real 2D space. Each line shall be composed of two cartesian vectors defining respectively, a point on the line, and the positive direction of the line. The single point shall be defined by a cartesian vector. It is assumed that a vector is a mathematical object with origin at (0,0) and terminus at some point (x,y) in the Real cartesian plane. Each line and the point shall have alphanumeric identifiers that will be used primarily for output purposes. 2. For each problem instance in the input file, compute the following: (a) The point of intersection of the two lines, if the lines are not parallel. (b) Alternatively, output a message stating that the given lines are “parallel” and non-intersecting, or “co-linear” and intersecting at every point on both lines. (c) For each line, compute the distance of the given point from that line. 3.For each problem instance in the input file, write to an ascii output file, also specified by the user at runtime, the following information: (a) An echo of the problem data with both lines and point properly identified. (b) The point of intersection of the two lines, or an appropriate message. (c) For each line (identified properly) output the distance of the given point. The system shall correctly process test input files provided by the client. The developer shall use a specification of the vector abstraction provided by the client, and shall deliver a reusable implementation of this abstraction.

March 17, 2009COP 4232 (C) Dr. David A. Workman3 Vector Abstraction Specification Requirements 1.The name shall be Vector. 2.A vector shall have a representation using Cartesian coordinates and using Polar coordinates. 3.The following operations shall be supported: –A constructor using Cartesian coordinates –A conversion to Polar coordinates –Projection functions for each Cartesian axis. –A Magnitude function –A Unitization function (computes a unit vector in the same direction) –A Negation funtion –A Dot product function –A Cross product function –A Add and Difference function –A Scalar product function –An Insertion operation for writing an vector image to an ascii file –An Extraction operation for parsing a vector image input from an ascii file. –A conversion to an ascii string (same format as Insertion and Extraction). 4.The vector abstraction shall raise a VectorException error for any illegal or undefined vector operations. Such an exception shall identify the operation and the reason for the error.

March 17, 2009COP 4232 (C) Dr. David A. Workman4 Vector Abstraction Example 2D Geometry Solver Application Problem 1 Problem 2 … Problem n ASCII Input File ASCII Output File Solution 1 Solution 2 … Solution n 2D Vector Abstraction USER

March 17, 2009COP 4232 (C) Dr. David A. Workman5 Vector Application: Requirements Y X (a, b) = a b head point (0, 0) tail point V A vector can be represented as a pair (a, b) denoting respectively the X and Y coordinates of the point in 2-space defining the head of a vector with tail at the origin (0, 0).  r rabSinbrCosar ar br    =)    (/)(/) sin() cos( = (r,  ) A vector can also be represented as a pair (r,  ), where r is the length or magnitude of the vector and  is the angular measure of its direction.

March 17, 2009COP 4232 (C) Dr. David A. Workman6 Vector Application: Requirements OperationExpressionDefinition UnitizeUnit(V 1 ) (x 1 /r 1, y 1 /r 1 ) = Polar(1.0,  1 ) Magnitude|V 1 | r 1 = AdditionV 1 + V 2 SubtractionV 1 - V 2 Dot Product V 1  V 2 Cross Product V 1  V 2 Scalar Product c  V 1 = V 1  c ( c  x 1, c  y 1 ) V 1 rotate( r 1, (  1 +c) mod 2  ) Negation- V 1 ( -x 1, -y 2 ) = Polar(r 1, -  1 ) = Polar(r 1, 2  -  1 )

March 17, 2009COP 4232 (C) Dr. David A. Workman7 Vector Application: Requirements Line Equation: where is a vector defining a point on the line and is a unit vector defining the direction of theline (that is, parallelto the line). LxA xU A U ()    Y X A xU  (x> 0) xU  (x< 0) U Lx() Lx()

March 17, 2009COP 4232 (C) Dr. David A. Workman8 The Problem: Obstacle Avoidance G W T Current path corridor V Target Object (T) Obstructing Object (G) Moving Object (W) The problem is depicted in the figure. An object, W, is in motion traveling in a direction and speed defined by velocity vector, V. This vector has been defined to create a future rendezvous with some Target object, T. At some point in time W discovers that some object, G, is obstructing its path. That is, if W continues in its current direction, it will eventually collide with G. W must alter its direction (and possibly speed) to avoid G with the least impact on the total distance it must travel to avoid the obstruction, G. All objects in the scenario are modeled by circles of uniform radius, r. Vector diagram satisfy the following relationships: G = T + D T = W + D W. |D T | ≥ 3r |D W | ≥ 3r We assume no two objects are closer that 3r. DTDT DWDW

March 17, 2009COP 4232 (C) Dr. David A. Workman9 The Solution The Solution involves computing the geometry of two possible paths around the obstructing object G. These paths are depicted in the diagram to the right. Each path has a turning point (P 1 and P 2 ) that is defined by the intersection of two lines, one defined by a clear path to the Target, one defined by a clear path from the Waiter, each tangential to the obstructing object. The alternative clear paths available to the Waiter are characterized by a unit vector in a direction obtained by rotating the vector D W by an angle ± α. Similarly, the alternative clear paths to the Target are obtained by rotating the vector D T through an angle ± β. The two complete clear path from W to T bypassing G are: (a) traveling in direction A - to turning point P 1 and then traveling in direction –B + to the Target, T. (b) traveling in direction A + to turning point P 2 and then traveling in direction –B - to the Target, T. The desired path is the one for which |P k – W| is a minimum, where k  {1,2}. The next slide gives solution details. G W Turing Point (P 1 ) T Turing Point (P 2 ) - α α β - β A-A- A+A+ B-B- B+B+

March 17, 2009COP 4232 (C) Dr. David A. Workman10 Solution Details G W Turing Point (P 1 ) T - α β A-A- B+B+ LALA LBLB DTDT DWDW

March 17, 2009COP 4232 (C) Dr. David A. Workman11 Data Flow Specification Solve 2D-Geometry Problems Input File Output File Problem instances Solution instances Prompt & Error messages File names Context Diagram (Level 0) Define the System Context And External Interfaces User

March 17, 2009COP 4232 (C) Dr. David A. Workman12 Data Flow Specification 1.0 Get File Names Input File Output File Problem instance Solution instances Prompt messages File names Solve 2D-Line Problems (Level 1 Decomposition) 2.0 Open Input File 3.0 Initialize Output File Input File Name Output File Name Error Message File System Data File System Data 4.0 Read Problem Instance 5.0 Compute Solution 6.0 Write Solution Instance Problem Data Solution Data Refine the System Functional Specification one Level See notes view

March 17, 2009COP 4232 (C) Dr. David A. Workman13 Functional Design Main Control Open Input File Get File Name Create Output File File Name Keyboard Display Output File Name Input File Name Input File Object (IFO) Output File Object (OfO) File Prompts & Errors Input File Output File Read Problem Instance Compute Solution Write Solution Instance Problem Specification Problem Description, Line Intersection, Description. Point-to-Line Distances L1 L2 P L1: Line L2: Line P : Vector (point) XP: Intersection Point XM: Intersection Message D1: Distance to L1 D2: Distance to L2 XP, XM, D1, D2 XP, XM, D1, D2 L1, L2, P Unitize Mag Dot Cross Diff Sum ScalarMult Vectors and/or Scalars Vectors or Scalars See notes view IFO OFO

March 17, 2009COP 4232 (C) Dr. David A. Workman14 Functional - Vectorapp.c typedef struct vec { double x,y;} Vector #includes: double Mag( Vector *X ); double Cross(Vector *X, Vector *Y); double Dot(Vector *X, Vector *Y); Vector *Unitof( Vector *v ); Vector *Diff(Vector *X, Vector *Y); Vector *Sum (Vector *X, Vector *Y); Vector *ScalarMult (double c, Vector *X); main () { /* 1: open input file. 2: create output file. 3: problem processing loop 4: close files } 3.1 read and parse line #1 write line #1 to output 3.2 read and parse line #2 write line #2 to output 3.3 read and parse point write point to output 3.4 compute point of intersection write intersection point to output 3.5 compute distance to line #1 3.6 compute distance to line #2 write distance of point to each line. Problem processing loop /* read description of line 1 */ fgets(line,80,fin); /* parse line 1 (A1, D1)*/ sscanf(line," ( %lf, %lf ) ( %lf, %lf )",&A1.x, &A1.y, &D1.x,&D1.y); if( feof(fin) ){ printf("\n Error in Problem instance # %d\n",count); fclose(fin); fclose(fout); exit(1); } double z1,z2; Vector *X1, *X2; /* Compute intersection point */ z1 = Cross(Diff(&A2,&A1),U2)/Cross(U1,U2); z2 = Cross(Diff(&A1,&A2),U2)/Cross(U2,U1); X1 = Sum(&A1,ScalarMult(z1,U1)); X2 = Sum(&A2,ScalarMult(z2,U2));

March 17, 2009COP 4232 (C) Dr. David A. Workman15 Functional- Design Decomposition Style: Functional –Physical Modules: 1 file –Logical Modules: 8 functions Abstractions: –Language Defined: File –Program Defined: Vector Data Encapsulation: None Information Hiding: Limited –The Vector type is public (-) –The client (main) accesses the internal components of Vector instances (-) –The internal view of Vector (polar vs. cartesian) is hidden in the functions that manipulate vectors (+). The client does not assume or exploit the internal view of Vector (+). OO-Ness: Limited –Variables denoting specific instances of type Vector are used in a manner consistent with an object view. (+) –Memory allocated to Vector instances is not reclaimed at the end of their lifetime (-). Reusability: Only at the program/application level. Distribution of Control: All control is encapsulated in the client (application).

March 17, 2009COP 4232 (C) Dr. David A. Workman16 Functional + Vectorapp.c #include: vector.h #includes: typedef struct vec { double x,y; } Vector; typedef struct vec* VECPTR; /* Cartesian view */ double Cartx(VECPTR V); double Carty(VECPTR V); VECPTR Cartesian( double x, double y); VECPTR Getcart(FILE *fin); /* View independent operations */ double Cross(VECPTR X, VECPTR Y); double Dot(VECPTR X, VECPTR Y); VECPTR Unitof( VECPTR v ); VECPTR Diff(VECPTR X, VECPTR Y); VECPTR Sum (VECPTR X, VECPTR Y); VECPTR ScalarMult (double c, VECPTR X); void Putvector(FILE *fout, VECPTR V); void Reclaim( VECPTR V); main () { /* 1: open input file. 2: create output file. 3: problem processing loop 4: close files } 3.1 read and parse line #1 write line #1 to output 3.2 read and parse line #2 write line #2 to output 3.3 read and parse point write point to output 3.4 compute point of intersection write intersection point to output 3.5 compute distance to line #1 3.6 compute distance to line #2 write distance of point to each line. Problem processing loop vector.h vector.c #include “vector.h” /* implementation of vector operations */ /****parse line 1 (A1, D1)***** / if((A1 = Getcart(fin)) == NULL){ printf("\n Error in Problem # %d\n",count); fclose(fin); fclose(fout); exit(1); }; /* similar code to read in D1 */ /* output problem data : line 1*/ fprintf(fout, ”A1 = “); Putvector( fout, A1 ); fprintf(fout,”, D1 = “); Putvector( fout, D1); fprintf(fout, “\n”);

March 17, 2009COP 4232 (C) Dr. David A. Workman17 Functional+ Design Decomposition Style: Functional –Physical Modules: 3 files ( Application, Vector header, Vector implementation) –Logical Modules: 16 functions Abstractions: –Language Defined: File –Program Defined: Vector ( methods added supporting two external views, IO, and memory mgmt ) Data Encapsulation: Vector Abstraction Information Hiding: Almost Complete for Vector abstraction –The Vector type is public (-) –The client (main) does not access any internal components of Vector instances (+) –The internal view of Vector (polar vs. cartesian) is completely hidden in the functions that manipulate vectors (+). The client does not assume or exploit the internal view of Vector (+). OO-Ness: Limited –Variables denoting specific instances of type Vector are used in a manner consistent with an object view. (+) –Memory allocated to Vector instances is reclaimed at the end of their useful lifetime (+). –More opportunities for abstraction, encapsulation, and information hiding exist(-). Reusability: The Vector abstraction is highly reusable. Distribution of Control: Some control has been been Encapsulated in the Vector abstraction. Specifically, the parsing of Vectors on input and the formatting of Vectors on output.

March 17, 2009COP 4232 (C) Dr. David A. Workman18 OO Design Introduction Vector Application: Line Intersection Solver Define Input File Define Output File Solve Problem Instances Use Case Diagram * Mathematician Input File Output File «includes»

March 17, 2009COP 4232 (C) Dr. David A. Workman19 OO Design Introduction Analysis Model (Collaboration Diagram) In-stream Out-stream Vector User-Interface Problem-Manager * 1: prompt 2: file name 3: file ids 4a: open stream 4b: create stream 5b: parse instance 7b: output instance 5a: create instances 6: manipulate instances 7a: output problem data

March 17, 2009COP 4232 (C) Dr. David A. Workman20 OO Design Introduction Design Model (Class Diagram) * ProblemMgr fin: ifstream fout: ofstream finid, foutid: string A1, D1, U1: Vector A2, D2, U2: Vector P: Vector X1, X2: Vector Q1, Q2: Vector d1, d2: double ProblemMgr(); ifstream Filename: string ifstream(); Open(); Close(); Eof(); >> (); ofstream Filename: string ofstream(); Open(); Close(); << (); Vector Double: x,y Vector(); Vector( double, double); Mag(); Unitof(); Cross( Vector ); Dot( Vector ); Sum( Vector ); Diff( Vector ); ScalarMult( double ); Getcart( ifstream ); Putvector( ofstream ); Reads problem Instances from Writes solution Instances to Represents problem instances as User Provides file names to

March 17, 2009COP 4232 (C) Dr. David A. Workman21 OO Design Introduction User ProblemMgr fin: ifstream fout: ofstream Prompt for input file name. finid Open( finid ) Prompt for output file name. foutid Open( foutid ) :Vector A1.Getcart(fin) >>* token Repeated for Vectors D1, A2, D2 and P. << Problem No. A1.Putvector(fout) Repeated for Vectors D1, A2, D2 and P. Repeated for each Problem Instance. Design Model (Sequence Diagram)

March 17, 2009COP 4232 (C) Dr. David A. Workman22 OO - vectorapp.cpp #include vector.h #includes: main () { /* 1: open input file. 2: create output file. 3: problem processing loop 4: close files } 3.1 read and parse line #1 write line #1 to output 3.2 read and parse line #2 write line #2 to output 3.3 read and parse point write point to output 3.4 compute point of intersection write intersection point to output 3.5 compute distance to line #1 3.6 compute distance to line #2 write distance of point to each line. Problem processing loop vector.h class Vector { public: // operations private: // data }; try{ //********parse problem data description A1.Getcart(fin); D1.Getcart(fin); A2.Getcart(fin); D2.Getcart(fin); P.Getcart(fin); }catch(string e){ cout << "Vector io exception raised: " << e << " in Problem instance " << count << endl; return 1; } vector.cpp #include “vector.h” /* implementation of vector class */ //***echo problem data */ fout << endl << "Line 1: A1 = "; A1.Putvector(fout); fout << " D1 = "; D1.Putvector(fout); fout << endl;

March 17, 2009COP 4232 (C) Dr. David A. Workman23 OO - class Vector { public: /* Polar view */ double Mag(); double Angle(); Vector Polar(double radius, double angle); Vector Getpolar(ifstream &fin); /* Cartesian (internal) view */ double Cartx(); //Inspector double Carty(); //Inspector Vector(); //Constructor (default) Vector( double xcoord, double ycoord); //Constructor ~Vector(); //Destructor Vector Getcart(ifstream &fin); /* View independent operations */ double Cross(Vector Y); double Dot(Vector Y); Vector Unitof(); Vector Diff(Vector Y); Vector Sum (Vector Y); Vector ScalarMult (double c); void Putvector(ofstream &fout); private: double x,y; }; vector.h

March 17, 2009COP 4232 (C) Dr. David A. Workman24 OO- Design Decomposition Style: Object Oriented Every component is an object or a class of objects. Even though ProblemMgr serves the same purpose as the “client function” in the Functional+ style, it is still designed as an object (class with one instance ) and consequently it is more reusable and more secure. –Physical Modules: 3 (vectorapp.cpp, vector.h, vector.cpp) –Logical Modules: 2 ( problem manager, vector class ) Abstractions: –Language defined: Ifstream, Ofstream –Program defined: Vector Data Encapsulation: Vector Class Information Hiding: Complete for Vector abstraction –The Vector type is private (+) –The client (main) cannot access any internal components of Vector instances (+) –The internal view of Vector (polar vs. cartesian) is completely hidden in the class that defines vectors (+). The client cannot exploit the internal view of Vector (+). OO-Ness: More than Functional+ –Variables are now full-fledged class instances (objects). –Dynamic memory management is (mostly) automated by the language. Reusability: Vector class is broadly reusable, and because of the inheritance mechanism supported by C++, potential reusability is fully supported by the language (this is not true with the Functional+ --- no support for subtyping or class extension). Distribution of Control: Essentially the same degree of control distribution as was present in Functional+.