creating calculator using java
DESCRIPTION
How to create a calculator on your own, tailored for Java. It contains only the basic idea so actually it can also be implemented in other languages as well.TRANSCRIPT
![Page 1: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/1.jpg)
Creating Simple Calculator
OBJECT ORIENTED PROGRAMMINGFINAL PROJECT
Fiona Angelina© 2011
![Page 2: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/2.jpg)
Step 1: Creating the Design
![Page 3: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/3.jpg)
Step 2: Make Sure All Buttons is Working
• Concatting Numbers
![Page 4: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/4.jpg)
Simplest Way of Concatting Number
• Treat the number as a String. Cast to Double when calculating.
• Pseudocode://Get text from textfield, put it into String s//Concat s with other number (0, 1, 2, …, 9)//Set textfield with s
![Page 5: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/5.jpg)
Simplest Way of Concatting Number
• Code might look like this:String s = text.getText();s = s.concat(“1”);text.setText(s);
![Page 6: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/6.jpg)
Step 2: Make Sure All Buttons is Working
• Clear button
![Page 7: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/7.jpg)
Clearing TextField
• To clear textfield, simply set it with a new value. In this case, it is “0”.
• Code might look like this:text.setText(“0”);
![Page 8: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/8.jpg)
Step 2: Make Sure All Buttons is Working
• Backspace button
![Page 9: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/9.jpg)
Allowing Backspace
• Use method subSequence with start index = 0 and end index = length of string – 1.
• Don’t forget to convert the resulting CharSequence to String.
• Code might look like this:String s = text.getText();CharSequence res = s.subsequence(0, s.length() – 1);
text.setText(res.toString());
![Page 10: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/10.jpg)
Step 3: Operations
![Page 11: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/11.jpg)
Strategy To Make Operation Works
• Strategy:– Create two variables:• operator: to store the last operator• operand: to store the last operand
![Page 12: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/12.jpg)
How Does It Work?
• 1: After finished concatting number, user clicks “+”.
This button is clicked
![Page 13: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/13.jpg)
How Does It Work?
• 2: Identify if it is the first number that user enters or not. Assume it is the first number, simply put the operand and operator inside the variables.
operator = “+”
operand = 98
![Page 14: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/14.jpg)
How Does It Work?
• 3: User concats another number, and press “*”.
This button is clicked
![Page 15: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/15.jpg)
How Does It Work?
• 4: Identify if it is a first operand or not. Now it is not the first operand. Therefore you have to calculate a result.
![Page 16: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/16.jpg)
How Does It Workoperator = “+”operand = 98 newNum = 6
operand = 104
operator = “*”operand = 104
CURRENT STATUS
![Page 17: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/17.jpg)
How Does It Work?
• 5: Suppose user concats another number, and this time press “=“.
This button is clicked
![Page 18: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/18.jpg)
How Does It Work?
• 6: When user enters “=“ instead of other kind of operators, you have to calculate the result, and reset all the variables.
![Page 19: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/19.jpg)
How Does It Work?
operator = “*”operand = 104 newNum = 3
operand = 312Output the result to textfield
operand = null operator = null
CURRENT STATUS
![Page 20: Creating Calculator using Java](https://reader036.vdocuments.net/reader036/viewer/2022082915/54528f55af7959013f8b920d/html5/thumbnails/20.jpg)
Step 4: Playing with Memory
• Strategy:– Create 1 null variable to represent the memory.– M+ & M- : identify if it is the first memory. If it is,
store the current value. If not, increment/decrement with the previous value.
– MC : set the memory to null.– MR : set textfield with value inside memory.