Basic Android Calculator


/ Published in: Java
Save to your folder(s)

This is a current side project. I make changes (mainly adding new things) about once a week (when I have time).

Notes: GUI in XML(not here), Eclipse Android plug-in, some images not here


Copy this code and paste it in your HTML
  1. package newProject.test;
  2. //Code by ehrenb
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6.  
  7. import android.app.Activity;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.Window;
  11. import android.widget.EditText;
  12. import android.widget.TextView;
  13. import android.widget.RadioButton;
  14. import android.widget.Toast;
  15. import java.util.ArrayList;
  16. import android.widget.*;
  17. //import java.util.ArrayList;
  18.  
  19. //needs error checking for number parsing**
  20.  
  21. public class myMenu extends Activity {
  22. private EditText inputNums;
  23. private TextView resultBox;
  24.  
  25.  
  26. @Override
  27. public void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.main);
  30. inputNums = (EditText) findViewById(R.id.input);
  31. resultBox = (TextView) findViewById(R.id.results);
  32. // requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
  33. }
  34.  
  35. //int[] storage = new int[10];
  36. //ArrayList<Double> storage=new ArrayList<Double>();
  37. double numToCalc=0;
  38.  
  39. // This method is called at button click because we assigned the name to the
  40. // "On Click property" of the button
  41. public void myClickHandler(View view) {
  42. switch (view.getId()) {
  43. }
  44. //initialize all the buttons
  45. Button addButton = (Button) findViewById(R.id.addButton);
  46. Button multButton = (Button) findViewById(R.id.multButton);
  47. Button subtractButton = (Button) findViewById(R.id.subtract);
  48. Button divideButton = (Button) findViewById(R.id.divideButton);
  49. Button factorsButton = (Button) findViewById(R.id.factorsButton);
  50.  
  51.  
  52. if (inputNums.getText().length() == 0) {
  53. Toast.makeText(this, "Please enter a valid number",
  54. Toast.LENGTH_LONG).show();
  55. return; //return is a MUST...force close without
  56. //parse for numbers, not just a blank input
  57.  
  58. }
  59.  
  60. //Code by ehrenb
  61. // check for non-numeric values
  62. //abstracted from:
  63. //src = http://www.ehow.com/how_7718014_error-check-fields-java.html
  64. for(int i= 0; i < inputNums.getText().length(); i++) {
  65. if(!Character.isDigit(inputNums.getText().charAt(i)))
  66. Toast.makeText(this, "Please enter a valid number",
  67. Toast.LENGTH_LONG).show();
  68.  
  69. return;
  70. }
  71.  
  72.  
  73.  
  74.  
  75. double inputValue = Double.parseDouble(inputNums.getText().toString());
  76.  
  77. if (addButton.isPressed()) {
  78.  
  79. //storage.add(inputValue); //add parsed double to arraylist
  80. //numToCalc = inputValue;
  81.  
  82. //inputNums.setText(inputValue.);
  83. //inputNums.append("+"); //add + to the textView
  84. //permNum=add(inputValue);
  85. resultBox.setText(String
  86. .valueOf(add(inputValue))); //do addition calculations
  87.  
  88. inputNums.setText("");//clear the textfield
  89. }
  90. if (multButton.isPressed()){
  91. numToCalc = inputValue;
  92. //add parsed double to arraylist
  93. //inputNums.setText(inputValue.);
  94. //inputNums.append("+"); //add + to the textView
  95. //storage.add(inputValue);
  96. //storage.add(inputValue);
  97. //permNum=resultValue;
  98. resultBox.setText(String
  99. .valueOf(multiply(inputValue))); //do addition calculations
  100.  
  101. inputNums.setText("");//clear the textfield
  102. }
  103.  
  104. if(subtractButton.isPressed() ){
  105.  
  106. resultBox.setText(String.valueOf(subtract(inputValue))); //do addition calculations
  107.  
  108. inputNums.setText("");//clear the textfield
  109.  
  110. }
  111. // Switch to the other button
  112. // if (fahrenheitButton.isChecked()) {
  113. // fahrenheitButton.setChecked(false);
  114. // celsiusButton.setChecked(true);
  115. // } else {
  116. // fahrenheitButton.setChecked(true);
  117. // celsiusButton.setChecked(false);
  118. // }
  119.  
  120. if (divideButton.isPressed()){
  121. numToCalc = inputValue;
  122.  
  123. resultBox.setText(String
  124. .valueOf(divide(inputValue))); //do division calculations
  125.  
  126. inputNums.setText("");//clear the textfield
  127. }
  128.  
  129. if (factorsButton.isPressed()){
  130. resultBox.setText(String.valueOf(findFactors(inputValue)));
  131. }
  132. }
  133.  
  134.  
  135.  
  136. // add
  137. int numOfAdds=0;
  138.  
  139. private double add(double addNum) {
  140.  
  141. //int temp = storage.size();
  142.  
  143. double total =0;
  144. // if (temp == 0) // if array is empty, do nothing
  145. // {
  146. //
  147. // }
  148. // else
  149. {
  150. double resultValue = Double.parseDouble(resultBox.getText().toString());
  151. double inputValue = Double.parseDouble(inputNums.getText().toString());
  152.  
  153. // total=numToCalc+resultValue;
  154. total = addNum+resultValue;
  155. //permNum=total;
  156. // for (int i=0; i<temp; i++) //loop adds up every item in the array
  157. // {
  158. // total = total +storage.get(i);
  159. //
  160. // }
  161. // storage.clear();//standard template for calc functions: clear the arrayList, then add
  162. // storage.add(total);
  163. }
  164. numOfAdds++;
  165. return (total);
  166. }
  167.  
  168.  
  169. int multiplicationInit=1;
  170. int numOfMultis=0;//FIX - counts amt of multiplications done in the program
  171.  
  172. private double multiply(double multiplyNum)
  173. {
  174. double inputValue = Double.parseDouble(inputNums.getText().toString());
  175. double resultValue = Double.parseDouble(resultBox.getText().toString());
  176.  
  177. double product=0;
  178. product=multiplyNum*resultValue;
  179.  
  180. if (numOfMultis==0 && resultValue==0 && numOfAdds==0 && numOfSubtract==0 && numOfDivide==0)
  181. //if it is absolutely the first calculation <<FIX!! If it is BOTH: The first Calculation & the resultBox=0 > it is ABSOLUTELY the first calculation.
  182. //previous problem: if(numOfMultis==0) only checks to see when the first multiplication is, not what the values are
  183. //still testing
  184. //seek to optimize this later--IE- no number of calculations counted
  185. {
  186.  
  187. product = multiplyNum*multiplicationInit;//workaround
  188. }
  189.  
  190.  
  191. else //assuming we have already done something to the resultValue
  192. //then we dont need to worry about the case of multiplying product*0
  193. {
  194. product = multiplyNum*resultValue;//multiply normally
  195. }
  196. numOfMultis++;
  197. return product;
  198. }
  199.  
  200. int numOfSubtract=0;
  201. int numOfDivide=0;
  202. private double subtract(double subtractNum)
  203. {
  204. double total =0;
  205. double resultValue = Double.parseDouble(resultBox.getText().toString());
  206.  
  207. total=resultValue-subtractNum;
  208. numOfSubtract++;
  209. return total;
  210. }
  211.  
  212.  
  213.  
  214. private double divide(double divideNum)
  215. {
  216. double resultValue = Double.parseDouble(resultBox.getText().toString());
  217. numOfDivide++;
  218. return resultValue/divideNum;
  219.  
  220. }
  221.  
  222. private String findFactors(double factNum)
  223. {
  224. ArrayList tempArray = new ArrayList();
  225. for(int i = 1; i <= factNum/2; i++)
  226. {
  227. if(factNum % i == 0)// if the remainder of num5 and a number is 0..it is a factor!
  228. tempArray.add(i);
  229. //System.out.print(i + " ");
  230. }
  231. return tempArray.toString();
  232. }
  233.  
  234. //MULTIPLCATION NOTES:
  235. //**Calculations are done off of the resultValue
  236. //Since the result value has to initially be 0, we must compensate
  237. //and make resultValue=1 in the case that the user multiplies first
  238. //instead of add/sub/div
  239. //If we were to keep resultValue=0, we would run into a problem where; if multiplying first(assuming resultValue hasnt been touched), our calculation would be forced to =0
  240. // because by default, resultValue must=0. IE 1*0=0
  241.  
  242. //goals:maintain universal resultValue
  243. //problems: if subtracting say 3-3=0. If we attempt to multiply after that, we will encounter an issue b/c it will force the 0 to be a 1.
  244. //Solution: compensate again in the functions that require it (force 1 to be a 0?)
  245. //above problem fixed
  246. //Code by ehrenb
  247.  
  248. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.