29
Object-Oriented Programming (OOP) The RPN Calculator WS 2010/11 Lecture Kleanthis Thramboulidis Visiting Professor Software Engineering Group Electrical & Computer Engineering University of Patras, Greece [email protected] [email protected] saarland.de University of Saarland Nov 2010, Saarbrucken, Germany.

Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Embed Size (px)

Citation preview

Page 1: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Object-Oriented Programming (OOP)The RPN CalculatorWS 2010/11 Lecture

Kleanthis ThramboulidisVisiting Professor

Software Engineering Group

Electrical & Computer Engineering

University of Patras, Greece

[email protected]

[email protected] of Saarland

Nov 2010, Saarbrucken, Germany.

Page 2: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Η RPN Calculator example application

� Develop a program for the system to accept expressions based on the Polish reverse notation and calculate their values.

3 6 + 8 6 - * (3+6) * (8 -6)

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›2

� Design Constraint� Stack has to be used to store operands.

Page 3: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

AgendaAgenda

� “Lego construction” approach

� Repository of components

� Program as an aggregation of existing components

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›3

components

� Mechanisms to integrate components

� The need for new objects

�� Mechanisms to Build new Mechanisms to Build new componentscomponents

� Case study: The RPN calculator

Page 4: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

“Lego construction” approach“Lego construction” approach

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›4

Page 5: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

“Lego construction” approach“Lego construction” approach

Requirements from the implementation environment

Repository of Repository of componentscomponents

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›5

Mechanisms to Mechanisms to Integrate componentsIntegrate components

Mechanisms to Build new componentsMechanisms to Build new components

Page 6: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Repository of componentsRepository of components

� JDK now contains 1.600 APIs� 200 offered in 95

� 500 offered in Java 1.1

� Includes 15 core class librariesapplet net accessibility

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›6

� applet net accessibility

� awt rmi swing

� beans security corba

� io sql text

� lang math util

Page 7: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Java Basic Library

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›7

Page 8: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

1st step

Use the classes Double and Stack of the basic library of Java to develop a Java program for the system to be able to calculate the value of the expression using the stack

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›8

the stack12.0 24.0 +=

Page 9: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Using BlueJ

http://www.bluej.org/The interactive Java environment

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›9

Page 10: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

The program (1st version)public class MyProg{

public static void main(){Double d1 = new Double(20);System.out.println("d1="+d1);Double d2 = new Double(40);

Stack<Double> st = new Stack<Double>();

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›10

Stack<Double> st = new Stack<Double>();st.push(d1);st.push(d2);

Double d3 = st.pop();Double d4 = st.pop(); st.push(new Double(d3+d4));System.out.println("sum is="+st.pop());

}

Draw the object interaction diagram

Page 11: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

The constituent components

Stack

Repository New Components

MyProg

Edit-time vs. run-time

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›11

Double

System

MyProg

Page 12: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Object Interaction Diagram

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›12

Page 13: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

public class MyProg{public static void main(){

Double d1 = new Double(20);System.out.println("d1="+d1);Double d2 = new Double(40);

Stack<Double> st = new Stack<Double>();

Increase modularity

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›13

Stack<Double> st = new Stack<Double>();st.push(d1);st.push(d2);

Double d3 = st.pop();Double d4 = st.pop(); st.push(d3+d4);System.out.println("sum is="+st.pop());

}

Page 14: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

2nd Version – Constituent Components

Stack

Repository New Components

MyProg

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›14

Double

System

MyProg

Adder

Page 15: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Build new ComponentAdder

class Adder{public static void operate(){

Double d3 = st.pop();Double d4 = st.pop();

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›15

Double d4 = st.pop(); st.push(new Double(d3+d4));

}}

Page 16: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Integrate componentsMyProg

Adder

public class MyProg{public static void main(){

Double d1 = new Double(20);System.out.println("d1="+d1);Double d2 = new Double(40);

Stack<Double> st = new Stack<Double>();st.push(d1);st.push(d2);……

?

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›16

Adder……System.out.println("sum is="+st.pop());

}

class Adder{public static void operate(){

Double d3 = st.pop();Double d4 = st.pop(); st.push(new Double(d1+d2));

}}

Flow of control ?

?

Page 17: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Source code (2nd version)public class MyProg{public static Stack<Double> st;public static void main(){

Double d1 = new Double(20);System.out.println("d1="+d1);Double d2 = new Double(40);

class Adder{public static void operate(){

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›17

st = new Stack<Double>();st.push(d1);st.push(d2);

Adder.operate();System.out.println("sum is="+st.pop());

}

public static void operate(){Double d3 = MyProg.st.pop();Double d4 = MyProg.st.pop(); st.push(new Double(d1+d2));

}}

Page 18: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

The graphical user interface

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›18

This is an instance of the class CalculatorGui that is included in the file CalcGuiV1Exer6.jar

Constructor: CalculatorGui(Operand op);

Page 19: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Τα components

Calc

Adder

CalcGuiV2Exer8.jar

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›20

Double

Stack

Operand

Who is creating these objects?

How do they communicate?

Constructor for CalcGuiV2Exer8: CalculatorGui(Operand op, Adder add,

Subtracter sub, Multiplier mul, Divider div, ResultPresenter rp){

Page 20: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

UML class diagram

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›21

Page 21: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Ορισµός της κλάσης Calc

class Calc {

//data members ?

// methods ?

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›22

// methods ?

public static void main() {

?

}

}

Page 22: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Τhe components for the + operation

Adder

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›23

Double

StackHow do they communicate for the program to provide the plus functionality?

Page 23: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Object Interaction Diagram for +

user st:Stack

pop()

ad:Adder

operate()

:Double

?

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›24

pop()

push()

new Double()

Page 24: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

The Adder class

class Adder {public void operate() {Double d1 = Calc.st.pop();Double d2 = Calc.st.pop();Double d3 = new Double(

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›25

Double d3 = new Double(d1.doubleValue() +d2.doubleValue());

Calc.st.push(d3);}

}

Page 25: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

RPN Calculator project

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›26

BlueJ Project

Page 26: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

MyCalculatorGui

public class MyCalculatorGui extends CalculatorGui {

public MyCalculatorGui(Operand op, Adder add, Subtracter sub, Multiplier mul, Divider div, ResultPresenter rp){

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›27

ResultPresenter rp){super(op, add, sub, mul, div, rp);}

}

Page 27: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

RPN Calculator project

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›28

Page 28: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

RPN Calculator project

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›29

Page 29: Object-Oriented Programming (OOP)seg.ece.upatras.gr/Courses/MIM/Labs/3rdLab/LegoIn... · Object-Oriented Programming (OOP) The RPN Calculator ... diagram. The constituent components

Updated class diagram

Is there any Gen/Spec relationship?

©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›30