New Post has been published on https://semicolon.codes/calculator-app-java-source-files/
Calculator App in Java with NetBeans IDE- Source Files
This Calculator App that can be used for Java project . Made with quite simple logic and works well, However, it’s for beginner to learn easily.
In the design, i made a simple quite calculator like interface which was quite good at final look(Not so much great 🙂 ). I tried to use functions and constructor. To reduce redundant codes that would go same for getting values from the buttons i placed and for arithmetic functions on the Calculator:
Inserting Buttons with Number values and arithmetic operations
Therefore, I placed some textfield and buttons on the first and also named them so that i can remember them easily.
Buttons : 0-9 (each with unique variable name)
Textfield : One large text field to show input and result
Extra buttons for +,-,*,/ And Clear.
Design looks something like this:
Lets code some functions and variables
Global variable with public access
At the coding part, first i made some global variables that would hod the values for our first number, second number ,arth (arithmetic operations) and result;
class Calc public String num1 = ""; public String num2 = ""; public String arth = ""; public String result= ""; other
To make global variable, it has to be defined on the top and public key has to be used to be accessible to all functions ( more info on file).
So the value input on the buttons are extracted as String and we have to convert it into integer later on for arithmetic operations cause we can’t do it if its string.
Extracting Values from Buttons and some conditions
So for getting value from each button, I simply made a function:
set(jButton10.getText()); //function to gets text from button with getText() function public void set(String a) //Function definition if(arth == "") num1 = display.getText() + a; display.setText(num1); else num2 = display.getText() + a; display.setText(num2);
This function stores value in num1 and num2.
There is a condition too which checks if the arth variable is empty means num1 has’t been set (simple logic) and puts value in num1 with concatenation else put value in num2.
public void clear() if(result != "") display.setText(""); result = ""; num1 = ""; num2 = ""; arth = ""; public void arth(String a) if(num1 != "") arth = a; if(result != "") num1 = display.getText(); num2 = ""; result = ""; display.setText("");
I used clear function to clear everything on textfield and on variable when there is result while user ends one operation and starts another.
Another arth function is to put operation sign on the variable when users clicks on the corresponding button.
Main arithmetic function of the calculator
public double operations(String a, String b, String c) double num1 = Double.parseDouble(a); double num2 = Double.parseDouble(b); String choice = c; double result = 0; switch(choice) case "+": result = num1+num2; break; case "-": result = num1-num2; break; case "*": result = num1*num2; break; case "/": result = num1/num2; break; default: break; return result;
The main operation on the Calculator App is operation function which is actually quite simple. This function it takes three parameter: string a , b ,c ( a= num1 ,b=num2 and c = arth).
Then it is parsed into double which also takes decimal values in case user attempts decimal operations. And it is executed by a switch function with c which holds “arth” value and decides the case.
Returning value and setting on TextField
The result is returned in double as we defined the double datatype function on the top and setText() is used to display the value in textfield.
Sorry if my phrases are bit confusing, here’s the link to the actual file so that you can understand more by looking at the codes.
Don’t worry , the file will be unlocked as soon as the following task is completed. If there is an error , please comment down below:
jQuery(function()jQuery('.link-btn a.wpdm-download-link img').after('<img src="https://semicolon.codes/wp-content/plugins/wpdm-download-button/images/03.png" alt="" />');jQuery('.link-btn a.wpdm-download-link img').remove(););