import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator implements ActionListener{ JFrame frame; JTextField textfield; JButton[] numberButtons = new JButton[10]; JButton[] functionButtons = new JButton[9]; JButton addButton,subButton,mulButton,divButton; JButton decButton, equButton, delButton, clrButton, negButton; JPanel panel;
Font myFont = new Font("Ink Free",Font.BOLD,30);
double num1=0,num2=0,result=0; char operator;
Calculator(){
frame = new JFrame("Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(420, 550); frame.setLayout(null);
textfield = new JTextField(); textfield.setBounds(50, 25, 300, 50); textfield.setFont(myFont); textfield.setEditable(false);
addButton = new JButton("+"); subButton = new JButton("-"); mulButton = new JButton("*"); divButton = new JButton("/"); decButton = new JButton("."); equButton = new JButton("="); delButton = new JButton("Del"); clrButton = new JButton("Clr"); negButton = new JButton("(-)");
it would be more easy to understad for beginners like me if you go slower than usual.. because i don't understand many terms like "ActionListener", interfaces etc.. it's like new to me since I'm a beginner..
@NOA777 the method Double.parseDouble() casts a value to a double type value. the getText() method is used to get the text from the text field. So Double.parseDouble(txtfield.getText()) is actually casting the string values to a double type value of whatever is written in the rext field as we can't perform calculation with string type of values.
00:03 How to make a simple calculator program using Java 03:50 Creating a Java calculator app with J buttons and J panel 07:05 Adding text field and buttons to the calculator app 10:42 Creating an array of JButtons 14:32 Adding delete and clear buttons to the calculator app interface. 17:54 Adding buttons to the panel 21:34 Adding functionality to decimal button and various math operations 26:25 Implementing Clear and Delete functionality in Java calculator app 30:17 Added functionality to the negative button, allowing the user to flip the sign of the number displayed in the text field. 33:44 How to make a very simple calculator using Java
This tutorial was very good and even gave the challenge of fixing the Delete and Clear font size while keeping the rest of the numbers and functions font the same size
This channel is so underrated I went through many tuto on youtube, but none of them was that good(sincerely), and this is in regards to any programming language This developer is Gold
This is an amazing tutorial, thumbs up. I would have loved to see how you handled division by 0. I have also noticed that its possible to have multiple dots in a number which would result in an error during calculations.
little bit late, but came across the tutorial right now. for multiple dots I did (most likely theres a better solution): if(e.getSource() == decButton) { boolean alrDec = false; for(int i = 0; i
I had a few problems with this application and I fixed one. It looks like when you use the negative button you can't click it first then click the number button you want to be negative. It will cause the program to crash. To fix this problem you can use a try and catch method plus a NumberFormatException. The NumberFormatException is an unchecked exception in Java that occurs when an attempt is made to convert a string with an incorrect format to a numeric value. Therefore, this exception is thrown when it is not possible to convert a string to a numeric type (e.g. int, float). It has this problem with negative values. However, the negative button works when you click the number button first then click the negative button. I still put this exception in just in case someone clicks the negative button first. It isn't a great fix because I would like the negative button to work both ways so it won't matter which you click first the number or the negative button. When I figure this out then I will update folks. First, I will attempt to replicate what my phone calculator does with negative buttons. They have a +/- option that has a few different behaviors that I can implement. The second problem I identified is that the functionality of the negative button doesn't work. I checked Bro Code's source code and copied then pasted it on my Eclipse file but still has the same problem. I knew it would but I wanted to make sure it wasn't just me. When you trying adding or multiplying or dividing a number against a negative number it will convert the negative number to -1. No matter what value your negative number is. For example it's -8, it will convert it to -1. Example 1, 3 * -8 = -24, but the calculator will process the equation like this: 3 * -1 = -3. I think maybe I can add a case to the switch statement so I'm going to try that first. Or I can add a loop to the negative button to address the -1 problem. As I said before, when I have solutions for this then I will post it to my comment thread.
Update: I found a solution to the problem I identified with negative numbers. This solution I tested and it works. Negative value numbers won't automatic be converted to -1 but will have the value you intended. Code: if (e.getSource() == negButton) { double num = Double.parseDouble(textfield.getText()); if (num >= 0) { double resultneg = num * -1; textfield.setText(resultneg + " "); } else { double resultpos = num * -1; textfield.setText(resultpos + " "); } If you notice I changed the button to no longer just be a negative button but a +/- button. I did this because of the calculator apps today on phones and computers are setup with +/- functions.
@@halimaomar9820 if else condition to check num > 0 or less than or equal to zero , pretty much does the same function as the original bro code.Though it checks explicitly,the results are the same Btw, the two possible errors that you have spotted out are correct but unfortunately there is no solution for those in your code
I just begun with Java and made my own calculator like this. But after finishing and seeing this video, there is a lot of things I could do to definetly reduce my code size.
Hey I was wondering if you could help me, I'm making this using IntelliJ right now and at 6:40 when I run it to check if a frame comes up I get the error: "Error: Could not find or load main class Main" and I see you don't have a main class either but for some reason yours works?
Dear Bro you are simply amazing... ❤❤💐💐being sooo... generous in sharing your wealth of knowledge and you certainly deserve a very big appreciation for making me impressed to learn Java programming. God Bless You Bro...🙏🙏. I really loved it and impatient to try it ASAP..... 👌👌👍👍
u can add this also to prevent adding more than one decimal point if (e.getSource() == decButton) { //programming the decimal buttons String temp = textField.getText(); if (!temp.contains(".")) { //preventing adding more than one cecimal textField.setText(temp + "."); } }
Thanks a lot. I need another help. I want to add the feature that is if I press Clr button it should show 0. I can easily do this by just putting textfield.setText("0"); in the clr button function. But the problem arises after that. If any number is given as input after pressing Clr button the zero stays before them, I want the zero disappear if any button is pressed after that. How can I do so?
Wow even though your style is different, its super easy to understand and frankly, might make me do my calculator over just because It seems easier the way you did it
In new in java , I found that you can type 0,3,4,5,56,6 it take me some time to fix it , also remove 5+2= 7.0 , now working on implement the keyboard . Oh , and eliminate the empty space every time you push +or / or whatever operator.
i just finish second book about java. A Beginners Guide and Complete Reference from Herbert SCHİLDT. I was looking for an example of real GUI software that wasn't beyond my knowledge and actually did something. So I could get an idea of the general programming structure. Your video help me a lot about this. Thank you.
for delete button use below code:- if(e.getSource()==delButton) { String string = textfield.getText(); textfield.setText(string.substring(0,string.length()-1)); } everytime it will delete the last elemnt of the string.
I got no idea how do you guys remember the what classes inside the libraries , should we just refer documentation,it's very confusing for me how to use these libraries using documentation
import javax.swing.AbstractAction; import java.awt.AWTError; import java.awt.AWTEvent; My Netbins adds(the last dot continuations) to the original code.How can i fix this?.
thanks for the video. for the decimal point button, i added a little bit of if statement to prevent the program from adding more dots to the textfield: if(e.getSource()==decButton) { String myString = textfield.getText(); if (!myString.contains(".")) { // Check if there is no dot already present textfield.setText(textfield.getText().concat(".")); } }
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener{
JFrame frame;
JTextField textfield;
JButton[] numberButtons = new JButton[10];
JButton[] functionButtons = new JButton[9];
JButton addButton,subButton,mulButton,divButton;
JButton decButton, equButton, delButton, clrButton, negButton;
JPanel panel;
Font myFont = new Font("Ink Free",Font.BOLD,30);
double num1=0,num2=0,result=0;
char operator;
Calculator(){
frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 550);
frame.setLayout(null);
textfield = new JTextField();
textfield.setBounds(50, 25, 300, 50);
textfield.setFont(myFont);
textfield.setEditable(false);
addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
decButton = new JButton(".");
equButton = new JButton("=");
delButton = new JButton("Del");
clrButton = new JButton("Clr");
negButton = new JButton("(-)");
functionButtons[0] = addButton;
functionButtons[1] = subButton;
functionButtons[2] = mulButton;
functionButtons[3] = divButton;
functionButtons[4] = decButton;
functionButtons[5] = equButton;
functionButtons[6] = delButton;
functionButtons[7] = clrButton;
functionButtons[8] = negButton;
for(int i =0;i
it would be more easy to understad for beginners like me if you go slower than usual.. because i don't understand many terms like "ActionListener", interfaces etc.. it's like new to me since I'm a beginner..
Nice👍🔥
@@komaltandle465 LOL
@NOA777 the method Double.parseDouble() casts a value to a double type value. the getText() method is used to get the text from the text field. So Double.parseDouble(txtfield.getText()) is actually casting the string values to a double type value of whatever is written in the rext field as we can't perform calculation with string type of values.
@NOA777
The method parseDouble() of wrapper class Double, convert string to double.
People are missing out on your amazing content. I am still learning Java, and your content helps a lot.
how are you now on your Java journey?
@@Bryysanity Im curious too
00:03 How to make a simple calculator program using Java
03:50 Creating a Java calculator app with J buttons and J panel
07:05 Adding text field and buttons to the calculator app
10:42 Creating an array of JButtons
14:32 Adding delete and clear buttons to the calculator app interface.
17:54 Adding buttons to the panel
21:34 Adding functionality to decimal button and various math operations
26:25 Implementing Clear and Delete functionality in Java calculator app
30:17 Added functionality to the negative button, allowing the user to flip the sign of the number displayed in the text field.
33:44 How to make a very simple calculator using Java
Came here straight after the 12 hour java tutorial. You are Awesome Bro. Keep 'em coming :)
Started practice with GUI's and this helped a lot!
off course same here bro😎
Most impressive. Very lucid coding. Congrats on teaching me a little more than I knew!
This tutorial was very good and even gave the challenge of fixing the Delete and Clear font size while keeping the rest of the numbers and functions font the same size
This channel is so underrated
I went through many tuto on youtube, but none of them was that good(sincerely), and this is in regards to any programming language
This developer is Gold
Thank you, Bro! My prayers to the algorithm.
Bro You are legend. I am not a student I started learning JAVA to make my travel agency CRM.
Extremely helpful. Got my mini project done within 30 minutes referring your source code and guide. Thankkksss a lotttt ☺👏
Please, don't stop!!! More and more tutorials!!!!
the first sentence (Please, don't stop!!) remember me of a chat i had i chai 😏😏
@@Zito_from_OHIO
you are really a great teacher you have made my life easy hahahah... God bless you sir
This is an amazing tutorial, thumbs up. I would have loved to see how you handled division by 0. I have also noticed that its possible to have multiple dots in a number which would result in an error during calculations.
if(e.getSource() == decButton)
{
if(textfield.getText().contains("."))
{
String temp = textfield.getText();
textfield.setText("");
for(int i=0;i
little bit late, but came across the tutorial right now.
for multiple dots I did (most likely theres a better solution):
if(e.getSource() == decButton) {
boolean alrDec = false;
for(int i = 0; i
I just start learning java a month ago and a little bit confuse what to do, and i found your channel, its really help me to practice
I had a few problems with this application and I fixed one. It looks like when you use the negative button you can't click it first then click the number button you want to be negative. It will cause the program to crash. To fix this problem you can use a try and catch method plus a NumberFormatException.
The NumberFormatException is an unchecked exception in Java that occurs when an attempt is made to convert a string with an incorrect format to a numeric value. Therefore, this exception is thrown when it is not possible to convert a string to a numeric type (e.g. int, float). It has this problem with negative values.
However, the negative button works when you click the number button first then click the negative button. I still put this exception in just in case someone clicks the negative button first.
It isn't a great fix because I would like the negative button to work both ways so it won't matter which you click first the number or the negative button. When I figure this out then I will update folks. First, I will attempt to replicate what my phone calculator does with negative buttons. They have a +/- option that has a few different behaviors that I can implement.
The second problem I identified is that the functionality of the negative button doesn't work. I checked Bro Code's source code and copied then pasted it on my Eclipse file but still has the same problem. I knew it would but I wanted to make sure it wasn't just me.
When you trying adding or multiplying or dividing a number against a negative number it will convert the negative number to -1. No matter what value your negative number is. For example it's -8, it will convert it to -1. Example 1, 3 * -8 = -24, but the calculator will process the equation like this: 3 * -1 = -3. I think maybe I can add a case to the switch statement so I'm going to try that first. Or I can add a loop to the negative button to address the -1 problem.
As I said before, when I have solutions for this then I will post it to my comment thread.
Update: I found a solution to the problem I identified with negative numbers. This solution I tested and it works. Negative value numbers won't automatic be converted to -1 but will have the value you intended.
Code:
if (e.getSource() == negButton) {
double num = Double.parseDouble(textfield.getText());
if (num >= 0) {
double resultneg = num * -1;
textfield.setText(resultneg + " ");
} else {
double resultpos = num * -1;
textfield.setText(resultpos + " ");
}
If you notice I changed the button to no longer just be a negative button but a +/- button. I did this because of the calculator apps today on phones and computers are setup with +/- functions.
Full solution code fixing all the bugs/errors:
try {
if (e.getSource() == negButton) {
double num = Double.parseDouble(textfield.getText());
if (num >= 0) {
double resultneg = num * -1;
textfield.setText(resultneg + "");
} else {
double resultpos = num * -1;
textfield.setText(resultpos + "");
}
}
}
catch (NumberFormatException num) {
System.out.println(
"NumberFormatException occurred");
}
@@halimaomar9820 if else condition to check num > 0 or less than or equal to zero , pretty much does the same function as the original bro code.Though it checks explicitly,the results are the same
Btw, the two possible errors that you have spotted out are correct but unfortunately there is no solution for those in your code
well I am just commenting to support you because this video has been really helpful and i learnt more ways to use GUI in java.
You always perfect sir because your way of teaching is all the time perfect. Thank you so much sir.🥰
I knew bro couldn't miss the negative sign before. He just cares for us bros and was testing us to grow stronger💪
Thanks man. Believe it or not Ive been learning Java for years and until now Ive never actually wrote a fully functional calculator.
bro this was really awesome ....
my sir told it but it was too confusing
but your program was clean and neat....
thank you so much .
Thanks Bro I've learned something new today about Java especially that GUI.
Cool, this is nice.
Awesome video.
And also, I have finished creating my AI ChatBot!!
nice! Which app is it for?
I just begun with Java and made my own calculator like this. But after finishing and seeing this video, there is a lot of things I could do to definetly reduce my code size.
Thank you very much for your assistance, you explained the code perfectly. Thumb Up.
You are a true Bro. Thank you very much sir! Everyone keep commenting, liking and subbing to help the algorithm!
Bro the goat. Just finished learning Java with you and am building my first project with you too
best yt for coding i learned many things from you thanks you so much bro code
Thank you @BroCode ☺
It was really up to the point !
yo bro, love it
thanks
keep up the good work
This was indeed Helpful brother
Thanks for the tutorial
the package come back as error what to do
Bro code,this is an amazing tutorial.Thank you Mann,you taught me alot here.
Thanks a million , It was really nice and perfect for a beginner like me
Amazing content as always man, thank you!
Hey I was wondering if you could help me, I'm making this using IntelliJ right now and at 6:40 when I run it to check if a frame comes up I get the error: "Error: Could not find or load main class Main" and I see you don't have a main class either but for some reason yours works?
You're the man. Good job!
no caption king. learned a lot from you . thnaks for the quality content
Dear Bro you are simply amazing... ❤❤💐💐being sooo... generous in sharing your wealth of knowledge and you certainly deserve a very big appreciation for making me impressed to learn Java programming. God Bless You Bro...🙏🙏. I really loved it and impatient to try it ASAP..... 👌👌👍👍
u can add this also to prevent adding more than one decimal point
if (e.getSource() == decButton) {
//programming the decimal buttons
String temp = textField.getText();
if (!temp.contains(".")) {
//preventing adding more than one cecimal
textField.setText(temp + ".");
}
}
Thanks a lot. I need another help. I want to add the feature that is if I press Clr button it should show 0. I can easily do this by just putting textfield.setText("0"); in the clr button function. But the problem arises after that. If any number is given as input after pressing Clr button the zero stays before them, I want the zero disappear if any button is pressed after that. How can I do so?
the functionality button for me isnt working
Thanks man
You explain java better than my teacher
Thanks pro
Very helpful video
Keep going
thanks for clean work it really help me !you are super good :)
man Thank you i really need this tutorial for my computer programming 2
Your amazing Content makes me to passionate about java more...
I thought to left but ...
Polich machaa..
You are great 👏🏻👏🏻
awesome broo..keep rocking
i truly learn a lot man thanks
Excellent as always bro!
Thank you so much for the perfect explanation ❤❤❤❤😭
Thankyou for your valuable content 😍
Thank you so much bro. this is really amazing
Thanks
Wonderful video ! This video is easy to understand and very helpful.
Awesome content !
Amazing video I really enjoy this as this is very useful in my program
Very beautiful, Love it!
Bro! These videos are awesome 👏
Wow even though your style is different, its super easy to understand and frankly, might make me do my calculator over just because It seems easier the way you did it
Amazing content, thank you!
In new in java , I found that you can type 0,3,4,5,56,6 it take me some time to fix it , also remove 5+2= 7.0 , now working on implement the keyboard . Oh , and eliminate the empty space every time you push +or / or whatever operator.
very helpful video. i understand for loops, if() more than before
i just finish second book about java. A Beginners Guide and Complete Reference from Herbert SCHİLDT. I was looking for an example of real GUI software that wasn't beyond my knowledge and actually did something. So I could get an idea of the general programming structure. Your video help me a lot about this. Thank you.
for delete button use below code:-
if(e.getSource()==delButton) {
String string = textfield.getText();
textfield.setText(string.substring(0,string.length()-1));
}
everytime it will delete the last elemnt of the string.
Your programme is not right bro
@@AnsarAbbas-ed5ig bro I have tested this code then uploaded here.
@@dipesh1401 OK bro i hope it would correct
Another great video. Thanks!
Incase you guys didnt know, calc is short for calculator, im just speaking in slang...
Great, fantastic. It may seem a little but too advanced at the beginning, but you can try it and see:)
Thanks for the help Big Bro!
Thanks! (I appreciate also the 420 on the preview)
so enjoyable, thanks for this Man
Amazing video. That robotic laugh at the end got me 😂
That’s was a lot of code thanks for your effort
big thanks, very helpful video! subbed
Thank you so much bro you are the only best coder
Very cool! Helped me alot!
Nice content keep it up!!!🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
Wonderful Tutorial
Awesome tutorial
well done bro🙌 thank you!!
Great tutorial, thank you.
Good day BRO! Thanks so much I can now code my simple calculator, through your tutorial.
Very well explained 👍
Bro, that was awesome ❤️
Thanks Bro, great video!
I love your channel.
This was really helpful
I liked very much this video and i from Buenos Aires, Argentina
thanks bro for this video!! very helpful :)
I got no idea how do you guys remember the what classes inside the libraries , should we just refer documentation,it's very confusing for me how to use these libraries using documentation
import javax.swing.AbstractAction;
import java.awt.AWTError;
import java.awt.AWTEvent;
My Netbins adds(the last dot continuations) to the original code.How can i fix this?.
Thanks man that helped a lot. Nw I see how Java works.
in this programme , if you put par exemple 5*3*2 = it gives you 6 not 30 , and thank u for every thing else ur a lagende Bro
I just finished this and I’m sooo happy n I feel awesome😭🫂
hey bro! thanks a lot for this amazing content..
Awesome job I like it Thanks
thank you so much bro code it help me a lot when i watch your vedio. love yah
By the way chat calc is short for Calculator, he is just using slang
Thank you so much for making this wonderful vidoes ❤️
Thank you very much! It was very helpful.
great example. Thanks
why didn't you used id 'string = string.substring(0, string.length() - 1)' at 28:10?
thanks for the video.
for the decimal point button, i added a little bit of if statement to prevent the program from adding more dots to the textfield:
if(e.getSource()==decButton)
{
String myString = textfield.getText();
if (!myString.contains(".")) { // Check if there is no dot already present
textfield.setText(textfield.getText().concat("."));
}
}