Skip to main contentCal State San Bernardino / [CNS] / [Comp Sci Dept] / [R J Botting] >> [CSci202] >> 04
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Contact] [Grading]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Wed Apr 11 10:26:54 PDT 2007

Contents


    CSci202 Computer Science II, Session 04, Objects and the UML


      (Previous): Review data types and pointers [ 03.html ]

      Preparation

      Study this page and Chapter 6 on Objects and write down the questions, doubts, and surprises that you have on a piece of paper. Include your printed name!

      Error in Figure 6.8 Etc

      Shows the wrong Arrowhead for a UML generalization.

      Assigned Work Due


      (1): Hand in your questions, doubts and surprises. Include a page number and your name.


      (2): Project 1 [ project1.html ] is due at start of this class.

      Quiz 1

      There will be a Quiz on first 5 chapters and/or your project (Not on today's reading and class). It is worth a maximum of 30 points. The structure is:
      1. Basics: chapter 1, 2, 3, 4: fill in the blanks in working code. 10 points.
      2. Pointers: chapter 5: predict output from a given program. 12 points
      3. Project 1. Questionnaire on project. 8 points.

      Input

      There is one small error in the books diagrams of classes. It shows generalization with a normal arrowhead. In the UML it is shown as an open arrowhead or small triangle like this

      [Use open arrowheads in figures 6.8 and 6.9]

      I have several pages of information on the Unified Modeling Language. The quick Guide [ uml0.html ] is a useful starting point.

      Demo -- diagrams of given C++ code

      Instructor will draw a class diagram of
       class Pair
       {
        public:
          double first;
          double second;
      
      
          Pair( double x, double y){ first=x; second=y;}
      
      
       };
      [ 04pair.cpp ] and an object diagram of [ 04testPair.cpp ] (a test program).

      Exercise -- diagrams of given C++ code

      Here is a class definition in C++, draw a class diagram in the UML that models it:

      Here is a class definition in C++, draw a class diagram in the UML that models it:

       	class Triangle
       	{
       	 public:
       		Pair vertex1;
       		Pair vertex2;
       		Pair vertex3;
       		Triangle(Pair v1, Pair v2, Pair v3) { vertex1=v1; vertex2=v2; vertex3=v3 }
       	};

      Demo -- class diagram of problem or solution

      Students describe some software and the teacher draws UML diagrams on the board that model the domain and/or design of the software.

      Exercise -- class diagram of problem or solution

      You have applied for a job as programmer/analyst with a local company called "Universal Widgets". The CIO is interviewing you and asks you to draw a diagram, using the Unified Modeling Language (UML), of the following objects and facts:
      1. Every Widget is a Thing.
      2. Every Wodget is a Thing.
      3. Every Thing has a price.
      4. The company makes a Thing on a particular Date, but many Things are made on any given Date.
      5. Different Widgets have different sizes (measured in inches).
      6. Each Wodget has two Doohickeys attached to it.
      7. Things can be destroyed.
      8. Each Thing can be zarked by the company before it is sold which changes its price, of course.

      Draw a single UML diagram that shows Thing, Widget, Wodget, Date, Doohickey as classes and the facts by relationships(associations, etc) between them. There are attributes (price) and operations(destroy, zark) to put in the diagram as well.

      Here [ 04ex.gif ] is one of my answers to this problem.

      Questions

        One will be answered, at least, from each person. Some in class, some on paper, and some will be recorded here before the next class.

        What is an object

        An object is something that knows some data and can do things with this data. It is a mixture of "know how" and "know what" -- a little piece of intelligence.

        Example: my watch.

        In C++ the operations that an object can do come from it's class.

        Example: My watch is a Digital Watch. A Digital Watch is a special kind of Watch.

        What does Object-Oriented mean?

        An object-oriented program is designed so that it reflects the world with which it is concerned. The real world is (mainly) made of objects that respond in more or less predictable ways to events. Real world objects belong to classes that describe them. The object "dick", for example, is an instance or object of class "People". An object oriented program has similar objects an classes. The behaviors inside the computer simulate the real world consequences of events.

        In the real world there are three relationships:
        RelationMeansExampleUML
        ISAis a special kind ofMale is a PersonMale---|>Person
        HAS Ahas aA Person has two LegsPerson <>-->Legs
        KNOWScan access A Bank knows its CustomersBank--->Customer
        Object oriented program can reflect these types of relationships.

        What are the benefits of OO programming

        First OO programs should fit the problem domain well. Thus you can start to solve problems by Modeling the problem as objects. Second: the solution is easier to understand. Third, small changes in the problem map in an obvious way to changes in the code. Fourth, you can use even a complicated class without having to master all the hidden internal details. Finally, big changes can often be done by adding a new class.... and not even recompiling the old code.

        It also impresses people in job interviews.

        When is OO a waste of time?

        Only when the problem is small and simple: for example a routine to evaluate a square root is best done by the old fashioned structured methods.

        How will we use objects?

        From now on think of ALL data as objects.

        Learn to start solving a problem by listing the classes of object and how they are related -- in the real world.

        Allocate operations to the classes in the problem so that they do useful work.

        When to use inheritance?

        Use inheritance/ generalization when one class of object "is a special kind of" another kind: Car is a special kind of vehicle, for example.

        More in meeting 7 and chapter 9.

        How does an operation act on an object?

        Suppose I have a Cat called "Stranger", what happens when I stroke it?
         		Cat stranger;
         		...
         		cout << stranger.stroke();
        Answer: output "purr".

        The compiler looks at the type of object stranger is and finds out that stranger is a Cat. It, therefore replaces stranger.stroke(); by a call to a function stroke in class Cat -- with the variable this pointing at stranger. The operations inside this function stroke all refer to the data in the object stranger. As a rule you find the data in the object and the function in the class. So all objects share the "member" functions.

        When will use the UML?

        Throughout this class, and also in CSCI320, CSCI330, CSCI350,......CSCI455,.... and your job.

        What are generic program units in C++?

        The are called templates. We will cover them in meetings 17 and 18 (Chapter 14).

      Exercises

      Based on chapter 6 and the Input above.

      Lab

      Arrays are insecure unless you are very careful: [ lab02.html ]

    . . . . . . . . . ( end of section CSci202 Computer Science II, Session 04, Objects and the UML) <<Contents | End>>
    (Next): Classes in C++: [ 05.html ]

    Abreviations

  1. TBA::="To Be Announced", something I have to do.
  2. TBD::="To Be Done", something you have to do.
  3. Dia::="A free Open Source Diagramming tool for Linux, Windoze, etc. ".

End