ASIA PACIFIC INSTITUTE OF INFORMATION TECHNOLOGY APIIT Diploma Part II
INCOURSE ASSIGNMENT JAVA PROGRAMMING Prepared By
A.N.Ahamed Nishadh (CB004081) Module Code & Title
AAPP004-3-2-JP Cohort
DF10A1ICT Date of Submission
20th September 2011 Instructor
Dr. Dhananjay Kulkarni
Submitted in partial fulfillment for the degree of Bachelor of Science (Hons) in Computing
ACKNOWLEDGEMENTS Firstly we would like to thank my lecturer Dr. Dhananjay Kulkarnifor all the help and guidance given while doing this assignment. Also there are many individuals who have helped me in numerous ways directly and indirectly so that I was able to complete this assignment. APIIT Lanka for providing me with resources and the Tech Team at APIIT Lanka Lan ka for their assistance at required times. t imes. And last but not least my friends, parents and the well-wishers without whose moral support and encouragement, I would not have been able to do a good job. Finally, if there are any shortcomings in this project, then I request to excuse me for all those and accept acc ept this documentation. Ahamed Nishadh
i
TABLE OF CONTENTS 1.0 - INTRODUCTION ............................................................................................ 1 2.0 ± FLOW CHARTS .............................................................................................. 2 2.1 ± Chart 1 .................................................................................................... 2 2.2 ± Chart 2 .................................................................................................... 3 2.3 ± Chart 3 .................................................................................................... 4 3.0 ± CLASS DIAGRAMS ....................................................................................... 5 4.0 ± SOURCE CODE .............................................................................................. 6 4.1 ± CALCULATORVIEW.JAVA ................................................................. 6 4.2 ± CALCULATE.JAVA .............................................................................26 5.0 ± TEST PLAN ................................................................................................... 28 6.0 ± ASSUMPTIONS AND LIMITATIONS ......................................................... 31 6.1 ± ASSUMPTIONS....................................................................................31 6.2 ± LIMITATIONS .....................................................................................31 7.0 ± BIBLIOGRAPHY .......................................................................................... 32
ii
1.0
- INTRODUCTION
The calculator as we all know is one of the most fascinating devices available. A device that makes the lives of those who require it easier on a day to day basis also helps in various other ways when calculations are needed to be done fast and accurately. From just being able to add or subtract two numbers to being able to do the most complex calculations out there, calculators come in many different shapes, sizes and with various different functionalities to help us out. One point that should be noted is that the functionality of the calculators keeps improving with the technology also improving. improving. As the assignment for our module, I was assigned to design and develop a calculator program. With the completion of developing the program, I can safely say that the calculator that I have designed and developed can do all basic arithmetic problems as well as certain scientific and programmer oriented calculations. I have used Object Oriented Programming by using the JAVA Programming language and made my calculator. Although JAVA Programming Language provides many of the mathematical functionality through the Math Library as well as other libraries, certain sections of the program had to be coded by myself in order to get the calculations working perfectly. The GUI of the calculator also makes the software more user friendly to the user. In this documentation, I have discussed on how the software s oftware works, and displayed the full source code of the documentation as well as mentioned any and all assumptions and limitations that the t he software is made upon or capable of.
1
2.0
± FLOW CHARTS
For my assignment, I have drawn 3 different Flow Charts according to the requirements. 1st Chart is where the main functions such as addition, subtraction, multiplication and division happens. 2 nd Chart is for scientific functions which require only one variable. 3 rd Chart is for scientific functions which require 2 variables. 2.1
± Chart 1
2
2.2
± Chart 2
3
2.3
± Chart 3
4
3.0
± CLASS DIAGRAMS
5
4.0
4.1
± SOURCE CODE ± CALCULATORVIEW.JAVA
package calculator; import org.jdesktop.application.Action; org.jdesktop.application.Action; import org.jdesktop.application.ResourceMap; org.jdesktop.application.ResourceMap; import org.jdesktop.application.SingleFrameApplication; org.jdesktop.application.SingleFrameApplication; import org.jdesktop.application.FrameView; org.jdesktop.application.FrameView; import org.jdesktop.application.TaskMonitor; org.jdesktop.application.TaskMonitor; import java.awt.event.ActionEven ja va.awt.event.ActionEvent;t; import java.awt.event.ActionListene ja va.awt.event.ActionListener; r; import java.util.Arrays; import javax.swing.Timer; import javax.swing.Icon ja vax.swing.Icon;; import javax.swing.JDialog; import javax.swing.JFrame ja vax.swing.JFrame;; import javax.swing.JOptionPane; public class CalculatorView extends FrameView { public double num[] = new double[10]; public int type[] = new int[10]; public int numbers = 0; public double total = 0; public int numberbase = 1; public int calctype = 0; public String scimethod; sci method; public double tempval1; public double tempval2; public String history[] = new String[50]; public int historycount=0; public double memory = 0; public int helpflag; public String help; calculate method = new calculate(); public CalculatorView(SingleFrameApplication app) { private void jrbDecActionPerformed(java.awt.event.Action jrbDecActionPerformed(java.awt.event.ActionEvent Event evt) { if (jrbDec.isSelected()==true (jr bDec.isSelected()==true){ ){ btnA.setEnabled(false); btnB.setEnabled(false); btnC.setEnabled(false); btnD.setEnabled(false); btnE.setEnabled(false); btnF.setEnabled(false); btnAC.setEnabled(true); btnDel.setEnabled(true); btnDiv.setEnabled(true); btnDot.setEnabled(true); btnEqual.setEnabled(true); 6
btnPlus.setEnabled(true); btnPlusMin.setEnabled(true); btnMul.setEnabled(true); btnMin.setEnabled(true); btnNum0.setEnabled(true); btnNum1.setEnabled(true); btnNum2.setEnabled(true); btnNum3.setEnabled(true); btnNum4.setEnabled(true); btnNum5.setEnabled(true); btnNum6.setEnabled(true); btnNum7.setEnabled(true); btnNum8.setEnabled(true); btnNum9.setEnabled(true); btnCos.setEnabled(true); btnACos.setEnabled(true); btnExp.setEnabled(true); btnFac.setEnabled(true); btnLog.setEnabled(true); btnMc.setEnabled(true); btnMod.setEnabled(true); btnMr.setEnabled(true); btnMs.setEnabled(true); btnPer.setEnabled(true); btnPi.setEnabled(true); btnPow.setEnabled(true); btnSin.setEnabled(true); btnASin.setEnabled(true); btnSqaure.setEnabled(true); btnSqrt.setEnabled(true); btnTan.setEnabled(true); btnATan.setEnabled(true); btnPowMin.setEnabled(true); btnFractions.setEnabled(true); btnCube.setEnabled(true); btnCuberoot.setEnabled(true); } if(numberbase==1){ //do nothing } if(numberbase==2){ int tempnumber; tempnumber tempnumber = Integer.parseInt(txtOutput.getText(),2); Integer.parseInt(txtOutput.getText(),2); txtOutput.setText(Integer.toString(tempnumber)); } if(numberbase==3){ int tempnumber; tempnumber tempnumber = Integer.parseInt(txtOutput.getText(),8); Integer.parseInt(txtOutput.getText(),8); txtOutput.setText(Integer.toString(tempnumber)); } 7
if(numberbase==4){ int tempnumber; tempnumber tempnumber = Integer.parseInt(txtOutput.getText(),16); Integer.parseInt(txtOutput.getText(),16); txtOutput.setText(Integer.toString(tempnumber)); } numberbase=1;
} private void btnNum1ActionPerformed(java.awt.event.ActionEven btnNum1ActionPerformed(java.awt.event.ActionEventt evt) { txtOutput.setText(txtOutput.getText()+"1"); } private void btnNum2ActionPerformed(java.awt.event.ActionEven btnNum2ActionPerformed(java.awt.event.ActionEventt evt) { txtOutput.setText(txtOutput.getText()+"2"); } private void btnNum3ActionPerformed(java.awt.event.ActionEven btnNum3ActionPerformed(java.awt.event.ActionEventt evt) { txtOutput.setText(txtOutput.getText()+"3"); } private void btnNum4ActionPerformed(java.awt.event.ActionEven btnNum4ActionPerformed(java.awt.event.ActionEventt evt) { txtOutput.setText(txtOutput.getText()+"4"); } private void btnNum5ActionPerformed(java.awt.event.ActionEven btnNum5ActionPerformed(java.awt.event.ActionEventt evt) { txtOutput.setText(txtOutput.getText()+"5"); } private void btnNum6ActionPerformed(java.awt.event.ActionEven btnNum6ActionPerformed(java.awt.event.ActionEventt evt) { txtOutput.setText(txtOutput.getText()+"6"); } private void btnNum7ActionPerformed(java.awt.event.ActionEven btnNum7ActionPerformed(java.awt.event.ActionEventt evt) { txtOutput.setText(txtOutput.getText()+"7"); } private void btnNum8ActionPerformed(java.awt.event.ActionEven btnNum8ActionPerformed(java.awt.event.ActionEventt evt) { txtOutput.setText(txtOutput.getText()+"8"); } private void btnNum9ActionPerformed(java.awt.event.ActionEven btnNum9ActionPerformed(java.awt.event.ActionEventt evt) { txtOutput.setText(txtOutput.getText()+"9"); } private void btnNum0ActionPerformed(java.awt.event.ActionEven btnNum0ActionPerformed(java.awt.event.ActionEventt evt) { txtOutput.setText(txtOutput.getText()+"0"); } private void btnPlusActionPerformed(java.awt.event.ActionEvent btnPlusActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="The plus operator adds two or more numbers." + "\n" + "\n" + "Steps: " + "\n" + "1. Enter first number" + "\n" + "2. Click on the '+' button" + "\n" + "3. Enter second number" + "\n" + "4. Click on the '=' button" + "\n" + "\n" + "Example: 2 + 2 = 4" ; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ if (numbers<10){ if (numbers==0){ num[0]=Double.parseDouble(txtOutput.getText()); 8
txtInput.setText(txtInput.getText()+txtOutput.getText()+"+"); txtOutput.setText(""); total=num[0]; type[0] =1; numbers=numbers+1;
} else{ num[numbers]=Double.parseDouble(txtOutput.getText()); txtInput.setText(txtInput.getText()+txtOutput.getText()+"+"); txtOutput.setText(""); if(type[numbers-1]==1){ total=method.add(total,num[numbers]); } if(type[numbers-1]==2){ total=method.subtract(total,num[numbers]); } if(type[numbers-1]==3){ total=method.multiply(total,num[numbers]); } if(type[numbers-1]==4){ total=method.division(total,num[numbers]); } type[numbers] type[numbers] =1; numbers=numbers+1; }
} else{ txtOutput.setText("You Have Entered More than the Limit Allowed. Pls Check Again"); } } } private void btnMinActionPerformed(java.awt.event.ActionEvent btnMinActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="The minus operator subtracts two or more numbers." + "\n" + "\n" + "Steps:" + "\n" + "1.Enter first number" + "\n" + "2. Click on the '-' button" + "\n" + "3. Enter second number" + "\n" + "4. Click on the '=' button" + "\n" + "\n" + "Example: 8-2 = 6"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ if (numbers<10){ if (numbers==0){ num[0]=Double.parseDouble(txtOutput.getText()); txtInput.setText(txtInput.getText()+txtOutput.getText()+"-"); txtOutput.setText(""); total=num[0]; type[0] =2; numbers=numbers+1; 9
} else{ num[numbers]=Double.parseDouble(txtOutput.getText()); txtInput.setText(txtInput.getText()+txtOutput.getText()+"-"); txtOutput.setText(""); if(type[numbers-1]==1){ total=method.add(total,num[numbers]); } if(type[numbers-1]==2){ total=method.subtract(total,num[numbers]); } if(type[numbers-1]==3){ total=method.multiply(total,num[numbers]); } if(type[numbers-1]==4){ total=method.division(total,num[numbers]); } type[numbers] type[numbers] =2; numbers=numbers+1; } } else{ txtOutput.setText("You Have Entered More than the Limit Allowed. Pls Check Again"); } } } private void btnDivActionPerformed(java.awt.event.ActionEven btnDivActionPerformed(java.awt.event.ActionEventt evt) { if(helpflag==1){ help="Used for dividing two or more numbers." + "\n" + "\n" + "Steps:" + "\n" + "1.Enter first number" + "\n" + "2. Click on the '/' button" + "\n" + "3. Enter second number" + "\n" + "4. Click on the '=' button" + "\n" + "\n" + "Example: 18/3 = 6"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ if (numbers<10){ if (numbers==0){ num[0]=Double.parseDouble(txtOutput.getText()); txtInput.setText(txtInput.getText()+txtOutput.getText()+"/"); txtOutput.setText(""); total=num[0]; type[0] =4; numbers=numbers+1; } else{ num[numbers]=Double.parseDouble( num[numbers]=Double.parseDouble(txtOutput.getText()); txtOutput.getText()); txtInput.setText("("+txtInput.getText()+tx txtInput.setText("("+txtInput.getText()+txtOutput.getText()+")"+"/"); tOutput.getText()+")"+"/"); txtOutput.setText(""); 10
}
if(type[numbers-1]==1){ total=method.add(total,num[numbers]); } if(type[numbers-1]==2){ total=method.subtract(total,num[numbers]); } if(type[numbers-1]==3){ total=method.multiply(total,num[numbers]); } if(type[numbers-1]==4){ total=method.division(total,num[numbers]); } type[numbers] type[numbers] =4; numbers=numbers+1;
} else{ txtOutput.setText("You Have Entered More than the Limit Allowed. Pls Check Again"); } } } private void btnMulActionPerformed(java.awt.event.ActionEvent btnMulActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Used for multiplying two or more numbers." + "\n" + "\n" + "Steps:" + "\n" + "1.Enter first number" + "\n" + "2. Click on the '*' button" + "\n" + "3. Enter second number" + "\n" + "4. Click on the '=' button" + "\n" + "\n" + "Example: 6*2 = 12"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ if (numbers<10){ if (numbers==0){ num[0]=Double.parseDouble(txtOutput.getText()); txtInput.setText(txtInput.getText()+txtOutput.getText()+"*"); txtOutput.setText(""); total=num[0]; type[0] =3; numbers=numbers+1; } else{ num[numbers]=Double.parseDouble(txtOutput.getText()); txtInput.setText("("+txtInput.getText()+txtOutput.getText()+")"+"*"); txtOutput.setText(""); if(type[numbers-1]==1){ total=method.add(total,num[numbers]); } if(type[numbers-1]==2){ total=method.subtract(total,num[numbers]); 11
}
} if(type[numbers-1]==3){ total=method.multiply(total,num[numbers]); } if(type[numbers-1]==4){ total=method.division(total,num[numbers]); } type[numbers] type[numbers] =3; numbers=numbers+1;
} else{ txtOutput.setText("You Have Entered More than the Limit Allowed. Pls Check Again"); } } } private void btnEqualActionPerformed(java.awt.event.ActionEvent btnEqualActionPerformed(java.awt.event.ActionEvent evt) { if (calctype == 0){ if (numbers<10){ num[numbers]=Double.parseDouble(txtOutput.getText()); txtInput.setText(txtInput.getText()+txtOutput.getText()); if(type[numbers-1]==1){ total=method.add(total,num[numbers]); } if(type[numbers-1]==2){ total=method.subtract(total,num[numbers]); } if(type[numbers-1]==3){ total=method.multiply(total,num[numbers]); } if(type[numbers-1]==4){ total=method.division(total,num[numbers]); } txtOutput.setText(Double.toString(total)); numbers=numbers+1; history[historycount]=txtInput.getText()+" history[historycount]=txtInput.getText()+" = "+txtOutput.getText(); "+txtOutput.getText(); historycount=historycount+1; } else{ txtOutput.setText("You Have Entered More than the Limit Allowed. Pls Check Again"); } } if(calctype == 1){ double temptotal; if(scimethod=="Power"){ tempval2=Double.parseDouble(txtOutput.getText()); temptotal=method.power(tempval1,tempval2); txtOutput.setText(Double.toString(temptotal)); 12
} if(scimethod=="Percentage"){ tempval2=Double.parseDouble(txtOutput.getText()); temptotal=method.percent(tempval1,tempval2); txtOutput.setText(Double.toString(temptotal)); } if(scimethod=="Mod"){ tempval2=Double.parseDouble(txtOutput.getText()); temptotal=method.mod(tempval1,tempval2); txtOutput.setText(Double.toString(temptotal)); } if(scimethod=="Log"){ tempval1=Double.parseDouble(txtOutput.getText()); temptotal=method.log(tempval1); txtOutput.setText(Double.toString(temptotal)); } if(scimethod=="Sin"){ tempval1=Double.parseDouble(txtOutput.getText()); temptotal=method.sin(tempval1); txtOutput.setText(Double.toString(temptotal)); } if(scimethod=="Cos"){ tempval1=Double.parseDouble(txtOutput.getText()); temptotal=method.cos(tempval1); txtOutput.setText(Double.toString(temptotal)); } if(scimethod=="Tan"){ tempval1=Double.parseDouble(txtOutput.getText()); temptotal=method.tan(tempval1); txtOutput.setText(Double.toString(temptotal)); } if(scimethod=="ASin"){ tempval1=Double.parseDouble(txtOutput.getText()); temptotal=method.asin(tempval1); txtOutput.setText(Double.toString(temptotal)); } if(scimethod=="ACos"){ tempval1=Double.parseDouble(txtOutput.getText()); temptotal=method.acos(tempval1); txtOutput.setText(Double.toString(temptotal)); } if(scimethod=="ATan"){ tempval1=Double.parseDouble(txtOutput.getText()); temptotal=method.atan(tempval1); txtOutput.setText(Double.toString(temptotal)); } if(scimethod=="Square if(scimethod=="Square Root"){ tempval1=Double.parseDouble(txtOutput.getText()); if(tempval1>0){ temptotal=method.sqrt(tempval1); 13
txtOutput.setText(Double.toString(temptotal)); } else{ txtOutput.setText("Enter txtOutput.setText("Enter a Positive Value"); }
} if(scimethod=="EXP"){ tempval1=Double.parseDouble(txtOutput.getText()); if (tempval1>0){ temptotal=method.power(10,tempval1); txtOutput.setText(Double.toString(temptotal)); } else{ txtOutput.setText("Enter txtOutput.setText("Enter a Positive Value"); } } if(scimethod=="Cube Root"){ tempval1=Double.parseDouble(txtOutput.getText()); temptotal=method.cbrt(tempval1); txtOutput.setText(Double.toString(temptotal)); } calctype=0; scimethod=""; lblSciName.setText(""); tempval1=0; tempval2=0;
} } private void btnDotActionPerformed(java.awt.event.Action btnDotActionPerformed(java.awt.event.ActionEvent Event evt) { txtOutput.setText(txtOutput.getText()+"."); } private void btnPlusMinActionPerformed(java.awt.event.ActionEvent btnPlusMinActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="The operator (unary) converts converts a positive positive number to negative negative and vice vice versa." + "\n" + "\n" + "Steps:" + "\n" + "1. Enter a value" + "\n" + "2. Click on the ' ' button" + "\n" + "\n" + "Example: "Example: 9 = -9.0"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ txtOutput.setText("-"+txtOutput.getText()); } } private void btnDelActionPerformed(java.awt.event.ActionEvent btnDelActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="The DEL (Clear) button deleted the last entered number."; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ 14
txtOutput.setText(txtOutput.getText().substring(0, txtOutput.getText().length()txtOutput.setText(txtOutput.getText().substring(0, txtOutput.getText().length()1)); } } private void btnACActionPerformed(java.awt.event.ActionEvent btnACActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="The AC (All Clear) button, clears everything"; JOptionPane.showMessageDialog(null,help); helpflag=1; } else{ Arrays.fill(num, 0); Arrays.fill(type,0); Arrays.fill(history,""); numbers = 0; total = 0; numberbase=0; scimethod=""; historycount=0; tempval1=0; tempval2=0; memory=0; txtInput.setText(null); txtOutput.setText(null); } } private void jrbBinActionPerformed(java.awt.event.ActionEvent jrbBinActionPerformed(java.awt.event.ActionEvent evt) { if (jrbBin.isSelected()==true){ btnA.setEnabled(false); btnB.setEnabled(false); btnC.setEnabled(false); btnD.setEnabled(false); btnE.setEnabled(false); btnF.setEnabled(false); btnAC.setEnabled(true); btnDel.setEnabled(true); btnDiv.setEnabled(false); btnDot.setEnabled(false); btnEqual.setEnabled(false); btnPlus.setEnabled(false); btnPlusMin.setEnabled(false); btnMul.setEnabled(false); btnMin.setEnabled(false); btnNum0.setEnabled(true); btnNum1.setEnabled(true); btnNum2.setEnabled(false); btnNum3.setEnabled(false); btnNum4.setEnabled(false); btnNum5.setEnabled(false); 15
btnNum6.setEnabled(false); btnNum7.setEnabled(false); btnNum8.setEnabled(false); btnNum9.setEnabled(false); btnCos.setEnabled(false); btnACos.setEnabled(false); btnExp.setEnabled(false); btnFac.setEnabled(false); btnLog.setEnabled(false); btnMc.setEnabled(false); btnMod.setEnabled(false); btnMr.setEnabled(false); btnMs.setEnabled(false); btnPer.setEnabled(false); btnPi.setEnabled(false); btnPow.setEnabled(false); btnSin.setEnabled(false); btnASin.setEnabled(false); btnSqaure.setEnabled(false); btnSqrt.setEnabled(false); btnTan.setEnabled(false); btnATan.setEnabled(false); btnPowMin.setEnabled(false); btnFractions.setEnabled(false); btnCube.setEnabled(false); btnCuberoot.setEnabled(false);
} if(numberbase==1){ int tempnumber; tempnumber tempnumber = Integer.parseInt(txtOutput.getText()); Integer.parseInt(txtOutput.getText()); txtOutput.setText(Integer.toBinaryString(tempnumber)); } if(numberbase==2){ //do nothing } if(numberbase==3){ int tempnumber; tempnumber tempnumber = Integer.parseInt(txtOutput.getText(),8); Integer.parseInt(txtOutput.getText(),8); txtOutput.setText(Integer.toBinaryString(tempnumber)); } if(numberbase==4){ int tempnumber; tempnumber tempnumber = Integer.parseInt(txtOutput.getText(),16); Integer.parseInt(txtOutput.getText(),16); txtOutput.setText(Integer.toBinaryString(tempnumber)); } numberbase=2; } private void jrbOctActionPerformed(java.awt.event.ActionEvent jrbOctActionPerformed(java.awt.event.ActionEvent evt) { if (jrbOct.isSelected()==true){ 16
btnA.setEnabled(false); btnB.setEnabled(false); btnC.setEnabled(false); btnD.setEnabled(false); btnE.setEnabled(false); btnF.setEnabled(false); btnAC.setEnabled(true); btnDel.setEnabled(true); btnDiv.setEnabled(false); btnDot.setEnabled(false); btnEqual.setEnabled(false); btnPlus.setEnabled(false); btnPlusMin.setEnabled(false); btnMul.setEnabled(false); btnMin.setEnabled(false); btnNum0.setEnabled(true); btnNum1.setEnabled(true); btnNum2.setEnabled(true); btnNum3.setEnabled(true); btnNum4.setEnabled(true); btnNum5.setEnabled(true); btnNum6.setEnabled(true); btnNum7.setEnabled(true); btnNum8.setEnabled(false); btnNum9.setEnabled(false); btnCos.setEnabled(false); btnACos.setEnabled(false); btnExp.setEnabled(false); btnFac.setEnabled(false); btnLog.setEnabled(false); btnMc.setEnabled(false); btnMod.setEnabled(false); btnMr.setEnabled(false); btnMs.setEnabled(false); btnPer.setEnabled(false); btnPi.setEnabled(false); btnPow.setEnabled(false); btnSin.setEnabled(false); btnASin.setEnabled(false); btnSqaure.setEnabled(false); btnSqrt.setEnabled(false); btnTan.setEnabled(false); btnATan.setEnabled(false); btnPowMin.setEnabled(false); btnFractions.setEnabled(false); btnCube.setEnabled(false); btnCuberoot.setEnabled(false);
} if(numberbase==1){ int tempnumber;
17
tempnumber tempnumber = Integer.parseInt(txtOutput.getText()); Integer.parseInt(txtOutput.getText()); txtOutput.setText(Integer.toOctalString(tempnumber)); } if(numberbase==2){ int tempnumber; tempnumber tempnumber = Integer.parseInt(txtOutput.getText(),2); Integer.parseInt(txtOutput.getText(),2); txtOutput.setText(Integer.toOctalString(tempnumber)); } if(numberbase==3){ // do nothing } if(numberbase==4){ int tempnumber; tempnumber tempnumber = Integer.parseInt(txtOutput.getText(),16); Integer.parseInt(txtOutput.getText(),16); txtOutput.setText(Integer.toOctalString(tempnumber)); } numberbase=3;
} private void jrbHexActionPerformed(java.awt.event.ActionEven jrbHexActionPerformed(java.awt.event.ActionEventt evt) { if (jrbHex.isSelected()==true){ btnA.setEnabled(true); btnB.setEnabled(true); btnC.setEnabled(true); btnD.setEnabled(true); btnE.setEnabled(true); btnF.setEnabled(true); btnAC.setEnabled(true); btnDel.setEnabled(true); btnDiv.setEnabled(false); btnDot.setEnabled(false); btnEqual.setEnabled(false); btnPlus.setEnabled(false); btnPlusMin.setEnabled(false); btnMul.setEnabled(false); btnMin.setEnabled(false); btnNum0.setEnabled(true); btnNum1.setEnabled(true); btnNum2.setEnabled(true); btnNum3.setEnabled(true); btnNum4.setEnabled(true); btnNum5.setEnabled(true); btnNum6.setEnabled(true); btnNum7.setEnabled(true); btnNum8.setEnabled(true); btnNum9.setEnabled(true); btnCos.setEnabled(false); btnACos.setEnabled(false); btnExp.setEnabled(false); btnFac.setEnabled(false); btnLog.setEnabled(false); 18
btnMc.setEnabled(false); btnMod.setEnabled(false); btnMr.setEnabled(false); btnMs.setEnabled(false); btnPer.setEnabled(false); btnPi.setEnabled(false); btnPow.setEnabled(false); btnSin.setEnabled(false); btnASin.setEnabled(false); btnSqaure.setEnabled(false); btnSqrt.setEnabled(false); btnTan.setEnabled(false); btnATan.setEnabled(false); btnPowMin.setEnabled(false); btnFractions.setEnabled(false); btnCube.setEnabled(false); btnCuberoot.setEnabled(false);
} if(numberbase==1){ int tempnumber; tempnumber tempnumber = Integer.parseInt(txtOutput.getText()); Integer.parseInt(txtOutput.getText()); txtOutput.setText(Integer.toHexString(tempnumber).toUpperCase()); } if(numberbase==2){ int tempnumber; tempnumber tempnumber = Integer.parseInt(txtOutput.getText(),2); Integer.parseInt(txtOutput.getText(),2); txtOutput.setText(Integer.toHexString(tempnumbe txtOutput.setText(Integer.toHexString(tempnumber).toUpperCase()); r).toUpperCase()); } if(numberbase==3){ int tempnumber; tempnumber tempnumber = Integer.parseInt(txtOutput.getText(),8); Integer.parseInt(txtOutput.getText(),8); txtOutput.setText(Integer.toHexString(tempnumber).toUpperCase()); } if(numberbase==4){ //do nothing } numberbase=4;
} private void btnAActionPerformed(java.awt.event.ActionEvent evt) { txtOutput.setText(txtOutput.getText()+"A"); } private void btnBActionPerformed(java.awt.event.ActionEvent btnBActionPerformed(java.awt.event.ActionEvent evt) { txtOutput.setText(txtOutput.getText()+"B"); } private void btnCActionPerformed(java.awt.event.ActionEvent btnCActionPerformed(java.awt.event.ActionEvent evt) { txtOutput.setText(txtOutput.getText()+"C"); } private void btnDActionPerformed(java.awt.event.ActionEvent evt) { txtOutput.setText(txtOutput.getText()+"D"); } 19
private void btnEActionPerformed(java.awt.event.ActionEvent btnEActionPerformed(java.awt.event.ActionEvent evt) { txtOutput.setText(txtOutput.getText()+"E"); } private void btnFActionPerformed(java.awt.event.ActionEvent btnFActionPerformed(java.awt.event.ActionEvent evt) { txtOutput.setText(txtOutput.getText()+"F"); } private void btnPiActionPerformed(java.awt.event.ActionEvent btnPiActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="A mathematical constant whose value is the ratio of any circle's circumference to its diameter." + "\n" + "p is approximately equal to 3.14159."; JOptionPane.showMessageDialog(null,help); helpflag=1; } else{ txtOutput.setText(Double.toString(Math.PI)); } } private void btnSqaureActionPerformed(java.awt.event.ActionEvent btnSqaureActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Returns the product of a number with itself." + "\n" + "\n" + "Steps:" + "\n" + "1. Enter the value" + "\n" + "2. Click on the 'x²' button" + "\n" + "\n" + "Example: 5² = 25"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ double temptotal; temptotal=method.power(Double.parseDo temptotal=method.power(Double.parseDouble(txtOutput.getText()),2); uble(txtOutput.getText()),2); txtOutput.setText(Double.toString(temptotal)); } } private void btnPowActionPerformed(java.awt.event.ActionEvent btnPowActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="The ^ operator raises a number to a power." + "\n" + "\n" + "Steps:" + "\n" + "1.Enter first number" + "\n" + "2. Click on the '^' button" + "\n" + "3. Enter second number" + "\n" + "4. Click on the '=' button" + "\n" + "\n" + "Example: 2^3 = 8" ; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ calctype=1; scimethod="Power"; lblSciName.setText(scimethod); tempval1=Double.parseDouble(txtOutput.getText()); txtOutput.setText(""); } } private void btnPerActionPerformed(java.awt.event.ActionEvent btnPerActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ 20
help="The % operator finds the percentage of One number." + "\n" + "This is done by dividing the second value by 100 and multiplying it by the first value" + "\n" + "\n" + "Steps:" + "\n" + "1.Enter first number" + "\n" + "2. Click on the '%' button" + "\n" + "3. Enter the percentage to be found" + "\n" + "4. Click on the '=' button" + "\n" + "\n" + "Example: 50*(20/100) = 10 " ; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ calctype=1; scimethod="Percentage"; lblSciName.setText(scimethod); tempval1=Double.parseDouble(txtOutput.getText()); txtOutput.setText(""); } } private void btnModActionPerformed(java.awt.event.ActionEven btnModActionPerformed(java.awt.event.ActionEventt evt) { if(helpflag==1){ help="The MOD operator finds the remainder of division of one number by another." + "\n" + "\n" + "Steps:" + "\n" + "1.Enter first number" + "\n" + "2. Click on the 'MOD' button" + "\n" + "3. Enter second number" + "\n" + "4. Click on the '=' button" + "\n" + "\n" + "Example: 10 MOD 3 = 1"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ calctype=1; scimethod="Mod"; lblSciName.setText(scimethod); tempval1=Double.parseDouble(txtOutput.getText()); txtOutput.setText(""); } } private void btnLogActionPerformed(java.awt.event.ActionEvent btnLogActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Returns the logarithm (base 10) of a double value." + "\n" + "\n" + "Steps:" + "\n" + "1. Click on the 'LOG' button" + "\n" + "2. Enter a Value" + "\n" + "\n" + "Example: LOG 50 = 1.6989"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ calctype=1; scimethod="Log"; lblSciName.setText(scimethod); txtOutput.setText(""); } } private void btnSinActionPerformed(java.awt.event.ActionEvent btnSinActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ 21
help="Returns the trigonometric sine of an angle." + "\n" + "\n" + "Steps:" + "\n" + "1. Click on the 'sin' button" + "\n" + "2. Enter a value" + "\n" + "\n" + "Example: Sin 90 = 1"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ calctype=1; scimethod="Sin"; lblSciName.setText(scimethod); txtOutput.setText(""); } } private void btnCosActionPerformed(java.awt.event.ActionEvent btnCosActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Returns the trigonometric cosine of an angle." + "\n" + "\n" + "Steps:" + "\n" + "1. Click on the 'cos' button" + "\n" + "2. Enter a value" + "\n" + "\n" + "Example: Cos 60 = 0.5"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ calctype=1; scimethod="Cos"; lblSciName.setText(scimethod); txtOutput.setText(""); } } private void btnTanActionPerformed(java.awt.event.ActionEvent btnTanActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Returns the trigonometric tangent of an angle." + "\n" + "\n" + "Steps:" + "\n" + "1. Click on the 'tan' button" + "\n" + "2. Enter a Value" + "\n" + "\n" + "Example: Tan 30 = 0.577"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ calctype=1; scimethod="Tan"; lblSciName.setText(scimethod); txtOutput.setText(""); } } private void btnASinActionPerformed(java.awt.event.ActionEvent btnASinActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Returns the arc sine of an angle." + "\n" + "\n" + "Steps:" + "\n" + "1. Click on the 'ASIN' button" + "\n" + "2. Enter A Value" + "\n" + "\n" + "Example: sin?¹ 1 = 90"; JOptionPane.showMessageDialog(null,help); helpflag=0; 22
} else{ calctype=1; scimethod="ASin"; lblSciName.setText(scimethod); txtOutput.setText(""); } } private void btnACosActionPerformed(java.awt.event.ActionEvent btnACosActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Returns the arc cosine of an angle." + "\n" + "\n" + "Steps:" + "\n" + "1. Click on the 'cos?¹' button" + "\n" + "2. Enter a Value" + "\n" + "\n" + "Example: cos?¹ 0.25 = 75.5"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ calctype=1; scimethod="ACos"; lblSciName.setText(scimethod); txtOutput.setText(""); } } private void btnATanActionPerformed(java.awt.event.ActionEvent btnATanActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Returns the arc tangent of an angle." + "\n" + "\n" + "Steps:" + "\n" + "1. Click on the 'tan?¹' button" + "\n" + "2. Enter a Value" + "\n" + "\n" + "Example: tan?¹ 0.75 = 36.86"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ calctype=1; scimethod="ATan"; lblSciName.setText(scimethod); txtOutput.setText(""); } } private void btnSqrtActionPerformed(java.awt.event.ActionEvent btnSqrtActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Returns the correctly rounded positive square root of a double value." + "\n" + "\n" + "Steps:" + "\n" + "1. Click on the 'v' button" + "\n" + "2. Enter a value" + "\n" + "\n" + "Example: v16 = 4"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ calctype=1; scimethod="Square scimethod="Square Root"; 23
lblSciName.setText(scimethod); txtOutput.setText(""); } } private void btnMcActionPerformed(java.awt.event.ActionEvent btnMcActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Clears the memory and sets it to zero."; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ memory=0; } } private void btnMrActionPerformed(java.awt.event.ActionEvent btnMrActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Displays the value stored in the calculator's calculat or's memory."; memory."; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ txtOutput.setText(Double.toString(memory)); } } private void btnMsActionPerformed(java.awt.event.ActionEvent btnMsActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Saves the number on display into the memory for future use."; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ memory=Double.parseDouble(txtOutput.getText()); } } private void btnFacActionPerformed(java.awt.event.ActionEvent btnFacActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Returns the product of all positive integers less than or equal to the input value." + "\n" + "\n" + "Steps:" + "\n" + "1. Enter the value" + "\n" + "2. Click on the 'n!' button" + "\n" + "\n" + "Example: 5! = 120"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ int temptotal; temptotal=method.factorial(Integer.parseInt(txtOutput.getText())); txtOutput.setText(Integer.toString(temptotal)); } } private void btnExpActionPerformed(java.awt.event.ActionEvent btnExpActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ 24
help="The EXP operator concatenates zeros to the first input." + "\n" + "The number of zeros depend on the second input." + "\n" + "\n" + "Steps:" + "\n" + "1.Enter first number" + "\n" + "2. Click on the 'EXP' button" + "\n" + "3. Enter second number" + "\n" + "4. Click on the '=' button" + "\n" + "\n" + "Example: 1 EXP 3= 1000" ; JOptionPane.showMessageDialog(null,help); helpflag=1; } else{ calctype=1; scimethod="EXP"; lblSciName.setText(scimethod); txtOutput.setText(""); } } private void btnFractionsActionPerformed(java.awt.event.ActionEvent btnFractionsActionPerformed(java.awt.event.ActionEvent evt) { if(helpflag==1){ help="Returns the fractional value of a number." + "\n" + "\n" + "Steps:" + "\n" + "1. Enter the value" + "\n" + "2. Click on the '1/x' button" + "\n" + "\n" + "Example: 1/2 = 0.5"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ double temptotal; temptotal=method.fractions(Double.parseDoub temptotal=method.fractions(Double.parseDouble(txtOutput.getText())) le(txtOutput.getText()));; txtOutput.setText(Double.toString(temptotal)); } } private void btnCubeActionPerformed(java.awt.even btnCubeActionPerformed(java.awt.event.ActionEvent t.ActionEvent evt) { if(helpflag==1){ help="Returns the product of a number multiplying itself three times." + "\n" + "\n" + "Steps:" + "\n" + "1. Enter the value" + "\n" + "2. Click on the 'x³' button" + "\n" + "\n" + "Example: 4³ = 64"; JOptionPane.showMessageDialog(null,help); helpflag=0; } else{ double temptotal; temptotal=method.power(Double.parseDo temptotal=method.power(Double.parseDouble(txtOutput.getText()),3); uble(txtOutput.getText()),3); txtOutput.setText(Double.toString(temptotal)); } } private void btnCuberootActionPerformed(java.awt.event.Ac btnCuberootActionPerformed(java.awt.event.ActionEvent tionEvent evt) { if(helpflag==1){ help="Returns the cube root of a double value." + "\n" + "\n" + "Steps:" + "\n" + "1. Enter the value" + "\n" + "2. Click on the '?' button" + "\n" + "\n" + "Example: ?8 = 2"; JOptionPane.showMessageDialog(null,help); 25
helpflag=1;
} else{ calctype=1; scimethod="Cube Root"; lblSciName.setText(scimethod); txtOutput.setText(""); }
} private void btnPowMinActionPerformed(java.awt.event.Ac btnPowMinActionPerformed(java.awt.event.ActionEvent tionEvent evt) { double temptotal; temptotal=method.power(Double.parseDo temptotal=method.power(Double.parseDouble(txtOutput.getText()),-1); uble(txtOutput.getText()),-1); txtOutput.setText(Double.toString(temptotal)); } private void btnHelpActionPerformed(java.awt.event.ActionEven btnHelpActionPerformed(java.awt.event.ActionEventt evt) { helpflag = 1; } private void btnHistoryActionPerformed(java.awt.event.ActionEvent btnHistoryActionPerformed(java.awt.event.ActionEvent evt) { String historybutton=""; historybutton=""; for (int i=0;i
4.2
± CALCULATE.JAVA
package calculator; public class calculate { public double add(double a, double b){ return a+b; } public double subtract(double a, double b){ return a-b; } public double multiply(double a, double b){ return a*b; } public double division(double a, double b){ return a/b; } public double mod(double a, double b){ return a%b; } public double power(double a, double b){ return Math.pow(a, b); } public double percent(double a, double b){ 26
return (a*(b/100)); } public double log(double a){ return Math.log10(a) Mat h.log10(a);; } public double sin(double a){ return Math.sin(Math.toR Mat h.sin(Math.toRadians(a)); adians(a)); } public double cos(double a){ return Math.cos(Math.toRadian Mat h.cos(Math.toRadians(a)); s(a)); } public double tan(double a){ return Math.tan(Math.toRad Mat h.tan(Math.toRadians(a)); ians(a)); } public double asin(double a){ return Math.toDegrees(Math Mat h.toDegrees(Math.asin(a)); .asin(a)); } public double acos(double a){ return Math.toDegrees(Math Mat h.toDegrees(Math.acos(a)); .acos(a)); } public double atan(double a){ return Math.toDegrees(Math Mat h.toDegrees(Math.atan(a)); .atan(a)); } public double sqrt(double a){ return Math.sqrt(a); } public int factorial(int a){ int i; int fact=1; for(i=1;i<=a;i++){ fact=fact*i; } return fact; } public double fractions(double a){ return 1/a; } public double cbrt(double a){ return Math.cbrt(a); } }
27
5.0
ID
± TEST PLAN CALCULA TION
CONDITIONS TO BE MET
STEPS TO BE DONE
Enter first number Click on µ+¶ Enter second number Click on µ=¶ Enter first number Click on µ-¶ Enter second number Click on µ=¶ Enter first number Click on µ*¶ Enter second number Click on µ=¶ Enter first number Click on µ/¶ Enter second number Click on µ=¶ Enter first number Click on µMOD¶ Enter second number Click on µ=¶ Enter first number Click on µ^¶ Enter second number Click on µ=¶
Addition
At least one number should be entered before clicking µ+¶
2.
Subtraction
At least one number should be entered before clicking µ-¶
3.
At least one Multiplicati number should on be entered before clicking µ-¶
4.
Division
At least one number should be entered before clicking µ/¶
Modulus
At least one number should be entered before clicking µMOD¶
Power
At least one number should be entered before clicking µ^¶
7.
EXP
EXP Button should be Click on µEXP¶ Enter number clicked before Click on µ=¶ entering number
8.
Percentage
At least one Enter first number should number
1.
5.
6.
28
INPUT GIVEN
OUTPUT OBTAINED
5+3
8.0
6-4
2.0
5*3
15.0
15/3
5.0
5 MOD 2
1
3^3
27.0
EXP 3
1000.0
50 % 20
10
9.
10.
11.
12. 13. 14. 15.
16.
17.
18. 19.
be entered before Click on µ%¶ clicking µ%¶ Enter second number Click on µ=¶ Enter a binary or Octal or a hexadecimal Decimal value Click on µDecimal¶ to convert Enter a binary or Octal or a hexadecimal Hexadecima value l Click on µHexadecimal¶ µHexadecimal¶ to convert Enter a binary or Octal or a hexadecimal Binary value Click on µBinary¶ to convert Click on µAC¶ to All Clear clear the text field Click on µDEL¶ DEL to clear one by one Click on History µHistory¶ to view calculation log A number should Enter a value Unary be entered before Click lick on µ ¶ clic clickking ing µ ¶ µSIN¶ should be Click on µSIN¶ clicked before Sin Enter a value entering a Click on µ=¶ number µCOS¶ should be Click on µCOS¶ clicked before Cos Enter a value entering a Click on µ=¶ number µTAN¶ should be Click on µTAN¶ clicked before Tan Enter a value entering a Click on µ=¶ number ASin µASIN¶ should Click on µASIN¶ 29
1010
10
1010
A
C
1100
1234
-
1234
123
-
2.0+2.0=4 Sin 90.0=1.0
9
-9.0
sin 90
1.0
cos 60
0.5
tan 30
0.577
ASin 1
90.0
20.
21.
22. 23.
24.
25. 28. 29. 30.
31.
32. 33. 34.
be clicked before entering a number µACOS¶ should be clicked before ACos entering a number µATAN¶ should be clicked before ATan entering a number A number should Square be entered before clicking µx²¶ A number should Cube be entered before clicking µx³¶ µ¥¶ should be clicked before Square Root entering a number µ ¶ should be clicked before Cube Root entering a number A number should 1/x be entered before clicking µ1/x¶ A number should n! be entered before clicking µn!¶ µLOG¶ should be clicked before log entering a number A number should Memory be present in the Save text field, in order to be saved Memory Recall Memory Clear
Enter a value Click on µ=¶ Click on µACOS¶ Enter a value Click on µ=¶
ACos 0.25
75.5
ATan 0.75
36.86
5²
25.0
4³
64.0
Click on µ¥¶ Enter a value Click on µ=¶
¥16
4.0
Click on µ ¶ Enter a value Click on µ=¶
8
2.0
Click on µ¶
-
3.14159«..
Enter a value Click on µ1/x¶
1/2
0.5
Enter a value Click on µn!¶
5!
120.0
Click on µLOG¶ Enter a value Click on µ=¶
LOG 50
1.6989
Click on µMS¶
-
-
Click on µMR¶
-
4.0
Click on µMC¶
-
-
Click on µATAN¶ Enter a value Click on µ=¶ Enter a value Click on µx²¶ Click on µ=¶ Enter a value Click on µx³¶ Click on µ=¶
30
6.0
± ASSUMPTIONS AND LIMITATIONS
Although I have done this calculator to the best of my abilities, there are certain assumptions that I have taken while doing programming this calculator and also some limitations that the calculator ca lculator is bound to. They are listed list ed below. 6.1
± ASSUMPTIONS
1. The user will always enter a value before entering a sign. If not, 0 is taken into account automatically and calculations ca lculations are performed. 2. Input is given by way of clicking the buttons on screen by the mouse pointer only and not by entering values through the keyboard. 6.2
± LIMITATIONS
1. Addition, Subtractions, Division, Multiplication of Binary, Octal and Hexadecimal values are not possible. 2. Only a maximum of 10 different numbers can be calculated in one formula.
31
7.0
± BIBLIOGRAPHY
Oracle.com, 2011. Java 2 Platform Standard Ed. 7.0. [Online] Available at: http://download.oracle.com/javase/7/docs/api/java/lang/Math.html [Accessed 2011]. www.roseindia.net, 2011. Rose India Technologies . [Online] Available at: http://www.roseindia.net/java [Accessed 2011].
32