Creating a java calculator

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I'm trying to create a basic calculator in Java.

I'm quite new to programming so I'm trying to get used to it. This is my code. It prompts for the numbers and operation, but displays the answers all together? Remove the semi-colons from your if statements, otherwise the code that follows will be free standing and will always execute:.

Also use. And the ; after the conditions was an empty statement so the conditon had no effect at all.

creating a java calculator

If you use java 7 you can also replace the if statements with a switch. Learn more. Basic calculator in Java Ask Question. Asked 7 years, 2 months ago. Active 2 years ago. Viewed k times. Scanner; import javax.

Creating a Calculator program in Java

Aruna Is this programming homework? This is a very common first-year task in CS. Active Oldest Votes. Here is simple code for calculator so you can consider this import java. Mohamud Mohamud 1 1 silver badge 2 2 bronze badges. While this code technically works, it does so by operating significantly differently from the code in the question, in a way that avoids addressing any of the bugs in that code.

Reimeus Reimeus k 12 12 gold badges silver badges bronze badges. Damn you by the way. Being a beginner programmer myself, I thought I'd finally be able to answer a question! Alas, not quick enough!! CompareStrings with equals. MrSmith42 MrSmith42 8, 5 5 gold badges 33 33 silver badges 46 46 bronze badges. Java program example for making a simple Calculator: import java.

A Calculator Example Program (With Java Code)

Linkon Linkon 8 8 silver badges 12 12 bronze badges. Jagger 8, 7 7 gold badges 40 40 silver badges 74 74 bronze badges. Ronak Mandania Ronak Mandania Answering please try to add some natural text to explain your solution at least a little bit. Ekonion Ekonion 1 1 gold badge 6 6 silver badges 26 26 bronze badges.

Dilip Kumar Dilip Kumar 1. Addition" ; System.We will start by installing NetBeans. Download the whole IDE bundle version depending upon your processor architecture. Assume that you have successfully installed NetBeans on your machine. Start NetBeans from the Window. The NetBeans main window will appear as shown below. The NetBeans main window contains menus, toolbars, project pane, files pane, runtime pane, navigator pane, and other similar panes.

creating a java calculator

Creating a Blank Project To create and run a program, you have to first create a project. Here are the steps to create a demo project. The new project box enables you to specify the project type. Step 2 : — Choose the Java type from the given list. Step 3 : — From the New Application Tab, enter the project name and unselect the main class from it. Uncheck the Main class option from the project tab. Step 2 : — To add a Jframe, right click the project name in the Project window.

Step 3 : — To the right of the Jframe, there is a Palette with a set of controls on it. Click the Text Field and apply it on the blank Field form. Now click Propertiesthen change the text field to blank. Click on the Code and change the Variable Name to txtDisplay. Step 4 : — Select the Button and drag it to the Jframe form. Change the text and the Variable Name as we did for the text field. Enter the value 1 for the text by selecting the properties. Do the same for all the buttons by changing the text and the Variable Name.

The Final Jframe structure will be formed. Step6 : — Double click on any of the button. This will take the user to the source section where the major coding of the program is done.

creating a java calculator

The image below depicts the Source section. The firstnum term is to accept the first number entered, whereas the secondnum is to accept the second number stored. The variable result is used to store the output value. Writing Code for the Number Button on Java Calculator To get a text from a form object, you can use the getText method of the object if it has one. So to get the text from btnOne we could use this code:.

Arithmetic Codes for the Button The code for the Addition operation is as shown below. The parse statement is used in order to convert the string argument as a signed decimal integer. The number entered into the EditText textbox is taken from the EditText and stored temporarily in a variable. Therefore the getText method can be used to retrieve the data, and the setText method to display the data.

The elswhere. Similarly, do this for all the other logics to be performed.Your email address will not be published. Post Views: 11, URL; import java. ResourceBundle; import javafx. ChangeListener; import javafx. ObservableValue; import javafx.

ActionEvent; import javafx. FXML; import javafx. Initializable; import javafx. Button; import javafx. URL. ResourceBundle. ChangeListener. ObservableValue. ActionEvent. FXML. Initializable. Button. TextField. TextField outputTF .By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up. I am a beginner in Java programming. Here is my simple calculator. I hope you could scan through my coding and let me know if there is any error in my coding or how I could simplify the code.

Couple tweaks if you want to make it look nicer. The name of the class should be somewhat describing the purpose so in your case Calculator would fit better. Then next tweak could be to do the System. Another suggestion would be to make 4 classes implementing binary operations with just one method taking two arguments and returning the result. Then you can have a map with a key being the operation in enum or string and the value would be the appropriate class.

Then you end up with more classes but this main method gets shorter. Generally it is better to split the code in more methods and classes and keep the motto "one class one responsibility", or Single Responsibility Principle. So as an example each of your arithmetic classes would be responsible for a single arithmetic operation. If you want to follow that completely, then you would have to create a class that gets the input from the user as well.

The Calculator class then would be just like a coordinator saying: give me numbers, give me an operator, perform the operation and print out the result at the end. Really, there's only one criticism, and a few suggestions. The criticism is designed to get you in to the right habits, it's not a major bug Java, for years, has had a problem with people being lazy about closing resources These 'forgotten' resources can sometimes lead to unexpected bugs and deadlocks.

In Java7, the concept 'try-with-resources' was added to ensure that resources are guaranteed to be closed nicely whether the code succeeds or not. It actually allows the programmer to be even lazier than before, and get a an even better result. As your program expands, you will find that there are a few things that become 'uncomfortable' What you have at the moment is great for it's purpose, but I encourage you to try to find ways to expand on the idea.

Research you may want to do is in to things like 'Reverse Polish Notation RPN ' which is an easier-to-parse format for writing mathematical expressions. In order to do these more expansive operations you will need to find ways to turn operators in to classes probably an Enumand have classes that allow you to encapsulate an expression unary or binary expressions.

You shouldn't have to instantiate more than one scanner, as your numbers and the operation are coming from the same input stream.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Code Review Stack Exchange is a question and answer site for peer programmer code reviews.

It only takes a minute to sign up. Today, I made a calculator in JFrame. The code is a little long but I'm still a beginner to Java. In the future I'm going to do some improvements on it, but here it is right now.

This is the meat of the calculator:. I'll start with what stands out. If you're worried about positioning consider using a layout manager and placing the buttons within, e. Your many static methods are very similar, you can simplify them by using an interface method also using lambda expressions. Sign up to join this community. The best answers are voted up and rise to the top.

Home Questions Tags Users Unanswered. Asked 4 years, 9 months ago. Active 2 years, 6 months ago. Viewed 33k times. Legato 9, 4 4 gold badges 38 38 silver badges bronze badges. Ethan Cotler Ethan Cotler 1 1 gold badge 1 1 silver badge 5 5 bronze badges. I hope you get some fine answers! Active Oldest Votes. Legato Legato 9, 4 4 gold badges 38 38 silver badges bronze badges. And also thanks for the feedback! Now I can shorten up my code a bit! Jul 20 '15 at Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.After the "Hello World! The reason for this is because of the simplicity of its structure in addition to covering most of the basic concepts in programming. Follow the steps below and you too will be able to build your own fully functioning calculator!

Did you use this instructable in your classroom? Add a Teacher Note to share how you incorporated it into your lesson. All software programs are written on Development Environments, programs made specifically to build and compile software. For the programming language we're using, Java, the program Dr. Java is an excellent and simple introductory program to use. Start by downloading the program from www. After you first open Dr. Java, you'll be greeted with a blank screen of nothing. So beautiful and pristine, we're going to cover this baby with a lot of code.

Start off by declaring a public Calculator class and then inside of it, a main method, exactly like shown in the picture. In Java, a class is simply an object which holds information. In our case, the object we're building is a calculator so we have named it as such.

A method is a process which performs a certain task. All methods follow the IPO structure as we will learn later. Brackets in java denote what is contained in what. In our example, our calculator object contains the method we are going to build.

The brackets of the method is where our code is going to be contained. As mentioned in the last step, all methods follow the IPO structure, short for input, process, output structure. This means that in short all methods receive some form of input, perform a process, and return an output. First we're going to declare the inputs. Inputs can be of different data types so we have to be specific in our declaration. The main data types are int i. For our example we need to declare two int type numbers and a char type operator which will signify the operation we want to perform.

Also one thing we must note are the use of semi-colons. In coding these serve almost as periods and signify breaks in line for the computer reading the code. Now we begin the process portion of the method.

creating a java calculator

The first thing we must build is a method for the user to enter whatever type of input we like. We can achieve this with the use of a scanner which stores any data the user types in an assigns it to the inputs we declared previously.

We import the scanner object top linedeclare one for use, and then type three lines asking the user for input and then assigning those values.During these challenging times, we guarantee we will work tirelessly to support you.

We will continue to give you accurate and timely information throughout the crisis, and we will deliver on our mission — to help everyone in the world learn how to do anything — no matter what. Thank you to our community and to all of our readers who are working to aid others in this time of crisis, and to all of those who are making personal sacrifices for the good of their communities.

We will get through this together. This article offers a quick and easy way to create your very own tip calculator, allowing you to input a number and calculate the tip automatically, without doing your own mental math. Log in Facebook Loading Google Loading Civic Loading No account yet? Create an account. We use cookies to make wikiHow great. By using our site, you agree to our cookie policy.

Java Programming Tutorial - 12 - Switch Statement

As the COVID situation develops, our hearts ache as we think about all the people around the world that are affected by the pandemic Read morebut we are also encouraged by the stories of our readers finding help through our site. Article Edit. Learn why people trust wikiHow.

Subscribe to RSS

Author Info Updated: June 23, To create this article, volunteer authors worked to edit and improve it over time. This article has also been viewed 12, times. Learn more Explore this Article Steps. Related Articles. To download Netbeans, go to the Netbeans. Since the tip calculator is a relatively simple application, you only need to download Java SE standard edition. Once you finish downloading the.

The standards options in the installer are sufficient for this program, so you can download the standard edition without fear of not having a required components for the program. Download the Java JDK. Run the NetBeans program and create a new project. Go to the dropdown menu at the top left that says File and select New Project.


Leave a Reply

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