object-oriented programming (oop)seg.ece.upatras.gr/courses/mim/labs/3rdlab/legoin... ·...
TRANSCRIPT
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] of Saarland
Nov 2010, Saarbrucken, Germany.
Η 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.
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
“Lego construction” approach“Lego construction” approach
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›4
“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
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
Java Basic Library
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›7
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 +=
Using BlueJ
http://www.bluej.org/The interactive Java environment
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›9
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
The constituent components
Stack
Repository New Components
MyProg
Edit-time vs. run-time
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›11
Double
System
MyProg
Object Interaction Diagram
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›12
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());
}
2nd Version – Constituent Components
Stack
Repository New Components
MyProg
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›14
Double
System
MyProg
Adder
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));
}}
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 ?
?
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));
}}
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);
Τα 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){
UML class diagram
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›21
Ορισµός της κλάσης Calc
class Calc {
//data members ?
// methods ?
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›22
// methods ?
public static void main() {
?
}
}
Τ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?
Object Interaction Diagram for +
user st:Stack
pop()
ad:Adder
operate()
:Double
?
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›24
pop()
push()
new Double()
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);}
}
RPN Calculator project
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›26
BlueJ Project
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);}
}
RPN Calculator project
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›28
RPN Calculator project
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›29
Updated class diagram
Is there any Gen/Spec relationship?
©Kleanthis Thramboulidis “Lego Construction” Approach ‹#›30