Week 2: Catalysts, Queues , Sorting, Component Coupling, and Inheritance

53
Week 2: Catalysts, Queues, Sorting, Component Coupling, and Inheritance Jimmy Voss Disclaimer: Not all material is original. Some is taken from the official course slides, and some is taken from Anna’s slides.

description

Week 2: Catalysts, Queues , Sorting, Component Coupling, and Inheritance. Jimmy Voss Disclaimer: Not all material is original. Some is taken from the official course slides, and some is taken from Anna’s slides. Announcements. Lab 1: Due: Tues Jan 17 - PowerPoint PPT Presentation

Transcript of Week 2: Catalysts, Queues , Sorting, Component Coupling, and Inheritance

Page 1: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Week 2: Catalysts, Queues, Sorting, Component Coupling,

and InheritanceJimmy Voss

Disclaimer: Not all material is original. Some is taken from the official course slides,

and some is taken from Anna’s slides.

Page 2: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Announcements

•Lab 1:• Due: Tues Jan 17• http://www.cse.ohio-state.edu/sce/now/222/labs/lab01/Lab01/Hello

_World.html

Page 3: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Scope

•A variable’s scope refers to the portion of the program body where it is active.•Variables come into scope at the location of their declaration.•Local variables leave scope upon the end of its code block via “}”

Page 4: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Catalyst objects

•An object which is in its default state both when it comes into scope and when it leaves scope is said to act as a catalyst.•We use the key word catalyst to denote such variables.

Page 5: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 6: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Catalyst Objects Exampleglobal_procedure NaiveSwap( alters Sequence_Of_Integer & x, alters Sequence_Of_Integer & y)/*! requires: |x| = |y| ensures: x = #y y = #x */{ Integer I = 0; while ( I < x.Length() ) { object catalyst Integer temp; temp = x[I]; y[I] = x[I]; x[I] = temp; Temp.Clear(); I = I + 1; }}

Page 7: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Queues

•Templated container object.•Traditionally, queues act like a grocery store line.

–FIFO – First in first out.•RESOLVE/C++ allows access of “current” item at the front of the Queue.•Mathematical model: Sequence of Item

Page 8: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Queues

•Operations:• Enqueue( x ) // consumes x• Dequeue( x ) // produces x• Accessor, i.e., [current]

• requires |self| > 0• Length()

Page 9: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 10: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 11: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

global_procedure Find_Min( preserves Queue_Of_Integer & q, produces Integer & min )/*! . . . !*/{ object Integer I = 0; min = q[current]; while ( I < q.Length() ) { object catalyst Integer temp; q.Dequeue( temp ); if ( temp < min ) { min = temp; } q.Enqueue( temp ); I = I + 1; }}

Code Using Queue_Of_Integer

Page 12: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 13: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Records

•Recall: A class is an object which contains both functions and variables. The variables are typically encapsulated.•Records are objects which contain variables but not functions. The variables are NOT encapsulated.•Example: Suppose a University wants to maintain a record for each student. What would we associate with a student?

Page 14: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Records in RESOLVE/C++

•Records are a templated objects which can hold up to 10 fields.•Fields are accessed via the accessor operator [].•Field names are set using the function: field_name.

Page 15: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Instantiating a Recordconcrete_instance class Name_And_Grade : instantiates Record < Text, Integer >{};

field_name (Name_And_Grade, 0, Text, name);field_name (Name_And_Grade, 1, Integer, grade);

Page 16: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 17: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

self Keyword

•Used in the code for member functions of classes.•In addition to the variables passed in via the argument list, the variable “self” is always passed in.•self represents the object which called the member function.

Page 18: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Example using self (not following RESOLVE/C++ conventions)

class foo{public: Initialize() { value = 0; } procedure_body Increment() { self.value = self.value + 1; } procedure_body IncrementTwice() { self.Increment(); self.Increment(); }private: object Integer value;};

Page 19: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Public Class Inheritance

•Recall: inheritance occurs in the form:class child : instantiates parent

•The child class “inherits” all public functions of the parent class.•Functions from the parent class can be overwritten in the child class if one desires.•Encapsulation: parent’s private variables cannot be accessed by the child class.•RESOLVE/C++ inheritance keywords:

• instantiates, specializes, extends, encapsulates, etc.

Page 20: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Public Class Inheritance Exampleclass Animal{public: ... void Make_Sound( alters Character_Ostream & out) { out << “No Sound Available\n”; } void Set_Weight( preserves Real & wt ) { self.weight = wt; }private: Real weight;};

Page 21: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Public Class Inheritance Example

class Dog : public Animal{public: ... void Make_Sound( Character_Ostream & out) { out << “Arf\n”; out << self.weight << “\n”; }

};

Violates encapsulation

Page 22: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Public Class Inheritance Example

•Suppose that we have a Dog object Dog1. Are the following lines of code legal, and what do they do?

–Dog1.Make_Sound( out );–Dog1.Set_Weight( 100.5 );

Page 23: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Relations between classes

•A class is Abstract if its functions have no implementation (that is, only headers). •Abstract base classes are used for all RESOLVE/C++ types. These include formal comments for specifications.•Design-by-Contract – Each function has a formal comment which states what input to the function is valid, and how the function will effect input variables. This DOES NOT give any insight about the implementation.

Page 24: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Relations Between Classes

•Types of Class Dependencies:–Abstract to Abstract – When an abstract class

inherits from an abstract base class–Concrete to Abstract – When a concrete child class

gives an implementation of an abstract base class.–Concrete to Concrete – When a concrete child class

inherits from a concrete base class.

Page 25: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Class Inheritance Relations

• Checks Relation – Child class checks that the ensures clauses of a parent class are met. (Used in debug code)

• Extends Relation – child class adds additional functionality (i.e. via additional functions) to a parent class.

• Instantiation – Child class fills in the base class template parameters.

• Implementation – Child class adds implementation code to an abstract base class’s formally specified functions/procedures.

Page 26: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Class Inheritance Relations

• Partial Instantiation (specializes relation) – The child class fills in some of the parent class templates, but the child class still has unfilled template parameters.

• Uses relation – Concrete to concrete relation. Child uses the implementation of the base class.

• Component Coupling Diagrams (CCDs) are used to show the relations between classes.

Page 27: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Template Variable Usage

• Template variables act as an arbitrary type name, filled in by the compiler at compile time.

• Can be:• A variable type in a function.• An arbitrary base class type in inheritance.• A variable type used within a class (i.e. Item in

container classes).• Instantiation via angle brackets < >.

Page 28: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Checks Relation Exampleconcrete_template < concrete_instance class Item, concrete_instance class Sequence_Base > class Sequence_Kernel_C : checks concrete_instance Sequence_Base { public: procedure_body Add ( preserves Integer pos, consumes Item& x ) { assert (0 <= pos, "0 <= pos"); assert (pos <= self.Length (), "pos <= |self|"); self.Sequence_Base::Add (pos, x); } procedure_body Remove ( /*...*/ ) { /*...*/ } function_body Item& operator [] ( preserves Integer pos ) { /*...*/ } };

Page 29: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 30: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Black box represents templated base class. (templated to remove concrete-to-concrete dependence).

Note: Sequence_Kernal is abstract.Sequence_Kernel_C is concrete.

Page 31: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Checks Relation Exampleconcrete_template < concrete_instance class Item, concrete_instance class Sequence_Base > class Sequence_Kernel_C : checks concrete_instance Sequence_Base { public: procedure_body Add ( preserves Integer pos, consumes Item& x ) { assert (0 <= pos, "0 <= pos"); assert (pos <= self.Length (), "pos <= |self|"); self.Sequence_Base::Add (pos, x); } procedure_body Remove ( /*...*/ ) { /*...*/ } function_body Item& operator [] ( preserves Integer pos ) { /*...*/ } };

Checks a template class base.

Page 32: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 33: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 34: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 35: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Extends Relation

• Child class adds additional functionality to parent class.

• Encapsulation – child class can only use public (or protected) functions and variables from parent class. The variables within the parent classes Representation are said to be encapsulated and cannot be directly modified by member functions defined in the child class.

• Can be Abstract-to-Abstract or Concrete-to-Concrete.

Page 36: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 37: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Example Sequence_Reverseabstract_template < concrete_instance class Item >class Sequence_Reverse : extends

abstract_instance Sequence_Kernel <Item>{public:

procedure Reverse () is_abstract; /*!

ensures self = reverse (#self)

!*/

};

Page 38: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 39: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

• This removes a concrete-to-concrete dependency.• The class which instantiates Sequence_Kernel’s member

functions can be chosen by the client.

Page 40: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 41: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Software Engineering View

•Goals:–Reusable code–Flexibility of Implementation used–Modular design–Separation of Code into logically related chunks.–Takes advantage of the Object Oriented

programming framework.

Page 42: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Utility Classes

•Utility Class – a class which provides a set of operations.•Reasons for use:

–Package together related operations.–Operations need to be defined at the same time as

other template parameters.–Modular code.

Page 43: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Utility Class Notation

• Function calls are made in the form:Utility_Class_Name::Function( <params> )

• Examples: object Integer hash = Text_Hash::Hash( name ); object Boolean flag = In_Order_Tester::In_Order( a, b );

Page 44: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Utility Class Exampleconcrete_template <

concrete_instance class Item,concrete_instance utility_class Item_Are_In_Order,/*! . . . !*/concrete_instance class Queue_Base/*! . . . !*/

>class Queue_Sort_1 : implements

abstract_instance Queue_Sort <Item>, extends

concrete_instance Queue_Base{ . . .};

Page 45: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 46: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Utility Class Example

• Item_Are_In_Order::General_Are_In_Order(a, b) – form of the function call as it would be made within the class Queue_Sort_1

• Item_Are_In_Order is a template class, and hence is not directly linked to Queue_Sort_1.

• Version of Item_Are_In_Order used must provide General_Are_In_Order which works on objects of type Item.

Page 47: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Summary (Class relationships)

• Checks – Base class checks parent’s requires.• Implements – Base provides implementation

of parent’s specs.• Extends – Base class adds functionality to

parent class.• Uses – Concrete to concrete dependency –

child uses parents implementation.• Partial Instantiation / Specializes – Child class

gives a partial instantiation of parent class.

Page 48: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance
Page 49: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Sorting Algorithms

• Sorting is one of the “traditional,” heavily studied problems in Computer Science.

• Goals: • Efficiency• Correctness

• Comparison based sorting algorithm – one which either directly or indirectly compares elements against each other to determine an ordering

• Simple comparison-based sorting Algorithms:• Insertion Sort, Selection Sort, and Bubble Sort

Page 50: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Insertion Sort

• Idea: Have a sorted and unsorted portion of a sequence. Each step takes 1 item from the unsorted portion and inserts it into the sorted portion.

Page 51: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Insertion SortProcedure_Body Insertion_Sort( alters Sequence_Of_Integer & S ){ object Integer Barrier = 1; while ( Barrier < S.Length() ) { object catalyst Integer I = Barrier;

// Find insertion location while ( I > 0 and S[I-1] > S[I] ) { S[I-1] &= S[I]; I = I – 1; } Barrier = Barrier + 1; }}

Page 52: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Efficiency

• Typically, comparison based sorting algorithm efficiency can be measured by the number of comparisons performed between elements.• Simple sorting algorithms are typically .• The fastest comparison based sorting algorithms are .

Page 53: Week 2:  Catalysts, Queues , Sorting,  Component Coupling, and Inheritance

Other Simple Sorting Algorithms

• Selection Sort – Maintain a barrier between sorted and unsorted elements. At each step, place smallest unsorted element at the end of the sorted portion.• Bubble Sort – Considered particularly slow.

• Maintain a barrier.• At each step, bubble largest unsorted element to

the top.• Note: Comparison sorting algorithms only require one operator ‘<‘.