0% found this document useful (0 votes)
7 views7 pages

CALCULATOR

Uploaded by

harisivam01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

CALCULATOR

Uploaded by

harisivam01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CALCULATOR

AIM:
To develop an application using netbeans with four buttons to
perform the basic mathematical operations of addition,
subtraction, multiplication and division of any two numbers
entered by the user.

Steps:
First design the form with the following components:
 two editable text fields to accept the two numbers .

 four buttons to decide the operation, one button to


reset the fields and one button to exit out of the
application.

 one non-editable text field to display the result.

 appropriate labels to direct the user.

When the user enters two numbers and clicks on the +


button, the sum of the numbers is displayed in the
jtextField3 which has been disabled (by setting its
editable property to false)
When the user clicks on the RESET button the contents
of all the Text Fields are cleared.
SOURCE CODE:
private void jButton1ActionPerformed(java.awt.event.ActionEvent
evt)
{
// Code to add Number1 and Number2:
Double Number1,Number2,Result;
Number1=Double.parseDouble(jTextField1 .getText());
Number2=Double.parseDouble(jTextField2.getText() );
Result=Number1+Number2;
jTextField3.setText(Double.toString(Result));
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent
evt)
{
// Code to clear the contents of the text field:
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent
evt)
{ // Code to subtract Number2 from Number1:
double Number1,Number2,Result;
Number1=Double.parseDouble(jTextField1.g etText());
Number2=Double.parseDouble(jTextField2.getText());
Result=Number1-Number2;
jTextField3.setText(Double.toString(Result));
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent
evt)
{
// Code to multiply Number1 and Number2:
double Number1,Number2,Result;
Number1=Double.parseDouble(jTextField1 .getText());
Number2=Double.parseDouble(jTextField2.getText());
Result=Number1*Number2;
jTextField3.setText(Double.toString(Result));
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent
evt)
{
System.exit(0);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent
evt)
{
// Code to divide Number1 by Number2:
double Number1,Number2,Result;
Number1=Double.parseDouble(jTextField1 .getText());
Number2=Double.parseDouble(jTextField2.getText() );
Result=Number1/Number2;
jTextField3.setText(Double.toString(Result));
}
steps of computation:
 double Number1,Number2,Result;
 declares three variables of type double
Number1=Double.parseDouble(jTextField1.getText());
And
Number2=Double.parseDouble(jTextField2.getText());
retrieves the value entered by the user in the first and second
text field using getText(). These values by default are treated
as strings i.e. a group of characters and not as a number so
the string values need to be converted to a double type and
this is achieved using the parseDouble() method. After
converting it to a double type the values are assigned to the
variables declared in the first line of code
Result=Number1+Number2;
 The two values stored in the variables are added and
the calculated value is stored in the variable Result
jTextField3.setText(Double.toString(Result));
● The value stored in the variable Result is of type double so
it is first converted to type string using the toString() method
and then the display text of the third text field is set to the
converted value using setText()
 The working of the other three buttons (second, third
and fourth) is similar to the one explained above. We
are already familiar with the working of the STOP
button so let us give a quick look to the coding of the
RESET button
Output:

You might also like