Tuesday, July 14, 2009

Java codes: How to write a java application (using GUI) to implement a simple Calculator???

The calculator should perform 4 operations (addition, subtraction, multiplication and division). The calculator should contain a button called clear that will clear the last input of the formula (forexample, 3+4 then "button clear" deletes 4 only) and also the calculator should contain another button called C/A that will clear all the input. And also the calculator should contain a button called Off to exit.

Java codes: How to write a java application (using GUI) to implement a simple Calculator???
Hmm, sounds like a homework assignment...





The GUI is really straightforward. Use two JPanels, one to hold the buttons and one to contain the entire calculator. Use a GridLayout on the one with the buttons and a BorderLayout on the overall panel (so you can get the text box across the top.) If you really wanted to I guess you could do a GridBagLayout, but it's easier to combine the simpler ones. For the calculations, use two Stacks, one that holds doubles or floats to hold your numbers, and one that holds ints representing your operations. Define constants such as Calculator.ADD and Calculator.DIVIDE to store in the operator stack. When the user clicks a number button it will append that number to the number in the text field. When an operation button is clicked, add the number in the text field to the numerical stack. When equals is clicked, iterate through both stacks until you have a result. A better method would be to show the entire equation in the text window and wait until equals is pressed to evaluate to allow for complex equations. This would require you to code a standard-to-postscript evaluator, which is a bit above what you need to complete this project.
Reply:Get Eclipse and use Java swing. Jbuttons, Jframes, Jtextfields etc. It is too much to put in an answer.
Reply:If you want me to write this program for you then tough. I don't have a GUI editor.





The best thing for you to do is to picture on paper or in your mind the actions that are required.





First you will need to type in a number. The number will be typed in one digit at a time and appear on the "display". You will be using the calculator keypad for this so each number will have to update the "display".


You will now press an operation key, but no operation will be performed at this stage in this example. (The final program may be different to this) but you will need to store the operation.


Now you enter another number. This second number will refresh the "display" but you will need to store the previous number because you have an operation to process.


Now you press any operation key or an equals operation and the previous operation will be performed. The result will be shown on the "display".





I think you will find that this is called a use case. A very important tool in the development of OO programs.


Or of course you could always just cobble something together and hope that it works.


No comments:

Post a Comment