Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Creating Calculator using Java

11,320
views

Published on

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.

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.

2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
11,320
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
157
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. OBJECT ORIENTED PROGRAMMING
FINAL PROJECT
Creating Simple Calculator
Fiona Angelina
• 2. Step 1: Creating the Design
• 3. Step 2: Make Sure All Buttons is Working
Concatting Numbers
• 4. 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
• 5. Simplest Way of Concatting Number
Code might look like this:
String s = text.getText();
s = s.concat(“1”);
text.setText(s);
• 6. Step 2: Make Sure All Buttons is Working
Clear button
• 7. 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”);
• 8. Step 2: Make Sure All Buttons is Working
Backspace button
• 9. 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());
• 10. Step 3: Operations
• 11. Strategy To Make Operation Works
Strategy:
Create two variables:
operator: to store the last operator
operand: to store the last operand
• 12. How Does It Work?
1: After finished concatting number, user clicks “+”.
This button is clicked
• 13. 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
• 14. How Does It Work?
3: User concats another number, and press “*”.
This button is clicked
• 15. 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.
• 16. How Does It Work
operator = “+”
operand = 98
newNum = 6
operand = 104
CURRENT STATUS
operator = “*”
operand = 104
• 17. How Does It Work?
5: Suppose user concats another number, and this time press “=“.
This button is clicked
• 18. 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.
• 19. How Does It Work?
operator = “*”
operand = 104
newNum = 3
Output the result to textfield
operand = 312
CURRENT STATUS
operand = null
operator = null
• 20. 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.