Create a Functional Calculator with NetBeans IDE

A Functional Calculator

NetBeans IDE is used to create Java application. We will create a functional calculator with this IDE.

Requirments:

Features:

  • Use of both mouse and keyboard to input numbers
  • Attractive design
  • Functional
  • Easy to use

Steps to create calculator

Step 1: Design in NetBeans IDE
Drag and drop the text field to show results and inputs. And buttons for inputs.
Step 2: Coding
Write code for the action that is performed after clicking the buttons.

Source code:

public double c;
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(“”);
}
private void btn9ActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(txtDisplay.getText()+”9″);
}
private void bt8ActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(txtDisplay.getText()+”8″);
}
private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(txtDisplay.getText()+”7″);
}
private void btn6ActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(txtDisplay.getText()+”6″);
}
private void btn5ActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(txtDisplay.getText()+”5″);
}
private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(txtDisplay.getText()+”4″);
}
private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(txtDisplay.getText()+”3″);
}
private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(txtDisplay.getText()+”2″);
}
private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(txtDisplay.getText()+”1″);
}
private void btn0ActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(txtDisplay.getText()+”0″);
}
private void btnPeriodActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText(txtDisplay.getText()+”.”);
}
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
c= Double.parseDouble(txtDisplay.getText());
txtDisplay.setText(“+”);
}
private void btnSubActionPerformed(java.awt.event.ActionEvent evt) {
c= Double.parseDouble(txtDisplay.getText());
txtDisplay.setText(“-“);
}
private void btnMultiActionPerformed(java.awt.event.ActionEvent evt) {
c= Double.parseDouble(txtDisplay.getText());
txtDisplay.setText(“x”);
}
private void btnDivActionPerformed(java.awt.event.ActionEvent evt) {
c= Double.parseDouble(txtDisplay.getText());
txtDisplay.setText(“/”);
}
private void btnAnsActionPerformed(java.awt.event.ActionEvent evt) {
String a=txtDisplay.getText();
char x= a.charAt(0);
if (x==’+’ || x==’-‘)
{
double b=Integer.parseInt(a);
txtDisplay.setText(c+b+””);
}
else if (x==’x’)
{
a= a.replace(‘x’,’0′);
double b=Integer.parseInt(a);
txtDisplay.setText(c*b+””);
}
else if (x==’/’)
{
a= a.replace(‘/’,’0′);
double b=Integer.parseInt(a);
txtDisplay.setText(c/b+””);
}
}

Logic behind the coding

First, we will get the input from the keyboard or mouse. When button are clicked then concatenation occurs.
After, operation button is pressed, the string is braked and stored in another variable.
Finally, the result or equal button is pressed calculation occurs according to the first operand of second string.
Clear button will clear the text field.
Download the actual project via link below.
Download
Similar projects:
https://infocode.org/a-simple-java-calculator/

Leave a Reply

Your email address will not be published. Required fields are marked *