Implementation in Java: 4.1 Calculator

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

4

Implementation in Java

In Chapter 3, we demonstrated how object-oriented concepts can be implemented via


notations in the Java programming language. For validation purposes, these concepts
allow objects in our system to be operational. We now proceed to see how they are
practically applied to example problems as typical programs.

4.1 Calculator
We first consider how a simple calculator with the basic four arithmetic functions, as
illustrated in Figure 4-1, may be implemented. Most generic machines allow adding
11 to 13 to be accomplished via the buttons 1 3 + 1 1 = .

34

1 2 3 +
4 5 6 –

7 8 9 *
C 0 = /

Figure 4-1: Four-function calculator.


40 Object-Oriented Programming and Java

For a simple implementation, we will initially not concern ourselves with the
nonessentials of external looks (e.g., keypad layout or casing specifics as in real
calculators), but instead concentrate on the core calculating engine. Enhancements
involving mechanisms for the user interface may be subsequently considered. This is
consistent with the software engineering principle of abstraction.
Conceptually, the core calculator engine may be viewed as being comprised of
registers to hold values, together with built-in operators such as addition and subtrac-
tion to manipulate such values. Having operations involving two operands, the calcu-
lator needs at least two registers.
Four arithmetic operations imply at least four operators. A compute operator is
required to get its execution started, together with a clear operator to prepare the
registers for new computations. These correspond to the equal and clear keys on a
calculator. Lastly, the digits form the basic input mechanism for numeric operands.
We would assume that the display operator will retrieve a value for display on the
calculator panel.
Object initialization may be easily accomplished via constructor methods. In
this case, a CalculatorEngine object is initialized via invoking the clear operator.
The resultant Java code skeleton for CalculatorEngine objects with this basic rep-
resentation is shown in Listing 4-1. Note that all operators easily correspond to
instance methods, as well as to buttons on the face on a conventional calculator.
Code represented by ellipses will be elaborated in due course.

class CalculatorEngine {
int value;
int keep; // two calculator registers
void add() { ... }
void subtract() { ... }
void multiply() { ... }
void divide() { ... }
void compute() { ... }
void clear() { ... }
void digit(int x) { ... }
int display() { ... }
CalculatorEngine() { clear(); }
}
Listing 4-1: CalculatorEngine skeleton.

4.1.1 The clear() Method


The clear() method initializes the CalculatorEngine object to the state for a calcu-
lator to begin the key sequence for next calculation. It is thus intuitive to set the
variables value and keep to 0.

void clear() {
value = 0;
keep = 0;
}
Implementation in Java 41

4.1.2 The display() Method


To implement the display() method to provide computation results, we must first
clarify the purposes of the instance variables value and keep. The former is updated
as a result of new inputs from numeric keys or the result of an operator, and thus is
used to update the display area.
As expressions are keyed in using an infix notation (e.g., 1 3 + 1 1 = ),
the first operand must be stashed away before it is overwritten by the entry of the
second operand. The keep instance variable serves this purpose.

int display() {
return(value);
}

4.1.3 The digit() Method


The digit() method accumulates digits keyed in via the numeric keypad. A new
digit shifts existing digits in the value instance variable one place to the left. This
manipulation is accomplished by multiplication by 10 and followed by addition of
the last digit.

void digit(int x) {
value = value*10 + x;
}

While this method stands out amongst the other methods as it expects an inte-
ger parameter to indicate which numeric key was pushed, it can be circumvented by
using wrapper methods such as zero(), one(), two(), three(), ...nine().

void one() {
digit(1);
}

void two() {
digit(2);
}
...

4.1.4 Operator Methods


The infix mode of the add, subtract, multiply and divide operators requires that the
specified operation be stashed away to be applied after input of the second operand.
For this purpose, we define another instance variable toDo which records the action
to be associated with the next compute operation.

char toDo;
void add() {
keep = value; // keep first operand
value = 0; // initialise and get ready for second operand
toDo = ’+’; // this is what we should do later
}
42 Object-Oriented Programming and Java

void subtract() {
keep = value; // keep first operand
value = 0; // initialise and get ready for second operand
toDo = ’-’; // this is what we should do later
}

Since all the binary operations have the same form, it is again natural to adopt
abstraction techniques to relocate common code in a binaryOperation() method:

void binaryOperation(char op) {


keep = value; // keep first operand
value = 0; // initialize and get ready for second operand
toDo = op;
}

void add() { binaryOperation('+'); }


void subtract() { binaryOperation('-'); }
void multiply() { binaryOperation('*'); }
void divide() { binaryOperation('/'); }

Lastly, we conclude with the compute operation which provides the answer to
applying the operator in toDo on the operands value and keep.
void compute(){
if (toDo == '+')
value = keep + value;
else if (toDo == '-')
value = keep - value;
else if (toDo == '*')
value = keep * value;
else if (toDo == '/')
value = keep / value;
keep = 0;
}

4.2 Code Execution


In the previous chapter, we learned that the new operator creates an object instance of
the class that it is applied to. Thus,
CalculatorEngine c = new CalculatorEngine();

creates an instance and associates it with the variable c. Subsequently, the code
sequence

c.digit(1);
c.digit(3);
c.add();
c.digit(1);
c.digit(1);
c.compute();
Implementation in Java 43

computes the value of the expression 13 + 11. For verification purposes, the Java
API (Application Program Interface) method System.out.println() may be used
to produce output on the screen:

System.out.println(c.display());

There is, however, a slight snag: the CalculatorEngine object instance is the
only object in existence, yet which object would send it messages to compute
expressions? Or even more fundamental, at the very commencement of program
execution when no objects existed, how was the first object created?
Java solves this issue through the introduction of class methods, which are
invoked with respect to the class they are associated with rather than object instan-
ces. More specifically, the body of the static method named main() is the first code
sequence to be executed. As such, the previous code sequence must be brought into
main() and rearranged as follows:

public static void main(String arg[]) {


CalculatorEngine c = new CalculatorEngine();
c.digit(1);
c.digit(3);
c.add();
c.digit(1);
c.digit(1);
c.compute();
System.out.println(c.display());
}

The various code fragments may be brought together within a class construct in
the file CalculatorEngine.java as shown in Listing 4-2.

class CalculatorEngine {
int value;
int keep; // two calculator registers
char toDo;

void binaryOperation(char op) {


keep = value; // keep first operand
value = 0; // initialize and get ready for second operand
toDo = op;
}

void add() { binaryOperation('+'); }


void subtract() { binaryOperation('-'); }
void multiply() { binaryOperation('*'); }
void divide() { binaryOperation('/'); }

void compute() {
if (toDo == '+')
value = keep + value;
else if (toDo == '-')
value = keep - value;
else if (toDo == '*')
value = keep * value;
else if (toDo == '/')
value = keep / value;
keep = 0;
}
44 Object-Oriented Programming and Java

void clear() {
value = 0;
keep = 0;
}

void digit(int x) {

value = value*10 + x;
}

int display() {
return(value);
}

CalculatorEngine() { clear(); }

public static void main(String arg[]) {


CalculatorEngine c = new CalculatorEngine();
c.digit(1);
c.digit(3);
c.add();
c.digit(1);
c.digit(1);
c.compute();
System.out.println(c.display());
}
}
Listing 4-2: CalculatorEngine class.

With the Java Development Kit (JDK) appropriately installed, it may be com-
piled via:

$ javac CalculatorEngine.java

where CalculatorEngine.java is the name of the file containing the Java source
and $ is the system’s command line prompt. Similarly, execution of the resultant
Java byte code may proceed via:

$ java CalculatorEngine

4.3 Simple User Interface


While the code in static void main() does execute to show the behavior of a
CalculatorEngine object instance, it is an absolutely clumsy situation. Each
evaluation of a new arithmetic expression requires editing code and recompilation.
Ideally, we should be compiling the source once, but inputting different expressions
for evaluation.
It is common to have an user interface object to work cooperatively with the
CalculatorEngine. This separation of concerns allow for the CalculatorEngine to
Implementation in Java 45

be independent of interface issues. We will initially consider a line-mode user inter-


face and subsequently enhance it for a windowing environment.
To this end, a CalculatorInterface object fulfills this role. It has the role of
a middleman that does not work too much, but instead accepts input and passes it
onto the CalculatorEngine. Similarly, feedback from the CalculatorEngine is
collected and becomes output for the CalculatorInterface object.
The implementation of CalculatorInterface consists of an initializing phase
where a CalculatorEngine object is bound to an CalculatorInterface object,
and an execution phase which performs the necessary dispatching. These are imple-
mented by the constructor and run() methods of CalculatorInterface respec-
tively, as illustrated in Listing 4-3.
import java.io.*;

class CalculatorInput {
BufferedReader stream;
CalculatorEngine engine;

CalculatorInput(CalculatorEngine e) {
InputStreamReader input = new InputStreamReader(System.in);
stream = new BufferedReader(input) ;
engine = e;
}

void run() throws Exception {


for (;;) {
System.out.print("[" + engine.display() + "]");
String m = stream.readLine();
if (m == null) break;
if (m.length() > 0) {
char c = m.charAt(0);
if (c == '+') engine.add();
else if (c == '-') engine.subtract();
else if (c == '*') engine.multiply();
else if (c == '/') engine.divide();
else if (c >= '0' && c <= '9') engine.digit(c - '0');
else if (c == '=') engine.compute();
else if (c == 'c' || c == 'C') engine.clear();
}
}
}

public static void main(String arg[]) throws Exception {


CalculatorEngine e = new CalculatorEngine();
CalculatorInput x = new CalculatorInput(e) ;
x.run();
}
}

Listing 4-3: CalculatorInput class.

While the code for CalculatorInterface relies on facilities for exception


and input/output handling that have not been described yet, these may initially be
ignored. Nevertheless, the code serves two immediate purposes here:
46 Object-Oriented Programming and Java

It demonstrates the context of a test harness and how it is easily constructed


to aid incremental development.
It shows the synergistic cooperation of two objects with distinct concerns in
an object-oriented design environment.

Until these topics are discussed in Chapters 9 and 10, it is not harmful that at
present, they be taken by faith. The “throws Exception” signature suffix allows for
Java exceptions to be for the moment ignored. It is useful for modular and secure
programming methodology. It also suffices that the BufferedReader class facilitates
input, and that the readLine() method allows an input line to be read.
The new user interface class may be compiled via

$ javac CalculatorInput.java

This provides the added flexibility of arbitrary computations via keyboard input
sequences. The calculator display is indicated within square brackets “[ ]”:

$ java CalculatorInput
[0]1
[1]3
[13]+
[0]1
[1]1
[11]=
[24]

4.4 Another Interface for CalculatorEngine


The separation of concerns between CalculatorEngine and CalculatorInterface
allows for the former to be reused in different environments. To show the ease of
code reusability when a neat modular structure is adopted, another user-interface
framework to work with CalculatorEngine is introduced in this section.
Similar to CalculatorInput, CalculatorFrame provides an environment for a
CalculatorEngine object to execute. The major difference is that CalculatorFrame
caters for a windowing environment, and gives the illusion that the calculator “hides”
behind the frame.
Implementation in Java 47

Windowing facilities in Java will be discussed in Chapter 13. However, this


graphical calculator example is still appropriate since its objective is to show the
benefits of modular code and reusable API libraries in Java. Code in the constructor
method sets up a calculator frame with buttons and a display at appropriate locations.
Using a graphical user interface in this instance is fairly straightforward since mouse
clicks on calculator buttons are mapped to actionPerformed() method. As such,
code that performs the necessary dispatching to CalculatorEngine shown in List-
ing 4-4 is similar to that in the run() method in CalculatorInput.

import java.awt.*;
import java.awt.event.*;

class CalculatorFrame extends Frame implements ActionListener {

CalculatorEngine engine;
TextField display;

WindowListener listener = new WindowAdapter() {


public void windowClosing(WindowEvent e) { System.exit(0); }
};
CalculatorFrame(CalculatorEngine e) {
super("Calculator");
Panel top, bottom; Button b;

engine = e;
top = new Panel();
top.add(display = new TextField(20));
bottom = new Panel();
bottom.setLayout(new GridLayout(4,4));
bottom.add(b = new Button("1")); b.addActionListener(this);
bottom.add(b = new Button("2")); b.addActionListener(this);
bottom.add(b = new Button("3")); b.addActionListener(this);
bottom.add(b = new Button("+")); b.addActionListener(this);
bottom.add(b = new Button("4")); b.addActionListener(this);
bottom.add(b = new Button("5")); b.addActionListener(this);
bottom.add(b = new Button("6")); b.addActionListener(this);
bottom.add(b = new Button("-")); b.addActionListener(this);
bottom.add(b = new Button("7")); b.addActionListener(this);
bottom.add(b = new Button("8")); b.addActionListener(this);
bottom.add(b = new Button("9")); b.addActionListener(this);
bottom.add(b = new Button("*")); b.addActionListener(this);
bottom.add(b = new Button("C")); b.addActionListener(this);
bottom.add(b = new Button("0")); b.addActionListener(this);
bottom.add(b = new Button("=")); b.addActionListener(this);
bottom.add(b = new Button("/")); b.addActionListener(this);
setLayout(new BorderLayout());
add("North", top);
add("South", bottom) ;
addWindowListener(listener) ;
setSize(180, 160) ;
show();
}
public void actionPerformed(ActionEvent e) {
char c = e.getActionCommand().charAt(0);
if (c == '+') engine.add();
else if (c == '-') engine.subtract();
else if (c == '*') engine.multiply();
48 Object-Oriented Programming and Java

else if (c == '/') engine.divide();


else if (c >= '0' && c <= '9') engine.digit(c - '0');
else if (c == '=') engine.compute();
else if (c == 'C') engine.clear();
display.setText(new Integer(engine.display()).toString());
}
public static void main(String arg[]) {
new CalculatorFrame(new CalculatorEngine());
}
}
Listing 4-4: CalculatorFrame class.

4.4.1 Event-Driven Programming


While much code may be presently skipped, it is of great encouragement to readers
that the API libraries allow for windowing applications to be developed with mini-
mal user-code. Much code occur in the constructor and actionPerformed() meth-
ods, which sets up the calculator buttons and respond to mouse clicks.
The code in the CalculatorFrame class looks somewhat strange because it is
not completely procedural in its specification. In particular, while the body of the
actionPerformed() method resembles that in run()in CalculatorInput, the for-
mer is not explicitly invoked from within the class, such as from static void
main() (as was the case for CalculatorInput) .
Procedural programming is the paradigm where actions are specified in a step-
by-step sequence such as a baking recipe. Within each Java method, code is specified
procedurally and the execution order may be easily determined.
In event-driven programming, code fragments are instead associated with
events and invoked when these events occur. In a typical graphical environment with
windowing facilities, events correspond to mouse movements, mouse clicks and
keystrokes from the keyboard. It is impossible to determine or plan in advance what
course of actions users might take to accomplish a certain task. Instead, we associate
code fragments with significant events so that their side-effects will be appropriate
response to such external events.
In an object-oriented system, methods are convenient units of code, and are
used to receive stimuli from external devices. In Java, an ActionListener keeps a
lookout for events involving mouse clicks. This is relevant to our CalculatorFrame,
and we in turn implement the actionPerformed() method as a trigger point for
mouse clicks on calculator buttons.
Thus, for each push as a calculator button, actionPerformed() is invoked, and
it uses the getActionCommand() to identify which button. The framework for win-
dowing using the AWT API will be further elaborated in Chapter 13.
Similarly, WindowAdaptor is used to monitor events involving windows, and
windowClosing() is the corresponding method which is invoked when the user
clicks to close the calculator window. Execution is terminated via System.exit(0).
Implementation in Java 49

4.5 Summary
This chapter demonstrates Java syntax and semantics covered earlier in Chapter 3.
The calculator case study example shows how:

a class construct may be developed and compiled for execution by the


virtual machine;
execution commences with the static void main() method;
objects are instantiated and appropriately coordinated for cooperative
message passing to model;
the BufferedReader class is used for input;
the AWT package is used for GUI programming using frames and involv-
ing event-handling.

4.6 Exercises
1. The operators for CalculatorEngine are binary and require two operands.
How would unary operators that require one operand be incorporated?
Modify the CalculatorEngine class to add the following capabilities.
squareOf
factorial

2. Choose two interface characters most apt for squareOf and factorial
and incorporate the additional capability into the CalculatorInput class.
3. Rearrange the layout of the calculator panel in the CalculatorFrame class
to accommodate the new capabilities, and modify the appropriate event-
handlers to take advantage of these functions.
4. The CalculatorFrame class produces the result of 247 corresponding to
the key input sequence 1 3 + 1 1 = 7 . Explain the reason for this
observation, and suggest how it may be corrected.
TUGAS PEMROGRAMAN BERORIENTASI
OBJEK

KALKULATOR

OLEH:
FEMBI REKRISNA GRANDEA PUTRA
M0513019

JURUSAN INFORMATIKA
FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM
UNIVERSITAS SEBELAS MARET
SURAKARTA
PENDAHULUAN

Java adalah bahasa pemrograman yang dapat dijalankan di komputer atau di telepon
genggam. Java juga merupakan bahasa pemrograman tingkat tinggi yang berorientasi objek
(OOP) yaitu cara ampuh dalam pengorganisasian dan pengembangan perangkat lunak. Pada
OOP, program komputer sebagai kelompok objek yang saling berinteraksi. Deskripsi
singkat OOP adalah mengorganisasikan program sebagai kumpulan komponen yang disebut
dengan objek. Program java tersusun dari bagian-bagian yang disebut kelas. Kelas itu
sendiri terdiri atas metode-metode yang melakukan pekerjaan dan mengembalika n
informasi setelah melakukan tugasnya.

Para pemrogram Java banyak mengambil keuntungan dari kumpulan kelas di pustaka
kelas Java, yang disebut dengan Java Application Programming Interface (API). Kelas-kelas
ini diorganisasikan menjadi sekelompok yang disebut paket (package). Jadi ada dua hal
yang harus dipelajari dalam Java, yaitu mempelajari bahasa Java dan bagaimana
mempergunakan kelas pada Java API. Kelas merupakan satu-satunya cara menyatakan
bagian eksekusi program, tidak ada cara lain. Sedangkan pengertian API itu sendiri ialah
seperangkat fungsi standar yang disediakan oleh OS atau Bahasa. Dalam Java, API
dimasukkan ke dalam package-package yang sesuai dengan fungsinya.

Beberapa fungsi java yaitu java merupakan bahasa yang sederhana. Java dirancang
agar mudah dipelajari dan digunakan secara efektif. Java tidak menyediakan fitur-fitur rumit
bahasa pemrograman tingkat tinggi, serta banyak pekerjaan pemrograman yang mulanya
harus dilakukan manual, sekarang digantikan dikerjakan Java secara otomatis seperti
dealokasi memori.
PEMBAHASAN

 Kalkulator dengan Input Berbasis Teks


Kalkulator ini memiliki fungsi yaitu untuk melakukan operasi perhitunga n
matematik. Penggunaannya bisa dikatakan agak susah serta tidak memiliki interface
yang menarik dan bisa memudahkan penggunanya.
Program ini menggunakan class CalculatorInput, karena yang dibutuhka n
dari program ini adalah input-an angka dengan jenis pengoperasiannya. Input-inp ut
tersebut nantinya akan dihubungkan dengan class CalculatorEngine yang digunaka n
untuk pengolahan operasi perhitungan pada program ini. Berikut merupakan source
code yang digunakan untuk mengidentifikasi jenis pengoperasian perhitungan dari
class CalculatorInput:

if (c == '+') engine.add();
else if (c == '-') engine.subtract();
else if (c == '*') engine.multiply();
else if (c == '/') engine.divide();
else if (c >= '0' && c <= '9') engine.digit(c - '0');
else if (c == '=') engine.compute();
else if (c == 'c' || c == 'C') engine.clear();

Setelah pembuatan source code pada class CalculatorInput selesai, barulah


dilakukan pembuatan class CalculatorEngine. Berikut source code-nya:

o Untuk operasi penjumlahan menggunakan source code berikut ini:

char toDo;
void add(){
keep=value;
value=0;
toDo='+';

o Untuk operasi pengurangan menggunakan source code berikut ini :

void subtract(){
keep=value;
value=0;
toDo='-';

o Untuk operasi perkalian menggunakan source code berikut ini:

void multiply(){
keep=value;
value=0;
toDo='*';

o Untuk operasi pembagian menggunakan source code berikut ini:


void divide(){
keep=value;
value=0;
toDo='/';

o Lalu operasi pemrosesan data (=) dilakukan dengan source code berikut:

void compute(){
if(toDo=='+')
value = keep + value;
else if(toDo=='-')
value = keep - value;
else if(toDo=='*')
value = keep * value;
else if(toDo=='/')
value = keep / value;
keep =0;
 Kalkulator dengan Input Berbasis GUI
Untuk kalkulator berbasis GUI pertama-tama dimulai dengan membuat class
baru yaitu class CalculatorFrame, yang digunakan untuk membuat desain interface
sebuah kalkulator yang nantinya akan dihubungkan ke class CalculatorEngine yang
digunakan untuk operasi perhitungan sebuah kalkulator.
Lalu setelah membuat class untuk interface, langkah selanjutnya adalah
membuat interface untuk menginputkan angka serta jenis pengoperasiannya. Berikut
merupakan source code-nya:

CalculatorFrame(CalculatorEngine e) {
super("Calculator");
Panel top, bottom; Button b;

engine = e;
top = new Panel();
top.add(display = new TextField(20));
bottom = new Panel();
bottom.setLayout(new GridLayout(4,4));
bottom.add(b = new Button("1")); b.addActionListener(this);
bottom.add(b = new Button("2")); b.addActionListener(this);
bottom.add(b = new Button("3")); b.addActionListener(this);
bottom.add(b = new Button("+")); b.addActionListener(this);
bottom.add(b = new Button("4")); b.addActionListener(this);
bottom.add(b = new Button("5")); b.addActionListener(this);
bottom.add(b = new Button("6")); b.addActionListener(this);
bottom.add(b = new Button("-")); b.addActionListener(this);
bottom.add(b = new Button("7")); b.addActionListener(this);
bottom.add(b = new Button("8")); b.addActionListener(this);
bottom.add(b = new Button("9")); b.addActionListener(this);
bottom.add(b = new Button("*")); b.addActionListener(this);
bottom.add(b = new Button("C")); b.addActionListener(this);
bottom.add(b = new Button("0")); b.addActionListener(this);
bottom.add(b = new Button("=")); b.addActionListener(this);
bottom.add(b = new Button("/")); b.addActionListener(this);
setLayout(new BorderLayout());
add("North", top);
add("South", bottom) ;
addWindowListener(listener) ;
setSize(180, 160) ;
show();
}

Seperti yang dijelaskan sebelumnya, mula-mula class calculator frame


dihubungkan ke class calculator Engine. Lalu membuat desain kalkulator serta
membuat desain tombol yang nantinya digunakan untuk meletakkan angka-angka
dan jenis pengoperasian (+, - , * , / , =). Setelah membuat interface- nya, langkah
selanjutnya adalah mengisi nama variable dari setiap tombol. Berikut source code-
nya:
public void actionPerformed(ActionEvent e) {
char c = e.getActionCommand().charAt(0);
if (c == '+') engine.add();
else if (c == '-') engine.subtract();
else if (c == '*') engine.multiply();
else if (c == '/') engine.divide();
else if (c >= '0' && c <= '9') engine.digit(c - '0');
else if (c == '=') engine.compute();
else if (c == 'C') engine.clear();
display.setText(new Integer(engine.display()).toString());

Setelah meng-input-kan nama variable dari setiap tombol kalkulator, lalu


dilakukan konversi dari tipe data integer ke tipe data string untuk text field. Text
field berguna sebagai tempat untuk menuliskan input angka dan hasil operasi
perhitungan pada kalkulator. Tanpa text field, tentu kita tidak akan bisa
menggunakan kalkulator karena tidak ada tempat yang menampilkan hasil dari
operasi perhitungan yang kita lakukan dengan kalkulator.
Setelah pembuatan desain interface selesai, barulah pembuatan engine dari
kalkulator dilakukan. Berikut merupakan source code dari class CalculatorEngine:

o Untuk operasi penjumlahan menggunakan source code berikut ini:

char toDo;
void add(){
keep=value;
value=0;
toDo='+';

o Untuk operasi pengurangan menggunakan source code berikut ini:

void subtract(){
keep=value;
value=0;
toDo='-';

o Untuk operasi perkalian menggunakan source code berikut ini:

void multiply(){
keep=value;
value=0;
toDo='*';

o Untuk operasi pembagian menggunakan source code berikut ini:

void divide(){
keep=value;
value=0;
toDo='/';
}
o Lalu operasi pemrosesan data (=) dilakukan dengan source code berikut:

void compute(){
if(toDo=='+')
value = keep + value;
else if(toDo=='-')
value = keep - value;
else if(toDo=='*')
value = keep * value;
else if(toDo=='/')
value = keep / value;
keep =0;

You might also like