You are on page 1of 35

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
for their assistance at required times.

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 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.

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 works, and displayed
the full source code of the documentation as well as mentioned any and all
assumptions and limitations that the 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. 2nd Chart is for scientific functions which
require only one variable. 3rd Chart is for scientific functions which require 2
variables.

2.1 – Chart 1

2
2.2 – Chart 2

START

ENTER DIGITS

ENTER
FUNCTION

PASS VALUE INTO


CALCULATE CLASS RELAVENT
METHOD

DO RELEVANT
CALCULATIONS

RETURN CALCULATED VALUE


BACK TO MAIN CLASS

DISPLAY THE
RESULT

END

3
2.3 – Chart 3

START

ENTER FIRST
DIGIT

ENTER
ENTER
SECOND
FUNCTION
DIGIT

PASS VALUES INTO


CALCULATE CLASS RELAVENT
METHOD

DO RELEVANT
CALCULATIONS

RETURN CALCULATED VALUE


BACK TO MAIN CLASS

DISPLAY THE
RESULT

END

4
3.0 – CLASS DIAGRAMS

5
4.0 – SOURCE CODE

4.1 – CALCULATORVIEW.JAVA
package calculator;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.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;
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.ActionEvent evt) {
if (jrbDec.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 = Integer.parseInt(txtOutput.getText(),2);
txtOutput.setText(Integer.toString(tempnumber));
}
if(numberbase==3){
int tempnumber;
tempnumber = Integer.parseInt(txtOutput.getText(),8);
txtOutput.setText(Integer.toString(tempnumber));
}

7
if(numberbase==4){
int tempnumber;
tempnumber = Integer.parseInt(txtOutput.getText(),16);
txtOutput.setText(Integer.toString(tempnumber));
}
numberbase=1;
}
private void btnNum1ActionPerformed(java.awt.event.ActionEvent evt) {
txtOutput.setText(txtOutput.getText()+"1");
}
private void btnNum2ActionPerformed(java.awt.event.ActionEvent evt) {
txtOutput.setText(txtOutput.getText()+"2");
}
private void btnNum3ActionPerformed(java.awt.event.ActionEvent evt) {
txtOutput.setText(txtOutput.getText()+"3");
}
private void btnNum4ActionPerformed(java.awt.event.ActionEvent evt) {
txtOutput.setText(txtOutput.getText()+"4");
}
private void btnNum5ActionPerformed(java.awt.event.ActionEvent evt) {
txtOutput.setText(txtOutput.getText()+"5");
}
private void btnNum6ActionPerformed(java.awt.event.ActionEvent evt) {
txtOutput.setText(txtOutput.getText()+"6");
}
private void btnNum7ActionPerformed(java.awt.event.ActionEvent evt) {
txtOutput.setText(txtOutput.getText()+"7");
}
private void btnNum8ActionPerformed(java.awt.event.ActionEvent evt) {
txtOutput.setText(txtOutput.getText()+"8");
}
private void btnNum9ActionPerformed(java.awt.event.ActionEvent evt) {
txtOutput.setText(txtOutput.getText()+"9");
}
private void btnNum0ActionPerformed(java.awt.event.ActionEvent evt) {
txtOutput.setText(txtOutput.getText()+"0");
}
private void 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] =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 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] =2;
numbers=numbers+1;
}
}
else{
txtOutput.setText("You Have Entered More than the Limit Allowed. Pls
Check Again");
}
}
}
private void btnDivActionPerformed(java.awt.event.ActionEvent 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(txtOutput.getText());
txtInput.setText("("+txtInput.getText()+txtOutput.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] =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 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] =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 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()+" = "+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 Root"){
tempval1=Double.parseDouble(txtOutput.getText());
if(tempval1>0){
temptotal=method.sqrt(tempval1);

13
txtOutput.setText(Double.toString(temptotal));
}
else{
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 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.ActionEvent evt) {
txtOutput.setText(txtOutput.getText()+".");
}
private void btnPlusMinActionPerformed(java.awt.event.ActionEvent evt) {
if(helpflag==1){
help="The ± operator (unary) converts a positive number to negative and vice
versa." + "\n" + "\n" + "Steps:" + "\n" + "1. Enter a value" + "\n" + "2. Click on
the '±' button" + "\n" + "\n" + "Example: 9 = -9.0";
JOptionPane.showMessageDialog(null,help);
helpflag=0;
}
else{
txtOutput.setText("-"+txtOutput.getText());
}
}
private void 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()-
1));
}
}
private void 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 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 = Integer.parseInt(txtOutput.getText());
txtOutput.setText(Integer.toBinaryString(tempnumber));
}
if(numberbase==2){
//do nothing
}
if(numberbase==3){
int tempnumber;
tempnumber = Integer.parseInt(txtOutput.getText(),8);
txtOutput.setText(Integer.toBinaryString(tempnumber));
}
if(numberbase==4){
int tempnumber;
tempnumber = Integer.parseInt(txtOutput.getText(),16);
txtOutput.setText(Integer.toBinaryString(tempnumber));
}
numberbase=2;

}
private void 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 = Integer.parseInt(txtOutput.getText());
txtOutput.setText(Integer.toOctalString(tempnumber));
}
if(numberbase==2){
int tempnumber;
tempnumber = Integer.parseInt(txtOutput.getText(),2);
txtOutput.setText(Integer.toOctalString(tempnumber));
}
if(numberbase==3){
// do nothing
}
if(numberbase==4){
int tempnumber;
tempnumber = Integer.parseInt(txtOutput.getText(),16);
txtOutput.setText(Integer.toOctalString(tempnumber));
}
numberbase=3;
}
private void jrbHexActionPerformed(java.awt.event.ActionEvent 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 = Integer.parseInt(txtOutput.getText());
txtOutput.setText(Integer.toHexString(tempnumber).toUpperCase());
}
if(numberbase==2){
int tempnumber;
tempnumber = Integer.parseInt(txtOutput.getText(),2);
txtOutput.setText(Integer.toHexString(tempnumber).toUpperCase());
}
if(numberbase==3){
int tempnumber;
tempnumber = 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 evt) {
txtOutput.setText(txtOutput.getText()+"B");
}
private void 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 evt) {
txtOutput.setText(txtOutput.getText()+"E");
}
private void btnFActionPerformed(java.awt.event.ActionEvent evt) {
txtOutput.setText(txtOutput.getText()+"F");
}
private void 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 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.parseDouble(txtOutput.getText()),2);
txtOutput.setText(Double.toString(temptotal));
}
}
private void 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 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.ActionEvent 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 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 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 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 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 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 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 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 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 Root";

23
lblSciName.setText(scimethod);
txtOutput.setText("");
}
}
private void 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 evt) {
if(helpflag==1){
help="Displays the value stored in the calculator's memory.";
JOptionPane.showMessageDialog(null,help);
helpflag=0;
}
else{
txtOutput.setText(Double.toString(memory));
}
}
private void 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 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 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 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.parseDouble(txtOutput.getText()));
txtOutput.setText(Double.toString(temptotal));
}
}
private void btnCubeActionPerformed(java.awt.event.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.parseDouble(txtOutput.getText()),3);
txtOutput.setText(Double.toString(temptotal));
}
}
private void btnCuberootActionPerformed(java.awt.event.ActionEvent 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.ActionEvent evt) {
double temptotal;
temptotal=method.power(Double.parseDouble(txtOutput.getText()),-1);
txtOutput.setText(Double.toString(temptotal));
}
private void btnHelpActionPerformed(java.awt.event.ActionEvent evt) {
helpflag = 1;
}
private void btnHistoryActionPerformed(java.awt.event.ActionEvent evt) {
String historybutton="";
for (int i=0;i<historycount;i++){
historybutton=historybutton+"\n"+history[i];
}
JOptionPane.showMessageDialog(null,historybutton);
}

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);
}
public double sin(double a){
return Math.sin(Math.toRadians(a));
}
public double cos(double a){
return Math.cos(Math.toRadians(a));
}
public double tan(double a){
return Math.tan(Math.toRadians(a));
}
public double asin(double a){
return Math.toDegrees(Math.asin(a));
}
public double acos(double a){
return Math.toDegrees(Math.acos(a));
}
public double atan(double a){
return Math.toDegrees(Math.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 – TEST PLAN

CALCULA CONDITIONS STEPS TO BE INPUT OUTPUT


ID
TION TO BE MET DONE GIVEN OBTAINED
Enter first
At least one number
number should Click on ‘+’
1. Addition 5+3 8.0
be entered before Enter second
clicking ‘+’ number
Click on ‘=’
Enter first
At least one number
number should Click on ‘-’
2. Subtraction 6-4 2.0
be entered before Enter second
clicking ‘-’ number
Click on ‘=’
Enter first
At least one number
Multiplicati number should Click on ‘*’
3. 5*3 15.0
on be entered before Enter second
clicking ‘-’ number
Click on ‘=’
Enter first
At least one number
number should Click on ‘/’
4. Division 15/3 5.0
be entered before Enter second
clicking ‘/’ number
Click on ‘=’
Enter first
At least one number
5
number should Click on ‘MOD’
5. Modulus MOD 1
be entered before Enter second
2
clicking ‘MOD’ number
Click on ‘=’
Enter first
At least one number
number should Click on ‘^’
6. Power 3^3 27.0
be entered before Enter second
clicking ‘^’ number
Click on ‘=’

EXP Button
Click on ‘EXP’
should be
7. EXP Enter number EXP 3 1000.0
clicked before
Click on ‘=’
entering number

At least one Enter first


8. Percentage 50 % 20 10
number should number

28
be entered before Click on ‘%’
clicking ‘%’ Enter second
number
Click on ‘=’
Enter a binary or
Octal or a
hexadecimal
9. Decimal - value 1010 10
Click on
‘Decimal’ to
convert
Enter a binary or
Octal or a
hexadecimal
Hexadecima
10. - value 1010 A
l
Click on
‘Hexadecimal’ to
convert
Enter a binary or
Octal or a
hexadecimal
11. Binary - value C 1100
Click on
‘Binary’ to
convert
Click on ‘AC’ to
12. All Clear - clear the text 1234 -
field
Click on ‘DEL’
13. DEL - to clear one by 1234 123
one
Click on
2.0+2.0=4
14. History - ‘History’ to view -
Sin 90.0=1.0
calculation log
A number should
Enter a value
15. Unary be entered before 9 -9.0
Click on ‘±’
clicking ‘±’
‘SIN’ should be
Click on ‘SIN’
clicked before
16. Sin Enter a value sin 90 1.0
entering a
Click on ‘=’
number
‘COS’ should be
Click on ‘COS’
clicked before
17. Cos Enter a value cos 60 0.5
entering a
Click on ‘=’
number
‘TAN’ should be
Click on ‘TAN’
clicked before
18. Tan Enter a value tan 30 0.577
entering a
Click on ‘=’
number
19. ASin ‘ASIN’ should Click on ‘ASIN’ ASin 1 90.0

29
be clicked before Enter a value
entering a Click on ‘=’
number
‘ACOS’ should
Click on ‘ACOS’
be clicked before ACos
20. ACos Enter a value 75.5
entering a 0.25
Click on ‘=’
number
‘ATAN’ should Click on
be clicked before ‘ATAN’ ATan
21. ATan 36.86
entering a Enter a value 0.75
number Click on ‘=’
A number should Enter a value
22. Square be entered before Click on ‘x²’ 5² 25.0
clicking ‘x²’ Click on ‘=’
A number should Enter a value
23. Cube be entered before Click on ‘x³’ 4³ 64.0
clicking ‘x³’ Click on ‘=’
‘√’ should be
Click on ‘√’
clicked before
24. Square Root Enter a value √16 4.0
entering a
Click on ‘=’
number
‘∛’ should be
Click on ‘∛’
clicked before
25. Cube Root Enter a value ∛8 2.0
entering a
Click on ‘=’
number
28. π - Click on ‘π’ - 3.14159…..
A number should
Enter a value
29. 1/x be entered before 1/2 0.5
Click on ‘1/x’
clicking ‘1/x’
A number should
Enter a value
30. n! be entered before 5! 120.0
Click on ‘n!’
clicking ‘n!’
‘LOG’ should be
Click on ‘LOG’
clicked before
31. log Enter a value LOG 50 1.6989
entering a
Click on ‘=’
number
A number should
Memory be present in the
32. Click on ‘MS’ - -
Save text field, in
order to be saved
Memory
33. - Click on ‘MR’ - 4.0
Recall
Memory
34. - Click on ‘MC’ - -
Clear

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 is bound to. They are listed 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 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

You might also like