Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. package com.example.student.simplecalc;
  2.  
  3. import android.app.Activity;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11.  
  12. import javax.microedition.khronos.egl.EGLDisplay;
  13.  
  14. public class MainActivity extends Activity {
  15.  
  16. private static final String TAG = "CalculatorActivity";
  17.  
  18. private Calculator mCalculator;
  19.  
  20. private EditText mOperandOneEditText;
  21. private EditText mOperandTwoEditText;
  22.  
  23. private TextView mResultTextView;
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29.  
  30. mCalculator = new Calculator();
  31. mResultTextView = (TextView) findViewById(R.id.operation_result_text_view);
  32. mOperandOneEditText = (EditText) findViewById(R.id.operand_one_edit_text);
  33. mOperandTwoEditText = (EditText) findViewById(R.id.operand_two_edit_text);
  34. }
  35.  
  36. public void onAdd(View view){
  37. compute(Calculator.Operator.ADD);
  38. }
  39.  
  40. public void onSub(View view){
  41. compute(Calculator.Operator.SUB);
  42. }
  43.  
  44. public void onDiv(View view){
  45. try{
  46. compute(Calculator.Operator.DIV);
  47. } catch (IllegalArgumentException iae){
  48. Log.e(TAG, "IllegalArgumentException", iae);
  49. mResultTextView.setText(getString(R.string.computationError));
  50. }
  51. }
  52.  
  53. public void onMul(View view){
  54. compute(Calculator.Operator.MUL);
  55. }
  56.  
  57. private void compute(Calculator.Operator operator){
  58. double operandOne;
  59. double operandTwo;
  60. try{
  61. operandOne = getOperand(mOperandOneEditText);
  62. operandTwo = getOperand(mOperandTwoEditText);
  63. } catch (NumberFormatException nfe){
  64. Log.e(TAG, "NumberFormatException", nfe);
  65. mResultTextView.setText(getString(R.string.computationError));
  66. return;
  67. }
  68.  
  69. String result;
  70. switch (operator){
  71. case ADD:
  72. result = String.valueOf(mCalculator.add(operandOne, operandTwo));
  73. break;
  74. case SUB:
  75. result = String.valueOf(mCalculator.sub(operandOne, operandTwo));
  76. break;
  77. case DIV:
  78. result = String.valueOf(mCalculator.div(operandOne, operandTwo));
  79. break;
  80. case MUL:
  81. result = String.valueOf(mCalculator.mul(operandOne, operandTwo));
  82. break;
  83. default:
  84. result = getString(R.string.computationError);
  85. break;
  86. }
  87. mResultTextView.setText(result);
  88. }
  89.  
  90. private static Double getOperand(EditText OperandEditText){
  91. String operandText = getOperandText(OperandEditText);
  92. return Double.valueOf(operandText);
  93. }
  94.  
  95. private static String getOperandText(EditText operandEditText){
  96. return operandEditText.getText().toString();
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement