JAVA Programming Assignment (Scientific Calculator) - Hirosh Tharaka (Trazor)
JAVA Programming Assignment (Scientific Calculator) - Hirosh Tharaka (Trazor)
INDIVIDUAL ASSIGNMENT
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
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
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
Table of Contents
Acknowledgement...............................................................................................................i Abstract ............................................................................................................................. ii Table of Figures ................................................................................................................iv 1. 2. a. b. c. d. 3. 4. 5. a. b. i. c. i. ii. iii. d. e. i. ii. 6. 7. 8. Introduction ................................................................................................................. 1 Flow Charts ................................................................................................................. 2 Standard Calculation ................................................................................................... 2 Scientific ..................................................................................................................... 3 Programmer ................................................................................................................ 4 Other Functions........................................................................................................... 5 Class Diagrams ........................................................................................................... 6 Source Code ................................................................................................................ 9 Sample Outputs ......................................................................................................... 34 Main Menu Selection ................................................................................................ 34 Standard Mode .......................................................................................................... 35 Addition, Subtraction, Multiplication, Division, Percentage.................................... 35 Scientific Mode ......................................................................................................... 36 Sin, Cos, Tan............................................................................................................. 36 Asin, Acos, Atan ....................................................................................................... 37 Square, Square root, Cube, Cube Root, Root, Power ........................................... 38 Programmer Mode .................................................................................................... 39 Other Options ............................................................................................................ 40 History ...................................................................................................................... 40 Error .......................................................................................................................... 41 Test Plans .................................................................................................................. 44 Assumptions.............................................................................................................. 49 Limitations ................................................................................................................ 49
9. Bibliography................................................................................................................. 49
iii | P a g e
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
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
IF STANDARD MODE
ENTER OPERAND_ONE
IF SYMBOL = +
IF SYMBOL = -
IF SYMBOL = /
IF SYMBOL = %
2|Page
b. Scientific
Start
IF SCIENTIFIC MODE
ENTER OPERAND_ONE
STOP
DISPLAY ANSWER
3|Page
c. Programmer
Start
IF Programmer MODE
ENTER OPERAND_ONE
CONVERST TO BINARY
CONVERST TO DECIMAL
IF BASE = OCT
TRUE
CONVERST TO OCTAL
IF BASE = HEX
TRUE
CONVERST TO HEX
4|Page
d. Other Functions
Start
IF HELP
DISPAY OUTPUT
IF HISTORY LOG
DISPAY OUTPUT
IF MS
DISPAY OUTPUT
IF MC
CLEAR MEMORY
IF MR
DISPAY OUTPUT
STOP
IF M+
IF M-
IF C
5|Page
3. Class Diagrams
MAIN CALCULATOR inputvar : int i_operat : int d_opr1 : double d_opr2 : double b_LastIsOperator: boolean TEXT FIELD mode : int PANELS base :int memory : double message : String ACTION PERFORM ED
BUTTONS
dotFlag : 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
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
4. Source Code
public class TEMPCALCView extends FrameView {
int inputvar , i_operat; String temp; double d_opr1, d_opr2; //boolean b_clrtxt; boolean b_LastIsOperator; int mode = 0; //default normal mode int base = 10; //default decimal mode double memory; String message = "History : "; int dotFlag = 0; //int linenum;
private static int Normal = 0; private static int Scientific = 1; private static int Programmer = 2; private static int Binary = 2; private static int Octal = 8; private static int Decimal = 10; private static int Hexa = 16; private static int OPERATOR_NON = 0; private static int OPERATOR_ADD = 1; private static int OPERATOR_SUB = 2; private static int OPERATOR_MUL = 3; private static int OPERATOR_DIV = 4; private static int OPERATOR_POW = 5; private static int OPERATOR_ROOT = 6; private static int OPERATOR_INV = 7 ; private static int OPERATOR_PERC = 8;
private static int OPERATOR_SQR = 9; //square private static int OPERATOR_CUBE = 10; // cube private static int OPERATOR_SQRRT = 11; //squareroot private static int OPERATOR_CUBERT = 12; private static int OPERATOR_ONEOVERX = 13; //oneoverX private static int OPERATOR_SIN = 14; private static int OPERATOR_COS = 15; 9|Page
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;
private void jBtnClearActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: jTxtEq.setText("0"); d_opr1 = 0; d_opr2 = 0; i_operat = OPERATOR_NON; dotFlag = 0; } private void jBtnEightActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: setValue(8); } private void jBtnMinusActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Operands(); i_operat = OPERATOR_SUB; dotFlag = 0; } private void jBtnOneActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: setValue(1); }
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
private void jBtnFiveActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: setValue(5); } private void jBtnMultiActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Operands(); i_operat = OPERATOR_MUL; dotFlag = 0; } private void jBtnDivideActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Operands(); i_operat = OPERATOR_DIV; dotFlag = 0; } private void jBtnZeroActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: setValue(0); } private void jBtnPlusMinusActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: OneOperand(OPERATOR_INV); // <==evaluateTwoOperators //i_operat = OPERATOR_INV; // double tmp = Double.parseDouble(jTxtEq.getText()); // jTxtEq.setText(Double.toString(tmp*-1));
} 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 ;
//int temp = Integer.parseInt((jTxtEq.getText()), base); // jTxtEq.setText(Integer.toBinaryString(temp)); //base = Binary; jBtnA.setEnabled(false); jBtnB.setEnabled(false); jBtnC.setEnabled(false); jBtnD.setEnabled(false); jBtnE.setEnabled(false); jBtnF.setEnabled(false); 19 | P a g e
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
private void jRB_DECActionPerformed(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.toString(temp)); base = Decimal; message = message + "\n " + tempvar + " base " + old_base + " = " + Integer.toString(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(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
private void jBtnAActionPerformed(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("A"); //jTxtEq.setText(Integer.toString(val)); } else { //inputvar = val; jTxtEq.setText( jTxtEq.getText() + "A"); } b_LastIsOperator = false; } private void jBtnBActionPerformed(java.awt.event.ActionEvent evt) { if ( b_LastIsOperator ) // last is operator jTxtEq.setText(""); temp = jTxtEq.getText(); if ("0".equals(temp)) { jTxtEq.setText("B"); //jTxtEq.setText(Integer.toString(val)); } else { //inputvar = val; jTxtEq.setText( jTxtEq.getText() + "B"); } b_LastIsOperator = false;// TODO add your handling code here: } private void jBtnCActionPerformed(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)) { 24 | P a g e
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
//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
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
33 | P a g e
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
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.
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
(Angel in degrees)(Trigonometry Operation) (=) The above answer in the calculator is calculated for 30 Sin = 0.5
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
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
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
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
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
6. Test Plans
Test No. 1. Function Addition PreConditions 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 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 56+4 Output 6.0
2.
Subtraction
9-3
6.0
3.
Multiplication
8*3
24.0
4.
Division
20/5
4.0
5.
Power
3 x^y 3
27.0
6.
Root
2 yx 9
3.0
44 | P a g e
JAVA PROGRAMMING DF10A1ICT before clicking 7. Exponential At least one numeric character must be entered before clicking exp At least one number should be entered before clicking % second operand Click = Enter first operand Click exp Click =
2 exp
7.389
8.
Percentage
9.
Decimal
10.
Hexadecimal
11.
Octal
12.
Binary
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%5
10
1001
15
1111
17
15
1111
45 | P a g e
JAVA PROGRAMMING DF10A1ICT Button Click on C to clear the display and all data stored in the memory Click on CE to the value in display
13.
1256 C
14.
Clear (CE)
15.
History Log
1234 CE
16.
Unary
17.
Sin
18.
Cosin
19.
Tan
20.
Sine Inverse
21.
Cosine Inverse
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
95
30 sin
0.5
60 cos
0.5
45 tan
0.5 Asin
30.0
0.5 Acos
60.0
46 | P a g e
JAVA PROGRAMMING DF10A1ICT 22. Tangent Inverse 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
23.
Square
5 x^2
25.0
24.
Cube
3 x^3
27.0
25.
Square Root
16 2x
4.0
26.
Cube Root
8 3x
2.0
27.
10^x
10 ^ 4
10000
28.
1/x
1 1/x 5
0.25
29
log
50 log
1.6989
30
log10
2 log10
0.3010
47 | P a g e
JAVA PROGRAMMING DF10A1ICT 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-
31.
Memory Save
Click on MS
5.0
32. 33. 34
If M = 65.0 5 M+
4.0 70
35
Memory Subtract
If M = 65.0 5 M-
-60
48 | P a g e
7. Assumptions
Assume that all the inputs are done by mouse The users wont use the text field(display) to direct inputs
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