Professional Documents
Culture Documents
APIIT Diploma Part II: Individual Assignment
APIIT Diploma Part II: Individual Assignment
INDIVIDUAL ASSIGNMENT
Prepared By
Hirosh Tharaka D. M. D CB004149
Module Code
AAPP004-3-2-JP JAVA PROGRAMMING
Cohort
DF10A1ICT
Date of Submission
20th September 2011
Instructor
Dr. Dhananjay Kulkarni
Submitted in partial fulfilment for the degree of
Bachelor of Science (Hons) in Computing
JAVA PROGRAMMING
DF10A1ICT
Acknowledgement
It is with great pleasure I acknowledge all those who helped me in making this project a
success. Especially I would like to dedicate my sincere gratitude to our JAVA lecturer
Dr. Dhananjay Kulkarni for his continuous support and guidance and valuable advices
throughout the project. And also I would like to thank my friends who support me and
encourage me throughout this project.
I would also like to thank my parents who were always with me support me to fulfill my
career objectives. Last but not the least my gratitude goes to APIIT Lanka for providing
the laboratory and library facilities.
i|Page
JAVA PROGRAMMING
DF10A1ICT
Abstract
The main objective of this project is to develop a basic scientific calculator using JAVA
programming language. This Scientific Calculator is based on three operational levels.
Those are Standard Mode, Scientific Mode and Programmers Mode. Under these three
main categories all the basic functions which are related like addition, subtraction,
multiplication, division, complex mathematic functions, trigonometry conversions and
base conversions are included. Interface was designed according to a very user friendly
manner with gray-tone colors
ii | P a g e
JAVA PROGRAMMING
DF10A1ICT
Table of Contents
Acknowledgement...............................................................................................................i
Abstract ............................................................................................................................. ii
Table of Figures ................................................................................................................iv
1.
Introduction ................................................................................................................. 1
2.
a.
b.
Scientific ..................................................................................................................... 3
c.
Programmer ................................................................................................................ 4
d.
Other Functions........................................................................................................... 5
3.
4.
5.
a.
b.
i.
c.
i.
ii.
iii.
d.
e.
i.
History ...................................................................................................................... 40
ii.
Error .......................................................................................................................... 41
6.
7.
Assumptions.............................................................................................................. 49
8.
Limitations ................................................................................................................ 49
9. Bibliography................................................................................................................. 49
iii | P a g e
JAVA PROGRAMMING
DF10A1ICT
Table of Figures
Figure 1 : Menu Radio Selection ..................................................................................... 34
Figure 2 Standard Mode Calculation ............................................................................... 35
Figure 3. Scientific Mode : Sin Calculation ..................................................................... 36
Figure 4 Scientific Mode : ACOS Calculation ................................................................ 37
Figure 5 . Scientific Mode : Power Calculation ............................................................... 38
Figure 6 : Programmer Mode : Binary to Hexa-Decimal ................................................ 39
Figure 7. History .............................................................................................................. 40
Figure 8 . Error : Cos Range ............................................................................................ 41
Figure 9 Error : Division by Zero .................................................................................... 42
Figure 10. Error: Logarithm Error .................................................................................. 43
iv | P a g e
JAVA PROGRAMMING
DF10A1ICT
1. Introduction
The entire application which is a Scientific Calculator is developed, designed and coded
using Net beans 7.0.1. Most of the mathematical functions have been done using the
functions in math class in java. This calculator is based on three categories.
One is the standard mode which only has allows the user to deal with the ordinary day
to day calculations with simple addition, subtraction, multiplication, divisions and etc
Second is the scientific mode which allows the user to deal with most the scientific
calculations like trigonometry calculations and advanced mathematical calculations.
Third is the Programmer mode which allows the user to deal with few of the
programmers functions like conversions among bases of two, eight, ten and sixteen.
1|Page
JAVA PROGRAMMING
DF10A1ICT
2. Flow Charts
a. Standard Calculation
Start
IF STANDARD
MODE
ENTER
OPERAND_ONE
IF SYMBOL = +
IF SYMBOL = -
ENTER
OPERAND_TWO
IF SYMBOL = *
IF SYMBOL = /
IF SYMBOL = %
DISPLAY
ANSWER
STOP
2|Page
JAVA PROGRAMMING
DF10A1ICT
b. Scientific
Start
IF SCIENTIFIC
MODE
ENTER
OPERAND_ONE
IF Function =
SIN / COS / TAN
IF Function =
ASIN / ACOS /
ATAN-
ENTER
OPERAND_TWO
IF Function =
POW / ROOT
IF SYMBOL =
SQR, SQRT, CUBE,
CUBERT, LOG,
LOG10, EXP
STOP
DISPLAY
ANSWER
3|Page
JAVA PROGRAMMING
DF10A1ICT
c. Programmer
Start
IF Programmer
MODE
CHECK CURRENT
BASE
ENTER
OPERAND_ONE
TRUE
CONVERST TO
BINARY
TRUE
CONVERST TO
DECIMAL
IF BASE = BIN
IF BASE = DEC
IF BASE = OCT
TRUE
CONVERST TO
OCTAL
IF BASE = HEX
TRUE
CONVERST TO
HEX
DISPLAY
ANSWER
STOP
4|Page
JAVA PROGRAMMING
DF10A1ICT
d. Other Functions
Start
IF HELP
DISPAY
OUTPUT
IF HISTORY LOG
DISPAY
OUTPUT
IF MS
DISPAY
OUTPUT
IF MC
IF MR
IF M+
IF M-
IF C
CLEAR MEMORY
DISPAY
OUTPUT
STOP
SUBTRACT CURRENT
VALUE FROM THE
VALUE IN MEMORY
5|Page
JAVA PROGRAMMING
DF10A1ICT
3. Class Diagrams
MAIN CALCULATOR
BUTTONS
inputvar : int
i_operat : int
d_opr1 : double
ACTION
PERFORM
ED
d_opr2 : double
b_LastIsOperator: boolean
TEXT
FIELD
mode : int
PANELS
base :int
memory : double
message : String
dotFlag : int
Normal : int
Scientific : int
Programmer : int
6|Page
JAVA PROGRAMMING
DF10A1ICT
Binary :int
Octal :int
Decimal :int
Hexa :int
OPERATOR_NON :int
OPERATOR_ADD :int
OPERATOR_SUB :int
OPERATOR_MUL :int
OPERATOR_DIV :int
OPERATOR_POW :int
OPERATOR_ROOT :int
OPERATOR_INV :int
OPERATOR_PERC :int
OPERATOR_SQR :int
OPERATOR_CUBE :int
OPERATOR_SQRRT :int
OPERATOR_CUBERT :int
OPERATOR_ONEOVERX :int
OPERATOR_SIN : int
OPERATOR_COS : int
OPERATOR_TAN : int
7|Page
JAVA PROGRAMMING
DF10A1ICT
OPERATOR_ASIN : int
OPERATOR_ACOS : int
OPERATOR_ATAN : int
OPERATOR_TENTOTHEPOWER : int
OPERATOR_LOG : int
OPERATOR_LOG10 : int
OPERATOR_EXPO : int
old_base : int
tempvar : String
temp : int
lngth : int
sinv : double
cosv : double
tanv : double
asin : double
acos: double
atan : double
8|Page
JAVA PROGRAMMING
DF10A1ICT
4. Source Code
public class TEMPCALCView extends FrameView {
//power
//root
//(+-)
//(percentage)
JAVA PROGRAMMING
DF10A1ICT
private static int OPERATOR_TAN = 16;
private static int OPERATOR_ASIN = 17;
private static int OPERATOR_ACOS = 18;
private static int OPERATOR_ATAN = 19;
private static int OPERATOR_TENTOTHEPOWER = 20;
private static int OPERATOR_LOG = 21;
private static int OPERATOR_LOG10 = 22;
private static int OPERATOR_EXPO = 23;
10 | P a g e
JAVA PROGRAMMING
DF10A1ICT
private void jBtnNineActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setValue(9);
}
private void jBtnSixActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setValue(6);
}
private void jBtnSevenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setValue(7);
}
private void jBtnThreeActionPerformed(java.awt.event.ActionEvent evt) {
setValue(3);
// TODO add your handling code here:
}
private void jBtnFourActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setValue(4);
}
private void jBtnPlusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Operands();
i_operat = OPERATOR_ADD;
dotFlag = 0;
}
private void jBtnEqualActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Operands();
i_operat = OPERATOR_NON;
dotFlag = 0;
}
private void jBtnTwoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
setValue(2);
}
11 | P a g e
JAVA PROGRAMMING
DF10A1ICT
}
private void jBtnOneOverXActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//i_operat = OPERATOR_ONEOVERX;
OneOperand(OPERATOR_ONEOVERX);
}
private void jBtnSquareActionPerformed(java.awt.event.ActionEvent evt) {
12 | P a g e
JAVA PROGRAMMING
DF10A1ICT
// TODO add your handling code here:
// i_operat = OPERATOR_SQR;
OneOperand(OPERATOR_SQR);
}
private void jBtnCubeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//i_operat = OPERATOR_CUBE;
OneOperand(OPERATOR_CUBE);
}
private void jBtnPowActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Operands();
i_operat = OPERATOR_POW;
}
private void jBtnSquareRootActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//i_operat = OPERATOR_SQRRT;
OneOperand(OPERATOR_SQRRT);
}
private void jBtnCubeRootActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//i_operat = OPERATOR_CUBERT;
OneOperand(OPERATOR_CUBERT);
}
private void jBtnRootActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Operands();
i_operat = OPERATOR_ROOT;
}
private void jBtnSinActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
13 | P a g e
JAVA PROGRAMMING
DF10A1ICT
OneOperand(OPERATOR_SIN);
}
private void jBtnCosActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
OneOperand(OPERATOR_COS);
}
private void jBtnTanActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
OneOperand(OPERATOR_TAN);
}
private void jBtnASinActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
OneOperand(OPERATOR_ASIN);
}
private void jBtnACosActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
OneOperand(OPERATOR_ACOS);
}
private void jBtnATanActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
OneOperand(OPERATOR_ATAN);
}
private void jBtnTenToThePowerActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
OneOperand(OPERATOR_TENTOTHEPOWER);
}
private void jBtnDotActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if ( b_LastIsOperator ) // last is operator
jTxtEq.setText("");
if (dotFlag == 0)
14 | P a g e
JAVA PROGRAMMING
DF10A1ICT
{ temp = jTxtEq.getText();
if ("0".equals(temp))
{
jTxtEq.setText("0.");
//jTxtEq.setText(Integer.toString(val));
}
else
{
//inputvar = val;
jTxtEq.setText( jTxtEq.getText() + ".");
}
b_LastIsOperator = false;
dotFlag = 1;
}
}
private void jBtnLogActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
OneOperand(OPERATOR_LOG);
}
private void jBtnLog10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
OneOperand(OPERATOR_LOG10);
}
private void jRBM_NormalActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mode = Normal;
jLbl_Mode.setText("Standard Mode");
//jPanel_Programmer.enable(false);
//jPanel_Trigonometry.enable(false);
jBtnSin.setEnabled(false);
jBtnCos.setEnabled(false);
jBtnTan.setEnabled(false);
jBtnASin.setEnabled(false);
jBtnACos.setEnabled(false);
jBtnATan.setEnabled(false);
jBtnLog.setEnabled(false);
jBtnLog10.setEnabled(false);
jBtnSquare.setEnabled(false);
jBtnCube.setEnabled(false);
15 | P a g e
JAVA PROGRAMMING
DF10A1ICT
jBtnPow.setEnabled(false);
jBtnSquareRoot.setEnabled(false);
jBtnCubeRoot.setEnabled(false);
jBtnRoot.setEnabled(false);
jBtnOneOverX.setEnabled(false);
jBtnTenToThePower.setEnabled(false);
jBtnExpo.setEnabled(false);
jRB_BIN.setEnabled(false);
jRB_OCT.setEnabled(false);
jRB_DEC.setEnabled(false);
jRB_HEX.setEnabled(false);
jBtnA.setEnabled(false);
jBtnB.setEnabled(false);
jBtnC.setEnabled(false);
jBtnD.setEnabled(false);
jBtnE.setEnabled(false);
jBtnF.setEnabled(false);
jBtnDot.setEnabled(true);
jBtnPerc.setEnabled(true);
jBtnZero.setEnabled(true);
jBtnOne.setEnabled(true);
jBtnTwo.setEnabled(true);
jBtnThree.setEnabled(true);
jBtnFour.setEnabled(true);
jBtnFive.setEnabled(true);
jBtnSix.setEnabled(true);
jBtnSeven.setEnabled(true);
jBtnEight.setEnabled(true);
jBtnNine.setEnabled(true);
jBtnMemSave.setEnabled(true);
jBtnMemRecall.setEnabled(true);
jBtnMemClear.setEnabled(true);
jBtnMPus.setEnabled(true);
jBtnMMinus.setEnabled(true);
}
16 | P a g e
JAVA PROGRAMMING
DF10A1ICT
private void jRBM_ScientificActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mode = Scientific;
jLbl_Mode.setText("Scientific Mode");
//jPanel_Programmer.enable(false);
//jPanel_Trigonometry.enable(true);
jBtnSin.setEnabled(true);
jBtnCos.setEnabled(true);
jBtnTan.setEnabled(true);
jBtnASin.setEnabled(true);
jBtnACos.setEnabled(true);
jBtnATan.setEnabled(true);
jBtnLog.setEnabled(true);
jBtnLog10.setEnabled(true);
jBtnSquare.setEnabled(true);
jBtnCube.setEnabled(true);
jBtnPow.setEnabled(true);
jBtnSquareRoot.setEnabled(true);
jBtnCubeRoot.setEnabled(true);
jBtnRoot.setEnabled(true);
jBtnOneOverX.setEnabled(true);
jBtnTenToThePower.setEnabled(true);
jBtnExpo.setEnabled(true);
jRB_BIN.setEnabled(false);
jRB_OCT.setEnabled(false);
jRB_DEC.setEnabled(false);
jRB_HEX.setEnabled(false);
jBtnA.setEnabled(false);
jBtnB.setEnabled(false);
jBtnC.setEnabled(false);
jBtnD.setEnabled(false);
jBtnE.setEnabled(false);
jBtnF.setEnabled(false);
jBtnZero.setEnabled(true);
jBtnOne.setEnabled(true);
jBtnTwo.setEnabled(true);
jBtnThree.setEnabled(true);
jBtnFour.setEnabled(true);
jBtnFive.setEnabled(true);
jBtnSix.setEnabled(true);
17 | P a g e
JAVA PROGRAMMING
DF10A1ICT
jBtnSeven.setEnabled(true);
jBtnEight.setEnabled(true);
jBtnNine.setEnabled(true);
jBtnDot.setEnabled(true);
jBtnPerc.setEnabled(false);
jBtnMemSave.setEnabled(true);
jBtnMemRecall.setEnabled(true);
jBtnMemClear.setEnabled(true);
jBtnMPus.setEnabled(true);
jBtnMMinus.setEnabled(true);
}
private void jRBM_ProgrammerActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mode = Programmer;
base = Decimal;
jLbl_Mode.setText("Programmer Mode");
//jPanel_Programmer.enable(true);
//jPanel_Trigonometry.enable(false);
jBtnSin.setEnabled(false);
jBtnCos.setEnabled(false);
jBtnTan.setEnabled(false);
jBtnASin.setEnabled(false);
jBtnACos.setEnabled(false);
jBtnATan.setEnabled(false);
jBtnLog.setEnabled(false);
jBtnLog10.setEnabled(false);
jBtnSquare.setEnabled(false);
jBtnCube.setEnabled(false);
jBtnPow.setEnabled(false);
jBtnSquareRoot.setEnabled(false);
jBtnCubeRoot.setEnabled(false);
jBtnRoot.setEnabled(false);
jBtnOneOverX.setEnabled(false);
jBtnTenToThePower.setEnabled(false);
jBtnExpo.setEnabled(false);
jBtnDot.setEnabled(false);
jBtnPerc.setEnabled(false);
jRB_BIN.setEnabled(true);
18 | P a g e
JAVA PROGRAMMING
DF10A1ICT
jRB_OCT.setEnabled(true);
jRB_DEC.setEnabled(true);
jRB_HEX.setEnabled(true);
jBtnA.setEnabled(false);
jBtnB.setEnabled(false);
jBtnC.setEnabled(false);
jBtnD.setEnabled(false);
jBtnE.setEnabled(false);
jBtnF.setEnabled(false);
jBtnMemSave.setEnabled(false);
jBtnMemRecall.setEnabled(false);
jBtnMemClear.setEnabled(false);
jBtnMPus.setEnabled(false);
jBtnMMinus.setEnabled(false);
}
private void jRB_BINActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int old_base = base;
String tempvar = jTxtEq.getText();
int temp = Integer.parseInt((jTxtEq.getText()), base);
jTxtEq.setText(Integer.toBinaryString(temp));
base = Binary;
message = message + "\n " + tempvar + " base " + old_base + " = " +
Integer.toBinaryString(temp) + " base " + base ;
JAVA PROGRAMMING
DF10A1ICT
jBtnZero.setEnabled(true);
jBtnOne.setEnabled(true);
jBtnTwo.setEnabled(false);
jBtnThree.setEnabled(false);
jBtnFour.setEnabled(false);
jBtnFive.setEnabled(false);
jBtnSix.setEnabled(false);
jBtnSeven.setEnabled(false);
jBtnEight.setEnabled(false);
jBtnNine.setEnabled(false);
}
private void jRB_OCTActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int old_base = base;
String tempvar = jTxtEq.getText();
int temp = Integer.parseInt((jTxtEq.getText()), base);
jTxtEq.setText(Integer.toOctalString(temp));
base = Octal;
message = message + "\n " + tempvar + " base " + old_base + " = " +
Integer.toOctalString(temp) + " base " + base ;
jBtnA.setEnabled(false);
jBtnB.setEnabled(false);
jBtnC.setEnabled(false);
jBtnD.setEnabled(false);
jBtnE.setEnabled(false);
jBtnF.setEnabled(false);
jBtnZero.setEnabled(true);
jBtnOne.setEnabled(true);
jBtnTwo.setEnabled(true);
jBtnThree.setEnabled(true);
jBtnFour.setEnabled(true);
jBtnFive.setEnabled(true);
jBtnSix.setEnabled(true);
jBtnSeven.setEnabled(true);
jBtnEight.setEnabled(false);
jBtnNine.setEnabled(false);
}
20 | P a g e
JAVA PROGRAMMING
DF10A1ICT
jBtnA.setEnabled(false);
jBtnB.setEnabled(false);
jBtnC.setEnabled(false);
jBtnD.setEnabled(false);
jBtnE.setEnabled(false);
jBtnF.setEnabled(false);
jBtnZero.setEnabled(true);
jBtnOne.setEnabled(true);
jBtnTwo.setEnabled(true);
jBtnThree.setEnabled(true);
jBtnFour.setEnabled(true);
jBtnFive.setEnabled(true);
jBtnSix.setEnabled(true);
jBtnSeven.setEnabled(true);
jBtnEight.setEnabled(true);
jBtnNine.setEnabled(true);
}
private void jRB_HEXActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int old_base = base;
String tempvar = jTxtEq.getText();
int temp = Integer.parseInt((jTxtEq.getText()), base);
jTxtEq.setText(Integer.toHexString(temp));
base = Hexa;
21 | P a g e
JAVA PROGRAMMING
DF10A1ICT
message = message + "\n " + tempvar + " base " + old_base + " = " +
Integer.toHexString(temp) + " base " + base ;
jBtnA.setEnabled(true);
jBtnB.setEnabled(true);
jBtnC.setEnabled(true);
jBtnD.setEnabled(true);
jBtnE.setEnabled(true);
jBtnF.setEnabled(true);
jBtnZero.setEnabled(true);
jBtnOne.setEnabled(true);
jBtnTwo.setEnabled(true);
jBtnThree.setEnabled(true);
jBtnFour.setEnabled(true);
jBtnFive.setEnabled(true);
jBtnSix.setEnabled(true);
jBtnSeven.setEnabled(true);
jBtnEight.setEnabled(true);
jBtnNine.setEnabled(true);
}
private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jBtnPercActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Operands();
i_operat = OPERATOR_PERC;
}
private void jBtnExpoActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
OneOperand(OPERATOR_EXPO);
}
private void jBtnMemSaveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
memory = Double.parseDouble(jTxtEq.getText());
jLblMemory.setText(Double.toString(memory));
}
private void jBtnMemClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
memory = 0;
22 | P a g e
JAVA PROGRAMMING
DF10A1ICT
jLblMemory.setText("Empty");
}
private void jBtnMPusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//M + y
b_LastIsOperator = true;
d_opr2 = Double.parseDouble(jTxtEq.getText());
d_opr2 = memory + d_opr2;
jTxtEq.setText(Double.toString(d_opr2));
i_operat = OPERATOR_ADD;
}
private void jBtnMMinusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// M - y
b_LastIsOperator = true;
d_opr2 = Double.parseDouble(jTxtEq.getText());
d_opr2 = memory - d_opr2;
jTxtEq.setText(Double.toString(d_opr2));
i_operat = OPERATOR_SUB;
}
private void jBtnHLogActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//linenum++;
//message = message + "\nLine" + Integer.toString(linenum) + " : " + "message" ;
JOptionPane.showMessageDialog(null, message);
}
private void jBtnAllClearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jTxtEq.setText("0");
d_opr1 = 0;
d_opr2 = 0;
i_operat = OPERATOR_NON;
message = "History : ";
memory = 0;
jLblMemory.setText("Empty");
dotFlag = 0;
}
23 | P a g e
JAVA PROGRAMMING
DF10A1ICT
JAVA PROGRAMMING
DF10A1ICT
jTxtEq.setText("C");
//jTxtEq.setText(Integer.toString(val));
}
else
{
//inputvar = val;
jTxtEq.setText( jTxtEq.getText() + "C");
}
b_LastIsOperator = false;
}
private void jBtnDActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if ( b_LastIsOperator ) // last is operator
jTxtEq.setText("");
temp = jTxtEq.getText();
if ("0".equals(temp))
{
jTxtEq.setText("D");
//jTxtEq.setText(Integer.toString(val));
}
else
{
//inputvar = val;
jTxtEq.setText( jTxtEq.getText() + "D");
}
b_LastIsOperator = false;
}
private void jBtnEActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if ( b_LastIsOperator ) // last is operator
jTxtEq.setText("");
temp = jTxtEq.getText();
if ("0".equals(temp))
{
jTxtEq.setText("E");
//jTxtEq.setText(Integer.toString(val));
}
else
{
//inputvar = val;
jTxtEq.setText( jTxtEq.getText() + "E");
}
25 | P a g e
JAVA PROGRAMMING
DF10A1ICT
b_LastIsOperator = false;
}
private void jBtnFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if ( b_LastIsOperator ) // last is operator
jTxtEq.setText("");
temp = jTxtEq.getText();
if ("0".equals(temp))
{
jTxtEq.setText("F");
//jTxtEq.setText(Integer.toString(val));
}
else
{
//inputvar = val;
jTxtEq.setText( jTxtEq.getText() + "F");
}
b_LastIsOperator = false;
}
private void jBtnDelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String temp2 = jTxtEq.getText();
int lngth = temp2.length();
temp2 = temp2.substring( 0 , lngth - 1);
jTxtEq.setText(temp2);
//temp2 = txt_ans.getText();
//leng = temp2.length();
//temp2 = temp2.substring(0, leng - 1);
//txt_ans.setText(temp2);
}
private void jBtnMemRecallActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//memory = Double.parseDouble(jTxtEq.getText());
jTxtEq.setText(Double.toString(memory));
//jLblMemory.setText(Double.toString(memory));
}
26 | P a g e
JAVA PROGRAMMING
DF10A1ICT
//FUNCTIONS =========
private void OneOperand (int temp_operator)
{
b_LastIsOperator = true;
d_opr2 = Double.parseDouble(jTxtEq.getText());
d_opr2 = evaluateValue(0,d_opr2, temp_operator);
jTxtEq.setText(Double.toString(d_opr2));
}
private void Operands ()
{
b_LastIsOperator = true;
//selection
{
d_opr2 = Double.parseDouble(jTxtEq.getText());
d_opr1 = evaluateValue(d_opr1, d_opr2, i_operat);
jTxtEq.setText(Double.toString(d_opr1));
}
}
private void setValue (int val)
{
if ( b_LastIsOperator ) // LastIsOperator
jTxtEq.setText("");
temp = jTxtEq.getText();
if ("0".equals(temp))
{
jTxtEq.setText(Integer.toString(val));
}
else
{
inputvar = val;
jTxtEq.setText( jTxtEq.getText() + inputvar);
}
b_LastIsOperator = false;
}
JAVA PROGRAMMING
DF10A1ICT
DecimalFormat twoDForm = new DecimalFormat("#.####");
switch(operator)
{
case 1 :
message = message + "\n" + Double.toString(x) + " + " + Double.toString(y)
+ " = " + (x+y);
return x + y ;
case 2 :
message = message + "\n" + Double.toString(x) + " - " + Double.toString(y)
+ " = " + (x-y);
return x - y ;
case 3 :
message = message + "\n" + Double.toString(x) + " * " + Double.toString(y)
+ " = " + (x*y);
return x * y ;
case 4 :
if (y == 0)
{
JOptionPane.showMessageDialog(null, "Error : Dividign By Zero ");
return 0;
}
else
{
message = message + "\n" + Double.toString(x) + " / " +
Double.toString(y) + " = " + (x/y);
return x / y ;
}
case 5 :
message = message + "\n" + Double.toString(x) + " ^ " + Double.toString(y)
+ " = " + (Math.pow(x,y));
return (Math.pow(x,y)) ;
case 6 :
if (x < 0)
{
JOptionPane.showMessageDialog(null, "Error : No Root for Negative
values ");
return 0;
}
else
{
28 | P a g e
JAVA PROGRAMMING
DF10A1ICT
message = message + "\n" + Double.toString(y) + " " +
Double.toString(x) + " = " + (Math.pow(x,(1/y)));
return (Math.pow(x,(1/y)));
}
case 7 :
message = message + "\n" + Double.toString(y) + " " + " = " + (y*-1);
return (y*-1) ;
case 8 :
if (y == 0)
{
JOptionPane.showMessageDialog(null, "Error : Dividign By Zero ");
return 0;
}
else
{
message = message + "\n" + Double.toString(x) + " % " +
Double.toString(y) + " = " + ((x/y)*100);
return (x/y)*100; // percentage
}
case 9 :
message = message + "\n" + Double.toString(y) + " ^ " + Double.toString(2)
+ " = " + (Math.pow(y,2));
return (Math.pow(y,2));
case 10:
message = message + "\n" + Double.toString(y) + " ^ " + Double.toString(3)
+ " = " + (Math.pow(y,3));
return (Math.pow(y,3));
case 11:
if (y < 0)
{
JOptionPane.showMessageDialog(null, "Error : No Squareroot for
Negative values ");
return 0;
}
else
{
message = message + "\n" + Double.toString(2) + " " +
Double.toString(y) + " = " + (Math.sqrt(y));
return (Math.sqrt(y));
}
case 12:
29 | P a g e
JAVA PROGRAMMING
DF10A1ICT
message = message + "\n" + Double.toString(3) + " " + Double.toString(y)
+ " = " + (Math.cbrt(y));
return (Math.cbrt(y));
case 13:
if (y == 0)
{
JOptionPane.showMessageDialog(null, "Error : Dividing By Zero");
return 0;
}
else
{
message = message + "\n" + Double.toString(1) + " / " +
Double.toString(y) + " = " + (1/y);
return (1/y) ;
}
case 14:
double sinv = Math.sin(Math.toRadians(y));
//DecimalFormat twoDForm = new DecimalFormat("#.####");
sinv = Double.valueOf(twoDForm.format(sinv));
message = message + "\n Sin " + Double.toString(y) + " = " + sinv;
return sinv;
//return (Math.sin(Math.toRadians(y)));
case 15:
double cosv = Math.cos(Math.toRadians(y));
//DecimalFormat twoDForm = new DecimalFormat("#.####");
cosv = Double.valueOf(twoDForm.format(cosv));
message = message + "\n Cos " + Double.toString(y) + " = " + cosv;
return cosv;
//return (Math.cos(Math.toRadians(y)));
case 16:
//if (y == 90)
//{
//invalid input
// jTxtEq.setText("Invalid Input");
//return 0;
// }
//else
{
double tanv = Math.tan(Math.toRadians(y));
//DecimalFormat twoDForm = new DecimalFormat("#.####");
tanv = Double.valueOf(twoDForm.format(tanv));
30 | P a g e
JAVA PROGRAMMING
DF10A1ICT
message = message + "\n Tan " + Double.toString(y) + " = " + tanv;
return tanv;
}
// (Math.tan(Math.toRadians(y)));
case 17:
if ( y < -1 || y > 1)
{
JOptionPane.showMessageDialog(null, "Error : Sin Value Is Out of
Range ( Sin : -1 ~ 1 ) ");
return 0;
}
else
{
double asin = Math.toDegrees(Math.asin(y));
asin = Double.valueOf(twoDForm.format(asin));
message = message + "\n ASin " + Double.toString(y) + " = " + asin;
return asin;
}
case 18:
if ( y < -1 || y > 1)
{
JOptionPane.showMessageDialog(null, "Error : Cos Value Is Out of
Range ( Cos : -1 ~ 1 ) ");
return 0;
}
else
{
double acos = Math.toDegrees(Math.acos(y));
acos = Double.valueOf(twoDForm.format(acos));
message = message + "\n ACos " + Double.toString(y) + " = " + acos;
return acos;
}
//message = message + "\n ACos " + Double.toString(y) + " = " +
(Math.acos(y));
//return (Math.acos(y));
case 19:
double atan = Math.toDegrees(Math.atan(y));
atan = Double.valueOf(twoDForm.format(atan));
message = message + "\n ATan " + Double.toString(y) + " = " + atan;
return atan;
//message = message + "\n ATan " + Double.toString(y) + " = " +
(Math.atan(y));
// return (Math.atan(y));
31 | P a g e
JAVA PROGRAMMING
DF10A1ICT
case 20:
message = message + "\n" + Double.toString(10) + " ^ " + Double.toString(y)
+ " = " + (Math.pow(10,y));
return (Math.pow(10, y));
case 21:
if ( y <= 0)
{
JOptionPane.showMessageDialog(null, "Error : Cannot find log of a
negative value ");
return 0;
}
else
{
message = message + "\n log " + Double.toString(y) + " = " + (Math.log(y));
return (Math.log(y));
}
case 22:
if ( y <= 0)
{
JOptionPane.showMessageDialog(null, "Error : Cannot find log of a
negative value ");
return 0;
}
else
{
message = message + "\n log10 " + Double.toString(y) + " = " +
(Math.log10(y));
return (Math.log10(y));
}
case 23:
message = message + "\n Exp " + Double.toString(y) + " = " +
(Math.exp(y));
return (Math.exp(y));
case 24:
case 25:
case 26:
case 27:
case 28:
case 29:
case 30:
32 | P a g e
JAVA PROGRAMMING
DF10A1ICT
default : return y;
}
}
33 | P a g e
JAVA PROGRAMMING
DF10A1ICT
5. Sample Outputs
a. Main Menu Selection
The above figure shows the main menu with three radio buttons where the user can
select which mode the user going to use. And the short cut keys are assigned for it.
Normal : Ctrl + N
Scientific : Ctrl + S
Programmer : Ctrl + P
34 | P a g e
JAVA PROGRAMMING
DF10A1ICT
b. Standard Mode
i. Addition, Subtraction, Multiplication, Division,
Percentage
The above figure is the result of addition of two numbers. Once the user click the
buttons in followings sequence fended with an equal sign, the result will be displayed in
the screen.
JAVA PROGRAMMING
DF10A1ICT
The above answer in the calculator is calculated for 9 + 6.
c. Scientific Mode
i. Sin, Cos, Tan
The above figure is the for the Sin value of Degree 30. Once the user click the buttons in
followings sequence fended with an equal sign, the result will be displayed in the screen.
36 | P a g e
JAVA PROGRAMMING
DF10A1ICT
37 | P a g e
JAVA PROGRAMMING
DF10A1ICT
The above figure is for the Cos inverse of 0.5. Once the user click the buttons in
followings sequence fended with an equal sign, the result will be displayed in the screen.
(Trigonometry Value)(Anti Trigonometry Operation) (=)
The above answer in the calculator is calculated for 0.5 Acos = 60
38 | P a g e
JAVA PROGRAMMING
DF10A1ICT
The above figure is for the 2 to the power of 6. Once the user click the buttons in
followings sequence fended with an equal sign, the result will be displayed in the screen.
(Operand One)(Advanced Mathematical Function) (Operand Two)( (=)
The above answer in the calculator is calculated for 2^6 = 64.
d. Programmer Mode
The above calculation is done for the conversion from base if two ( binary ) to base of
sixteen (16). The above calculation is 111102 >1e16
39 | P a g e
JAVA PROGRAMMING
DF10A1ICT
e. Other Options
i. History
Figure 7. History
The above figure shows the preview of how the history will be viewed as the user
pressed the History log
40 | P a g e
JAVA PROGRAMMING
DF10A1ICT
ii. Error
The above error shows when the user tries to get the Cos invers value of any value
which is larger than 1 on less than -1. In the same way it works for sin inverse.
41 | P a g e
JAVA PROGRAMMING
DF10A1ICT
Once the user tries to divide any value from zero, it throws the dvision by zero like
shown in the above figure.
42 | P a g e
JAVA PROGRAMMING
DF10A1ICT
The above error throws from the calculator when the user tries to find the log value of a
negative value.
43 | P a g e
JAVA PROGRAMMING
DF10A1ICT
6. Test Plans
Test
No.
Function
PreConditions
1.
Addition
At least one
numeric
character must
be entered
before clicking
+
2.
3.
4.
5.
6.
Subtraction
Multiplication
Division
Power
Root
At least one
numeric
character must
be entered
before clicking
-
At least one
numeric
character must
be entered
before clicking
*
At least one
numeric
character must
be entered
before clicking
*
At least one
numeric
character must
be entered
before clicking
^
At least one
numeric
character must
be entered
Test Steps
-
Enter first
operand
Click +
Enter the
second
operand
Click =
Enter first
operand
Click -
Enter the
second
operand
Click =
Enter first
operand
Click *
Enter the
second
operand
Click =
Enter first
operand
Click /
Enter the
second
operand
Click =
Enter first
operand
Click ^
Enter the
second
operand
Click =
Enter first
operand
Click
Enter the
Input
Output
56+4
6.0
9-3
6.0
8*3
24.0
20/5
4.0
3 x^y 3
27.0
2 yx 9
3.0
44 | P a g e
JAVA PROGRAMMING
DF10A1ICT
before clicking
7.
8.
9.
Exponential
Percentage
Decimal
At least one
numeric
character must
be entered
before clicking
exp
At least one
number should
be entered
before clicking
%
At least one
number should
be entered
before clicking
DEC
10.
11.
Hexadecimal
Octal
At least one
number should
be entered
before clicking
HEX
At least one
number should
be entered
before clicking
OCT
12.
Binary
At least one
number should
be entered
before clicking
BIN
second
operand
Click =
Enter first
operand
Click exp
Click =
Enter first
operand
Click %
Enter the
second
operand
Click =
Enter a
binary, octal
or
hexadecimal
value
Click on
DEC Radio
Button
Enter a
binary, octal
or decimal
value
Click on
HEX Radio
Button
Enter a
binary,
hexadecimal
or decimal
value
Click on
OCT Radio
Button
Enter a
binary, octal
or decimal
value
Click on
BIN Radio
2 exp
7.389
2%5
10
1001
15
1111
17
15
1111
45 | P a g e
JAVA PROGRAMMING
DF10A1ICT
13.
14.
Clear (CE)
15.
History Log
16.
Unary
17.
Sin
18.
Cosin
19.
Tan
20.
Sine Inverse
21.
Cosine
Inverse
At least one
number should
be entered
before clicking
CE
-
At least one
number should
be entered
before clicking
At least one
number should
be entered in
degrees before
clicking Sin
At least one
number should
be entered in
degrees before
clicking Cos
At least one
number should
be entered in
degrees before
clicking Tan
At least one
number should
be entered in
radian before
clicking ASin
At least one
number should
be entered in
radian before
clicking
ACos
Button
Click on C to
clear the display
and all data
stored in the
memory
Click on CE to
the value in
display
Click on
History to view
calculation log
- Enter a value
- Click on
1256 C
1234 CE
5.0+3.0=9
Cos 60.0=0.5
95
-95.0
Enter a value
Click on
Sin
30 sin
0.5
Enter a value
Click on
Cos
60 cos
0.5
Enter a value
Click on
Tan
45 tan
Enter a value
Click on
Asin
0.5 Asin
30.0
Enter a value
Click on
Acos
0.5 Acos
60.0
46 | P a g e
JAVA PROGRAMMING
DF10A1ICT
22.
Tangent
Inverse
23.
Square
24.
Cube
25.
Square Root
26.
Cube Root
27.
10^x
28.
1/x
29
log
30
log10
At least one
number should
be entered in
radian before
clicking
ATan
At least one
number must
be entered
before clicking
x^2
At least one
number must
be entered
before clicking
x^3
At least one
number must
be entered
before clicking
2x
At least one
number must
be entered
before clicking
3x
At least one
number must
be entered
before clicking
10^x
At least one
number must
be entered
before clicking
1/x
At least one
number must
be entered
before clicking
log
At least one
number must
be entered
before clicking
Enter a value
Click on
Atan
1 Atan
45
Enter a value
Click on
x^2
5 x^2
25.0
Enter a value
Click on
x^3
3 x^3
27.0
Enter a value
Click on 2x
16 2x
4.0
Enter a value
Click on
3x
8 3x
2.0
Enter a value
Click on
10^x
10 ^ 4
10000
Enter a value
Click on
1/x
1 1/x 5
0.25
Enter a value
Click on
log
50 log
1.6989
Enter a value
Click on
log10
2 log10
0.3010
47 | P a g e
JAVA PROGRAMMING
DF10A1ICT
31.
Memory Save
32.
Memory
Recall
33.
Memory
Clear
Memory Add
34
35
Memory
Subtract
log10
At least one
number must
be entered or
calculate to
store before
clicking MS
At least one
number must
be entered or
calculate to
store before
clicking M+
At least one
number must
be entered or
calculate to
store before
clicking M-
Click on MS
5.0
Click on MR
4.0
Click on MC
Enter a value
or calculation
Click on
M+
If M = 65.0
70
Enter a value
or calculation
Click on M
If M = 65.0
5 M+
-60
5 M-
48 | P a g e
JAVA PROGRAMMING
DF10A1ICT
7. Assumptions
8. Limitations
All the inputs are allowed to input in Degrees for sin, cos, tan.
All the Asin, Acos and Atan give the output from degrees.
Not compatible with brackets.
Calculations in Binary, Octal, Decimal and Hex-Decimal are not allowed, only the
conversions are allowed among those bases.
9. Bibliography
Anon., n.d. Java 2 Platform Standard Ed. 5.0. [Online] Available at:
http://download.oracle.com/javase/1,5,0/docs/api/java/lang/Math.html [Accessed 15
September 2011].
49 | P a g e