Write a calculator program in java

Each package contains a set of related interfacesclasses, and exceptions. Refer to the separate platforms for a description of the packages available. Execution System Main articles: Java software platform and Java virtual machine One design goal of Java is portability, which means that programs written for the Java platform must run similarly on any combination of hardware and operating system with adequate runtime support.

Write a calculator program in java

I'm not going to go into the code you have posted in your questions, but rather attempt to help you attack your overall goal of writing a calculator demo. The first step should be to get a clear idea of the problem by writing it down: The calculator should read a series digits from the keypad, then a mathematical operator, then another series of digits and so on.

Each uninterrupted series of digits should be converted into an integer value. The calculator should calculate the result of applying the mathematical operator to the first and second integer values.

If further operators and integers are entered, the mathematical operator should be applied to the result of the prior calculation and the further integer.

This process should continue until the equals button is pressed. When digits are pressed on the keypad they should appear in the text display appended to those digits entered so far. When an operator or the equals button is pressed this signals the end of the entry of an integer, if this completes a calculation then the result should be shown on the display, otherwise the integer should remain in the display until another digit is pressed - then the display should be cleared and the new digit displayed, and subsequent digits appended, as before.

From this description we can identify some nouns: We then decide how to model these in our implementation. Here is one possible design: Let's start to make the bones of this: We will need to set up our JButtons to respond to being activated and wire up that event to a method we define in our Calculator class.

One way to do this is to create the JButtons and add a listener to them. We can make Calculator implement the ActionListener interface, which forces it to define a method actionPerformed with a single ActionEvent argument.

We can see how this would work by creating a JButton in Calculator's constructor.

write a calculator program in java

However, the code should illustrate how we can run code from a button press. Let's get on and get this code in a working state by setting everything up: Let's make digits That's because we have the default window layout which isn't what we need. We want the buttons to appear in a grid, grouped together in the window.

Nothing is permanent except change

Let's create a JPanel to group the buttons together in and use a GridLayout for the panel, then we can add the panel to the window. That's because they appear in the order added from top-left to bottom-right.

We can fix that by specifying the order we want with an array, and iterate through that we use the for-each style iterator for this because it's neater. While we're doing that, we can do something similar for the mathematical operators and equals button and add them to the frame too here we'll be explicit about the layout to use for the window and we'll use a BorderLayout.

We just need to add a display. First, let's see what we need to do if a digit is pressed.

Your Answer

We need to deal with the Append and Show verbs and append that digit to our input and display it. Now we need to implement the Convert verb and convert our digits into an integer.

This will happen when an operator or equals is pressed. We need to remember what operator was pressed so we know what calculation to do when the second integer is entered.

This isn't quite right. It will put into result the sum of the previous 2 integers rather than the sum of all integers entered so far. We need to store the first value in result then add each subsequent integer to the value stored in result. This means we don't need the previousInteger variable any more, and we have some duplicate code in both the if and else that we can just execute before the if.

Let's implement the other operators. We can solve this by skipping the calculation in this case. Here is the full code so far. It doesn't handle divide by 0, keypad is never used and can be removed, and integerEntered really need only be a local variable and not a instance variable.

However, the code should mostly work, let me know if you spot any problems. I also have a cleaner version the first implementation I did but it wasn't so simple to explain.I am a beginner in java and I have written this code with the help of an online available program.

The code attached is a part of the code which performs action when buttons of the calculator are pfmlures.com the CalculatorDemo class i have initialized all the buttons(b0-b24) and the TextField tf. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview .

If you already have the content you want to write to the file (and not generated on the fly), the pfmlures.com addition in Java 7 as part of native I/O provides the simplest and most efficient way to achieve your goals.. Basically creating and writing to a file is one line only, moreover one simple method call!.

The following example creates and writes to 6 different files to showcase. Java lies between Sumatra to the west and Bali to the east.

Borneo lies to the north and Christmas Island is to the south. It is the world's 13th largest pfmlures.com is surrounded by the Java Sea to the north, Sunda Strait to the west, the Indian Ocean to the south and Bali Strait and Madura Strait in the east..

Java is almost entirely of volcanic origin; it contains thirty-eight mountains. Calculator in Java with Source Code, see the example of calculator in java, Swing Tutorial with example of JButton, JRadioButton, JTextField, JTextArea, JList, JColorChooser classes that are found in pfmlures.com package.

write a calculator program in java

Below you will find project ideas. I have divided them into 10 different topic areas that I think the project may use the most. Keep in mind that a few of these projects could have .

beginner - Simple Java calculator - Code Review Stack Exchange