Advertisement
mmayoub

Ex01, Calculator, Java file

Oct 1st, 2022 (edited)
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | Software | 0 0
  1. package com.example.mybmicalculator16092022;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.graphics.Color;
  6. import android.os.Bundle;
  7.  
  8.  
  9. import android.view.View;
  10. import android.widget.Button;
  11.  
  12. import android.widget.EditText;
  13. import android.widget.LinearLayout;
  14. import android.widget.RadioGroup;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. public class MainActivity extends AppCompatActivity {
  19.     // 1 - define variables
  20.     private EditText etNumber1;
  21.     private EditText etNumber2;
  22.     private TextView tvResult;
  23.  
  24.     private RadioGroup rgOperation;
  25.  
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         // 0 - select a layout file for the activity
  30.         setContentView(R.layout.activity_main);
  31.  
  32.         // 2 - bind each variable to a view from the layout
  33.         etNumber1 = findViewById(R.id.etNo1);
  34.         etNumber2 = findViewById(R.id.etNo2);
  35.         tvResult = findViewById(R.id.tvResult);
  36.         rgOperation = findViewById(R.id.rgOperation);
  37.     }
  38.  
  39.     public void btnClicked(View view) {
  40.         int n1, n2;
  41.         // check user input data
  42.         try {
  43.             n1 = Integer.parseInt(etNumber1.getText().toString());
  44.         } catch (Exception e) {
  45.             tvResult.setText("Error: First number is missing or not integer");
  46.             return;
  47.         }
  48.         try {
  49.             String t = etNumber2.getText().toString();
  50.             n2 = Integer.parseInt(t);
  51.         } catch (Exception e) {
  52.             tvResult.setText("Error: Second number is missing or not integer");
  53.             return;
  54.         }
  55.  
  56.         // check for selected operation
  57.         double res = 0;
  58.         String sign = "";
  59.  
  60.         int id = rgOperation.getCheckedRadioButtonId();
  61.         // id=-1 if no operation was selected
  62.  
  63.         if (id == R.id.rbAdd) {
  64.             res = n1 + n2;
  65.             sign = "+";
  66.         } else {
  67.             if (id == R.id.rbSub) {
  68.                 res = n1 - n2;
  69.                 sign = "-";
  70.             } else {
  71.                 if (id == R.id.rbMul) {
  72.                     res = n1 * n2;
  73.                     sign = "*";
  74.                 } else {
  75.                     if (id == R.id.rbDiv) {
  76.                         res = (double) n1 / n2;
  77.                         sign = "/";
  78.                         // error - res =(double) (n1 / n2);
  79.                     } else {
  80.                         tvResult.setText("You should Select an operation first");
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.  
  86.         // display result if no errors were found
  87.         if (sign != "") {
  88.             tvResult.setText(n1 + sign + n2 + " = " + res);
  89.         }
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement