1 / 41

Comp 5421 Object Oriented Programming

Comp 5421 Object Oriented Programming. What is Object Oriented Design? Lecture 1 Tianxiang Shen Summer 2002 Department of Computer Science Concordia university. Project. The main practical component of the course is a project that you will complete as a member of a team.

kobe
Download Presentation

Comp 5421 Object Oriented Programming

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. Comp 5421 Object Oriented Programming What is Object Oriented Design? Lecture 1 Tianxiang Shen Summer 2002 Department of Computer Science Concordia university Concordia TAV 2002 Comp5421_1

  2. Project • The main practical component of the course is a project that you will complete as a member of a team. • Each team will consist of five or six team members. • Each team may propose a project, or choose one from Dr. Grogono’s project list: www.cs.concordia.ca/~faculty/grogono/projects.html. In either case, you must submit a project proposal to the instructor before proceeding. Concordia TAV 2002 Comp5421_1

  3. Project • The project consists of six components, each with its own marks, as shown in the following table. Concordia TAV 2002 Comp5421_1

  4. Project(cont.) • Items 1 through 5 are submitted by the team. Everyone in the team will get the same marks for these items. I will give different marks to team members for one or more items only if I receive a written request signed by all team members. • Item 6 is submitted by individual team members. This document will be used to assess your individual contribution to the project. Concordia TAV 2002 Comp5421_1

  5. Description of Project Components (cont.) The User Manual describes the external attributes of the software in such a way that anyone can use the program. (marking by Lab Instructor) The Design Document includes the high-level design and the detailed design of the software. You can use diagrams, text, or both. The Implementation Notes describe: the implementation plan; the test plan; test results; problems that you encountered and how you solved them; comments on the good/bad features of the language, platform, GUI, etc., that you used; anything else that you think is worth reporting. Concordia TAV 2002 Comp5421_1

  6. Description of Project Components (cont.) 4. Source Code should be complete; it should be formatted properly with appropriate indentation and spacing; and it should contain useful comments. 5. Demonstration is a meeting of the entire team with the instructor in front of a computer. We will run the software and discuss the project. (Marking by Lab Instructor) 6. Individual Report describes: your role in the project; your interactions with other team members; problems you encountered with team work (if any) and how you solved them; your assessment of the contributions of team members - your own and the others; and what you learned by doing this project. Concordia TAV 2002 Comp5421_1

  7. Project Schedule • Week 1: Team set up. • Week 3: Submit proposal. • Week 5: Submit design document. • Week 12: Demonstration • Week 13: Final Submission Concordia TAV 2002 Comp5421_1

  8. Object Oriented Programming(1) Introduction Of Object Oriented Design Concordia TAV 2002 Comp5421_1

  9. Phases of Software Development (cont.)Waterfall Model Concordia TAV 2002 Comp5421_1

  10. Phases of Software Development (cont.) • Requirement analysis is to find out what the program is expected to do (application domain) • Specification is precise description of what the program is supposed to do (but not how it works); it bridges application domain and implementation domains since it describes the external behavior of the system. (can be viewed as a contract) • Design is precise description of internal structures. OOD is usually a description of the class and the their interactions. It is most important phase, if good design, then implementation should be straightforward, otherwise, the project may fail together. Concordia TAV 2002 Comp5421_1

  11. Phases of Software Development(cont.) • Implementation: writing code (programming) • Testing: To ensure that the finished program meets its specification • Documentation: User manual for users. Updated design, specification, and requirements documents • Demonstrations: Trade shows and Marketing efforts, etc. • Revisions & Versions: Incorporate new features, enhancements, suggestions, correct any bugs, etc. Concordia TAV 2002 Comp5421_1

  12. Example of Object Oriented DesignDescription You are to write a program that simulates a solar system. The program will accept as input the initial location, mass, and velocity of the sun, each of the planets and their moons, and some set of asteroids and comets. The program should then simulate the specified solar system in action by repeatedly computing the next position of each of the specified bodies over a period ranging from hundreds to millions of years. The only force you need consider is the gravitational attraction between bodies. Do not worry about collisions. A solar system can be comprised of about one hundred bodies. The simulation of these bodies will proceed in discrete time steps. At each time step the total gravitational force on each body is computed by summing the gravitationalattraction between that body and all other bodies. Then, using the current position, velocity, and this accumulated force, the next position is found. To make the computation more tractable given the large number of bodies, the bodies are grouped hierarchically into subsystems. This sharply reduces the number of necessary gravity computations at a small cost in accuracy. For example, in our solar system Jupiter and all its moons are considered as a single subsystem when computing its attraction with the distant Earth. Concordia TAV 2002 Comp5421_1

  13. Example of Object Oriented Design • Solar system simulation: an example of a continuous simulation. • Requirements: 1. Simulate motion of the sun, planets, satellites and a few other assorted objects; 2. Simulation should run faster than real time; 3. Output of the simulation should be graphical display of the solar system with facilities like scaling, zooming, etc. Concordia TAV 2002 Comp5421_1

  14. Solar system simulation • Specification: Provide a way of setting-up the initial conditions of simulation: mass, position, and velocity at a particular time; Start from these initial condition, use Newton’s laws to compute position and velocity of each body at subsequent times. First-order approximations are sufficient. Note: This is not a complete specification, in more detail later on. Concordia TAV 2002 Comp5421_1

  15. Review Physics Newton’s Law: • 1st Law: If net (resultant) force acting on an body is zero, the velocity of an body does not change. • 2nd Law: If net force acting on an body is not zero, the body will be accelerated in the direction of the force; F = M  a = M  P" • 3rd Law: If body A exerts a force FBA on a body B, the second body exerts an equal and opposite force on the first. (Action equals reaction for quantity) Concordia TAV 2002 Comp5421_1

  16. Review Physics (cont.) • Newton’s Law of Gravitation: Any two particles 1 and 2 attract each other with a force F1,2 = G  M1  M2 / d2 where G is a universal gravitational constant G = 6.672 X 10-11 N · m2/kg 2 d is distance between them • Let F i k be gravitational force on body i by body k, then total gravitational force: F i = F i k i  k • Position pi, Newton's 2nd law: mi d2Pi / dt2 = F i Concordia TAV 2002 Comp5421_1

  17. Review Physics (cont.) • When an object moves with constant speed around a circle, the object is acted by a force, it is called centripetal force. F = M X V2 / R Note: The centripetal force on satellites is the gravitational force. Concordia TAV 2002 Comp5421_1

  18. Review Physics (cont.) • Rewrite F=M*A into orthogonal components parallel to X, Y, Z axes: x“ = Fx / M, y“ = FY / M, z“ = Fz / M • To first-order approximation (by specification of the problem) x’ (t) = [ x(t + t) – x(t) ] / t x(t + t) = x(t) + x’(t) t • Similarly, x’(t + t) = x’(t) + x’’(t) t Note: Taylor Series: x(t+t)=x(t)+ x’(t)t+ x’’(t)/2! (t)2 +···+ x(n-1)(t)/(n-1)! (t)n-1 + Rn where Remainder Rn = x (n)()/(n)! (t)n (  lies between t and t+ t ) Concordia TAV 2002 Comp5421_1

  19. Review Physical (cont.) • Linear Momentum of an object: a vector P, defines as P = m V • Law of conservation of linear momentum: If the system is closed, and isolated, then the total linear momentum  Pi = constant. Concordia TAV 2002 Comp5421_1

  20. Units • Astronomical units are either very large or very small. To avoid this problem, we use arbitrary units. • Consider two-body system (sun s, planet p): Mass of sun: Ms= 20, Mass of planet: M p= 1, Radius of orbit: R = 20, Orbital velocity: V = 50. • Gravitational force between sun and planet must balance with “centrifugal force” acting on the planet: G Ms Mp / R2 = Mp V2 / R Concordia TAV 2002 Comp5421_1

  21. Units (cont.) • Modified gravitation constant G in our arbitrary units will be: G = V2 R / Ms = 502 X 20 / 20 = 2500 (units) • Say, t =0.01, then the distance moved in one step is v t = 0.5 (unit). Number of steps needed to orbit once: 2  R / (V t ) = 80   250 • This is a reasonable choice between speed and precision. Concordia TAV 2002 Comp5421_1

  22. Subsystems • Subsystems reduce the amount of computation. • Consider the system with: the Sun, the Earth, the Moon, the planet Jupiter with its 11 major satellites. Then, the total interactions are 105. • The effect of Jupiter's satellites on the Earth is negligible.  Consider only 3 subsystems: the Sun, the Earth & Moon, the Jupiter & 11 planets. • Sun's 3 interactions + 1 interaction within Earth/Moon + 66 interactions within Jupiter subsystem  A total of 70 interactions. Concordia TAV 2002 Comp5421_1

  23. Object Oriented Design • Describe a solution to a problem in terms of real world objects (of a class) that behave in the same way (member data & member functions / methods). • Identify methods (not its actual implementation). • Understand how one object of a class interacts with other object of the same or other class. • OO design identifies all classes and interactions. Concordia TAV 2002 Comp5421_1

  24. Identifying Classes • Finding potential classes (from noun): Attraction, body, force, moon, number, position, program, solar system, subsystem, and velocity • Choosing top-level classes: 1. minimum subset of the classes is sufficient to describe a solution 2. Classes & instances: Solar system (unique instance) Subsystem (Earth/ Moon, etc.) Body (Sun, Earth, Moon, etc.) Concordia TAV 2002 Comp5421_1

  25. Attributes • Attributes: Solar system has a set of components (subsystem or body); subsystem has a set of components, mass, current position, current velocity; Body has a mass, current position, current velocity. • Note: Mass of a subsystem is the total mass of its components. Position and velocity of a subsystem are actually the position and velocity of the center of mass of its components. Position and velocity of a body that is a component of a subsystem are relative to the center of mass of the system. Concordia TAV 2002 Comp5421_1

  26. Methods • Constructor for each object. • Update() to update position and velocity at time t to position and position at t+t. • To tell an object the total force on it, two ways: 1. setForce(); 2. clearForce() to set the force to zero, and addForce() to add a force to current force. Concordia TAV 2002 Comp5421_1

  27. Methods(cont.) • setForce() needs all forces on the object to be summed externally to the object, whereas object itself sums forces with clearForce() and addForce(). • Which approach is better? It's a design choice. • Let us assume to use clearForce(), addForce(). • Unique solar system object first sends clearForce() to all its components and then, for each pair of components i and k with ik, sends addForce(Fik) to component i, and addForce(-Fik) to component k. Likewise for each subsystem. • display() for an object to write a message or update a graphical image Concordia TAV 2002 Comp5421_1

  28. Class Vector • Position, Velocity and Acceleration are all vector quantities. We introduce class Vector to represent them. • Advantage of class vector is that we can overload standard operators to act on vectors. For example, to update a position, we write simply pos += pos + vel * t where operator+= and operator+ defined for vectors operator* defined to multiply a vector by a scalar. Concordia TAV 2002 Comp5421_1

  29. Designing Class Body • Attribute (data member) of class Body. class Body { public: private: double mass; Vector pos; Vector vel; Vector acc; Vector force; } Concordia TAV 2002 Comp5421_1

  30. Designing Class Body(cont.) • Constructor provides initial values for mass, position and velocity, and default values for acceleration and force. Body: :Body(double iMass, Vector iPos, Vector iVel) { mass = iMass; pos = iPos; vel = iVel; acc = Vector(); // assignment operator & force = Vector(); // construct a zero vector } Concordia TAV 2002 Comp5421_1

  31. Designing Class Body(cont.) • update() method updates the state of a body. void Body::update (const double DT) { acc = force / mass; vel += acc * DT; pos += vel * DT; } // multiply & divide a vector by a scalar, // operator+= for vectors Concordia TAV 2002 Comp5421_1

  32. Designing Class Body(cont.) // Update for the whole system void update(double DT) { int a, b; for (a = 0; a < numbodies; a++) bods [a] - >clear Force(); for ( a = 0; a < numBodies; a++) for (b = a + 1; b < numBodies; b++) { Vector sep = bods [a]->getPos() – bods [b]->getPos (); Vector dir = sep.unit(); double dist = sep.length(); double mag = (GRAV * bods[a] ->getMass () * bods[b]->getMass( ) ) / (dist * dist); Vector force = dir * mag; bods[a]->addForce(-force); bods[b]->addForce(force); } for (a = 0; a < numBodies; a++) bods [a] ->update (DT); } Concordia TAV 2002 Comp5421_1

  33. Designing Class Body(cont.) • Above code needs the following functions of class Body, and some more methods of class Vector (binary -, unary -, unit, length, multiplication of scalars. getMass(), getPos() are trivial and therefore they are not shown here. • Void Body::clearForce () { force = Vector (); } • Void Body::addForce (Vector f ) { force += f; } Concordia TAV 2002 Comp5421_1

  34. Designing Class Vector • The representation of class vector is an array with three components: class Vector { . public: . . . . private: double cpts [3] ; } Concordia TAV 2002 Comp5421_1

  35. Designing Class Vector(cont.) • A single constructor initializes the three components with zero as the default value. (for creating zero vector.) Vector::Vector(double x=0.0, double y=0.0,double z = 0.0) { cpts [0] = x; cpts [1] = y; cpts [2] = z; } • Give clients access to the components by overloading the subscript operator, [], as follows: double & Vector::operator[] (int i) { return cpts[i]; } Concordia TAV 2002 Comp5421_1

  36. Designing Class Vector(cont.) • Assignment operator is conventional, and we return a reference to the result. Vector & Vector::operator= (Vector & other) { cpts[0] = other[0]; cpts[1] = other[1]; cpts[2] = other[2]; return *this; } Vector & Vector::operator+= (Vector & other) { cpts[0] += other[0]; cpts[1] += other[1] cpts[2] += other[2]; return *this; } Concordia TAV 2002 Comp5421_1

  37. Designing Class Vector(cont.) • we can define operators both binary minus (as in u – v ), and unary minus (as in –v ). Vector Vector::operator- (Vector & other) { return Vector ( cpts[0] - other[0], cpts[1] - other[1], cpts[2] - other[2]); } Vector Vector::operator- () { return Vector (-cpts[0], -cpts[1], -cpts[2] ); } Concordia TAV 2002 Comp5421_1

  38. Designing Class Vector(cont.) • Below are vector multiplication / division by a scalar. Vector Vector::operator* (double scalar) { return Vector (cpts[0]*scalar, cpts[1]*scalar, cpts[2]*scalar); } Vector Vector::operator/ (double scalar) { return Vector (cpts[0]/scalar, cpts[1]/scalar, cpts[2]/scalar) ; } Concordia TAV 2002 Comp5421_1

  39. Designing Class Vector(cont.) • Methods for length and unit. double Vector::length () { return sqrt (sqr(cpts[0]) + sqr(cpts[1]) + sqr(cpts[2])); } Vector Vector::unit () { return (*this) / length(); } • Method to write a vector to a stream, it is useful. ostream & operator<< (ostream & os, Vector v) { os << '(' << v[0] ,<< ', ' << v[1]<< ',‘<< v[2] << ')'; return os; } Note: vector argument v is passed by reference Concordia TAV 2002 Comp5421_1

  40. Discussion • Results of design process is a collectionof classes. For each class, we know: 1.interface of the class - the set of methods that the class provides; 2.implementation of the class - the attributes of the class and how the methods affect them. For the whole system, we also know: how the classes interact, that is, what messages they send to one another and when they send them. Concordia TAV 2002 Comp5421_1

  41. Discussion(cont.) Distinguish carefully: • A class is a static, compile-time entity. • An object is a dynamic, run-time entity. • There may be zero, or one, or more instances of a class. • The instances are objects. • All instances of a class behave in the same way because they have the same methods. • Each instance of a class has its own, unique data. Concordia TAV 2002 Comp5421_1

More Related