SDSU CS535 Object-Oriented Programming & Design
Fall Semester, 1996
Conceptual Definition of OO

[To Lecture Notes Index]
San Diego State University -- This page last updated Monday, 16 September, 1996
----------

Contents of Conceptual Definition of OO

    1. Conceptual Level Definition

                                Conceptual Definition of OO Slide # 1Listen Here!

Conceptual Level Definition

of OO


"Extracting the essential details about an item or group of items, while ignoring the unessential details."
Edward Berard

Enclosing all parts of an abstraction within a container

Hiding parts of the abstraction

Abstractions arranged in order of rank or level

                                Conceptual Definition of OO Slide # 2Listen Here!
Stack Example

Definition
"A stack is a linear list for which all insertions and deletions are made at one end of the list"

Operations
pop, push, isEmpty, isFull

                                Conceptual Definition of OO Slide # 3Listen Here!
Stack Class
class Stack[1]  {
   private float[] elements;
   private int topOfStack = -1;
   
   public Stack( int stackSize )  {
      elements = new float[ stackSize ];
   }
   
   public void push( float item )  {
      elements[ ++topOfStack ] = item;
   }
   
   public float pop()  {
      return elements[ topOfStack-- ];
   }
   
   public boolean isEmpty()  {
      if ( topOfStack < 0 )    return true;
      else               return false;
   }
   
   public boolean isFull()  {
      if ( topOfStack >= elements.length )    return true;
      else                           return false;
   }
}
Using the Stack
   Stack me = new Stack( 20 );
   me.push( 5 );
   me.push( 12 );
   System.out.println( me.pop() );
   System.out.println( me.pop() );

                                Conceptual Definition of OO Slide # 4Listen Here!
Why Not Struct?
struct Stack 
{
   float stack[100];      
   int topOfStack;
};

void push(Stack& it, int item) 
{  
   it.stack[(it.topOfStack)++] = item;   
}

float pop(Stack& it)
{  
   return it.stack[--(it.topOfStack)];   
}

main()
{
   Stack TryThisOut;         // TryThisOut, Yours, Mine are 
   Stack Yours, Mine;         //  Stack objects

   TryThisOut.topOfStack = 0;
   Yours.topOfStack = 0;

   push(TryThisOut, 5.0);
   push(Yours, 3.3);
   push(TryThisOut, 9.9);

   cout << pop(TryThisOut) << endl;
}

                                Conceptual Definition of OO Slide # 5Listen Here!
RPN Calculator

Postfix (Reverse) Polish Notation
( 3 + 9 - 13 ) * 6 + 2
becomes
3 9 + 13 - 6 * 2 +

Evaluating RPN
read next token
if token is operand push on stack
if token is operator
pop two items off the stack
apply operator to the items from stack
push result on stack
repeat until no more tokens
result is on the top of the stack

                                Conceptual Definition of OO Slide # 6Listen Here!
CalculatorEngine
class CalculatorEngine
{
   private Stack operands = new Stack( 100 );
   
   public float evaluate( String expression )
   {
      StringTokenizer tokenList = new StringTokenizer( expression );
   
      while ( tokenList.hasMoreTokens() )
         processToken( tokenList.nextToken() );
      
      return    operands.pop();
   }
   
   private void processToken( String token )
   {
      if ( token.equals( "+" ) )
         operands.push( operands.pop() + operands.pop() );
         
      else if ( token.equals( "-" ) )
         operands.push( operands.pop() - operands.pop() );
         
      else if ( token.equals( "/" ) )
         operands.push( operands.pop() / operands.pop() );
         
      else if ( token.equals( "*" ) )
         operands.push( operands.pop() * operands.pop() );
      
      else
         operands.push( stringToFloat( token ) );
   }

   private float stringToFloat( String number )
   {
      return Float.valueOf( number ).floatValue();
   }
}

                                Conceptual Definition of OO Slide # 7Listen Here!
Calculator Application
class CalculatorGUI
{

   CalculatorEngine rpnEvaluator;

   public CalculatorGUI( CalculatorEngine myComputeEngine )
   {
      rpnEvaluator = myComputeEngine;

      // code to build the gui interface removed
   }

   // code to make gui run removed
}

Driver Program for Calculator
public static void main( String[] arguments )
{
   CalculatorGUI fourBanger;

   fourBanger = new CalculatorGUI( new CalculatorEngine() );
}

                                Conceptual Definition of OO Slide # 8Listen Here!
Computation as Simulation
Procedure programming consists of procedures acting on data



Object-oriented programming consists of objects interacting



Main() creates web of objects and starts them interacting



                                Conceptual Definition of OO Slide # 9
The OO Process

Step One
Identify the data and the operations on the data
These start to form the classes

Step Two
Determine how classes interact


Step N
Create program on top of the classes

                                Conceptual Definition of OO Slide # 10
Class Relationships
is-part-of
has-a
contains
uses
If class A is-part-of class B then there is no inheritance
An object of type B will interact with an object of type A
Some negotiation between A and B for responsibilities may be needed
Example:
Assume A contains a list that B uses

Who sorts the list? A or B?


----------