Creating Calculator using Java

14,336 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.

  • Be the first to comment

Creating Calculator using Java

  1. 1. OBJECT ORIENTED PROGRAMMING<br />FINAL PROJECT<br />Creating Simple Calculator<br />Fiona Angelina<br />© 2011<br />
  2. 2. Step 1: Creating the Design<br />
  3. 3. Step 2: Make Sure All Buttons is Working<br />Concatting Numbers<br />
  4. 4. Simplest Way of Concatting Number<br />Treat the number as a String. Cast to Double when calculating.<br />Pseudocode:<br />//Get text from textfield, put it into String s<br />//Concat s with other number (0, 1, 2, …, 9)<br />//Set textfield with s<br />
  5. 5. Simplest Way of Concatting Number<br />Code might look like this:<br />String s = text.getText();<br />s = s.concat(“1”);<br />text.setText(s);<br />
  6. 6. Step 2: Make Sure All Buttons is Working<br />Clear button<br />
  7. 7. Clearing TextField<br />To clear textfield, simply set it with a new value. In this case, it is “0”.<br />Code might look like this:<br />text.setText(“0”);<br />
  8. 8. Step 2: Make Sure All Buttons is Working<br />Backspace button<br />
  9. 9. Allowing Backspace<br />Use method subSequence with start index = 0 and end index = length of string – 1.<br />Don’t forget to convert the resulting CharSequence to String.<br />Code might look like this:<br />String s = text.getText();<br />CharSequence res = s.subsequence(0, s.length() – 1);<br />text.setText(res.toString());<br />
  10. 10. Step 3: Operations<br />
  11. 11. Strategy To Make Operation Works<br />Strategy:<br />Create two variables:<br />operator: to store the last operator<br />operand: to store the last operand<br />
  12. 12. How Does It Work?<br />1: After finished concatting number, user clicks “+”.<br />This button is clicked<br />
  13. 13. How Does It Work?<br />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.<br />operator = “+”<br />operand = 98<br />
  14. 14. How Does It Work?<br />3: User concats another number, and press “*”.<br />This button is clicked<br />
  15. 15. How Does It Work?<br />4: Identify if it is a first operand or not. Now it is not the first operand. Therefore you have to calculate a result.<br />
  16. 16. How Does It Work<br />operator = “+”<br />operand = 98<br />newNum = 6<br />operand = 104<br />CURRENT STATUS<br />operator = “*”<br />operand = 104<br />
  17. 17. How Does It Work?<br />5: Suppose user concats another number, and this time press “=“.<br />This button is clicked<br />
  18. 18. How Does It Work?<br />6: When user enters “=“ instead of other kind of operators, you have to calculate the result, and reset all the variables.<br />
  19. 19. How Does It Work?<br />operator = “*”<br />operand = 104<br />newNum = 3<br />Output the result to textfield<br />operand = 312<br />CURRENT STATUS<br />operand = null<br />operator = null<br />
  20. 20. Step 4: Playing with Memory<br />Strategy:<br />Create 1 null variable to represent the memory.<br />M+ & M- : identify if it is the first memory. If it is, store the current value. If not, increment/decrement with the previous value.<br />MC : set the memory to null.<br />MR : set textfield with value inside memory.<br />

×