Calculator
Calculator
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<APPLET CODE="Calculator.class" WIDTH=205 HEIGHT=210>
</APPLET>
*/
/**************************
Data Members
***************************/
private TextField Panel; //To accept user input and display result
private Button Button0, //This button to pass 0 to panel
Button1, //This button to pass 1 to panel
Button2, //This button to pass 2 to panel
Button3, //This button to pass 3 to panel
Button4, //This button to pass 4 to panel
Button5, //This button to pass 5 to panel
Button6, //This button to pass 6 to panel
Button7, //This button to pass 7 to panel
Button8, //This button to pass 8 to panel
Button9, //This button to pass 9 to panel
ButtonDiv , //This button operate division
ButtonMul, //This button operate multiplication
ButtonAdd, //This button operate addition
ButtonSub, //This button opearte subtraction
ButtonAss, //This button return result
ButtonOpar, //This button call open parentheses
ButtonCpar, //This button call close parentheses
ButtonPoint, //This button to pass . to panel
ButtonClear; //This button clear panel
private int Flag, //Record the state after calculating
Err_lve; //State the error level
/*************************
Initialize similar like Constructor, that is use for Applet
**************************/
Flag = 0;
Err_lve = 0;
Panel.addActionListener(this);
Button0.addActionListener(this);
Button1.addActionListener(this);
Button2.addActionListener(this);
Button3.addActionListener(this);
Button4.addActionListener(this);
Button5.addActionListener(this);
Button6.addActionListener(this);
Button7.addActionListener(this);
Button8.addActionListener(this);
Button9.addActionListener(this);
ButtonDiv.addActionListener(this);
ButtonMul.addActionListener(this);
ButtonAdd.addActionListener(this);
ButtonSub.addActionListener(this);
ButtonAss.addActionListener(this);
ButtonOpar.addActionListener(this);
ButtonCpar.addActionListener(this);
ButtonPoint.addActionListener(this);
ButtonClear.addActionListener(this);
}
/*
Method: paint
Purpose: Draw the outline of calculator *(this method can auto. draw when develop Applet, but it isn't Applet's method)
Parameters: Graphics
Returns: None
*/
/*
Method: actionPerformed
Purpose: Event-handling
Parameters: Action event
Returns: None
*/
/*
Method: ClearPanel
Purpose: Clear Panel TextField objects
Parameters: None
Returns: None
*/
/*
Method: Compute
Purpose: Calculate the expression and shown the result, when the user hit return key or "=" button.
Parameters: The whole expression
Returns: None
*/
Tmpstr = Panel.getText();
no_of_char = Tmpstr.length();
if (no_of_par != 0) Err_lve = 1; //If open and close parentheses do not match return error
if (Err_lve == 0)
{
//Checking Parentheses
for (index = 0; index < no_of_cha; index++)
{
cha = result.charAt(index);
if (cha == '(')
{
if (pare_match == 0) op_index = index;
pare_match ++;
no_of_pare ++;
}
if (cha == ')')
{
pare_match --;
if (pare_match == 0) cp_index = index;
}
}//End of checking Parentheses
//Implement algorithm
if (no_of_pare == 0 && Err_lve == 0)
{
if ( (!(Character.isDigit(result.charAt(0))) && (result.charAt(0) != '-')) || !
(Character.isDigit(result.charAt(result.length()-1))) ) Err_lve = 3;
if (f_index > 2)
if ( ((result.charAt(f_index-1)) == '-') &&
((result.charAt(f_index-2)) == 'E') )
f_index = f_index - 2;
r_index = index + 1;
while ( (r_index < result.length()-1) &&
((Character.isDigit(result.charAt(r_index+1))) || ((result.charAt(r_index+1)) == '.') || ((result.charAt(r_index+1)) == 'E')) )
{
r_index++;
if (r_index < result.length()-2)
if ( ((result.charAt(r_index)) == 'E') &&
((result.charAt(r_index+1)) == '-') )
r_index++;
}
r_operand = result.substring(index+1, r_index+1);
if (index > 0)
if ( ((result.charAt(index)) == 'E') && ((result.charAt(index+1)) == '-') )
index = index + 2;
cha = result.charAt(index);
r_index = index + 1;
while ( (r_index < result.length()-1) &&
((Character.isDigit(result.charAt(r_index+1))) || ((result.charAt(r_index+1)) == '.') || ((result.charAt(r_index+1)) == 'E')) )
{
r_index++;
if (r_index < result.length()-2)
if ( ((result.charAt(r_index)) == 'E') &&
((result.charAt(r_index+1)) == '-') )
r_index++;
}
r_operand = result.substring(index+1, r_index+1);
result = Algorithm(cha, f_operand, r_operand) +
result.substring(r_index+1);
index = 0;
}
}
}//End of implement add and subtract
}
return result;
}
/*
Method: Algorithm
Purpose: Implement the simple expression
Parameters: Operator, front value and rear value
Returns: None
*/
F = new Double(F_operand);
R = new Double(R_operand);
f = F.doubleValue();
r = R.doubleValue();
res = Double.toString(ans);
return res;
}
/*
Method: Err_msg
Purpose: Prompt error message
Parameters: Error level
Returns: None
*/
Submitted by:
Jefferson DC. Esguerra
Reynalyn R. Manapat
BSIT – 2b
Submitted to:
Mr. Michael Dennis Perez
Description
Calculator.java is a simple applet that calculates the arithmetic expression from the
text field and returns the result to the text field. The user can enter the expression by typing
within the text field and/or by clicking the buttons.
Note that the arithmetic expression can have numbers with a decimal point, the nested
parentheses and the four binary arithmetic operators, such as addition (+), subtraction (-),
multiplication (*), and division (/). All the numbers entered can be accepted as
positive/negative floating point numbers.
Output
The applet begins with three import statements. The first imports the Abstract
Window Toolkit (AWT) classes which contains support for a window-based, graphical
interface. Applets interact with the user through the AWT, not through the console-based I/O
classes. Next is the java.awt.event package which is capable of handling events and lastly is
the applet package which contains the class Applet. Every applet that you create must be a
subclass of Applet.
The next line is an HTML text file that contains the appropriate Applet tag for the
program to execute an applet in the web browser or in an appletviewer. The program declares
the class Calculator. This class must be declared as public because it will be accessed by
outside code.
Inside class Calculator is the declaration of variables used in the applet and after
that is the initialization of similar like Constructor, which is use for Applet. It sets its
background and layout and the bounds of the buttons used in the application in the applet
environment. It contains addActionListener that registers events each time there was a button
that is pressed or clicked.
public void paint(Graphics graphic) contains the graphics, colors and draw the
outline of the calculator. When the button is clicked, a call is made to the actionPerformed()
method defined in the class of the listener object. An ActionEvent object is passed as a
parameter to it actionPerformed ( ). There is also a method to clear the panel after performing
any operation when you want to enter another expression. The command will output a “ “ that
indicates that there is no value in the text field.
The next method is the DisplayEx ( ) method. Its purpose is to display the whole
expression that the user enters. After it is the Compute ( ) method that calculate the
expressions and show the result when the user hit the return key or the equal (=) button. Next
is the implementation of the expression. The last part of the program is when there was an
invalid entry in the text field, an error message will prompt. For example, if you enter an
invalid expression in the text field, “Invalid expression” will appear on the panel.