Advertisement
rohithdsouza

Calculator java

Mar 22nd, 2021
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. //By Rohith  D'souza
  2. package com.rohithdsouza.calculatorrohith;
  3.  
  4. import androidx.appcompat.app.AppCompatActivity;
  5.  
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11.  
  12.  
  13. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  14.  
  15.     EditText num1;
  16.     EditText num2;
  17.     Button add;
  18.     Button sub;
  19.     Button mul;
  20.     Button div;
  21.     TextView ans;
  22.  
  23.     long n1,n2;
  24.     String val="";
  25.  
  26.     public  void onClick(View v)
  27.     {
  28.  
  29.         switch(v.getId()) {
  30.             case R.id.btnAdd :
  31.                 n1 = Integer.parseInt(num1.getText().toString());
  32.                 n2= Integer.parseInt(num2.getText().toString());
  33.                 val = (n1 + " + "+ n2 + " = " + (n1+n2));
  34.                 ans.setText(val);
  35.                 break;
  36.             case R.id.btnSub :
  37.                 n1 = Integer.parseInt(num1.getText().toString());
  38.                 n2= Integer.parseInt(num2.getText().toString());
  39.                  val = (n1 + " - "+ n2 + " = " + (n1-n2));
  40.                 ans.setText(val);
  41.                 break;
  42.  
  43.             case R.id.btnMul :
  44.                 n1 = Integer.parseInt(num1.getText().toString());
  45.                 n2= Integer.parseInt(num2.getText().toString());
  46.                 val = (n1 + " * "+ n2 + " = " + (n1*n2));
  47.                 ans.setText(val);
  48.                 break;
  49.             case R.id.btnDiv :
  50.                 n1 = Integer.parseInt(num1.getText().toString());
  51.                 n2= Integer.parseInt(num2.getText().toString());
  52.                 val = (n1 + " / "+ n2 + " = " + (n1/n2));
  53.                 ans.setText(val);
  54.                 break;
  55.  
  56.  
  57.         }
  58.     }
  59.  
  60.  
  61.     @Override
  62.     protected void onCreate(Bundle savedInstanceState) {
  63.         super.onCreate(savedInstanceState);
  64.         setContentView(R.layout.activity_main);
  65.  
  66.         num1 = findViewById(R.id.editTextNumber);
  67.         num2 = findViewById(R.id.editTextNumber2);
  68.         add = findViewById(R.id.btnAdd);
  69.         sub = findViewById(R.id.btnSub);
  70.         mul = findViewById(R.id.btnMul);
  71.         div = findViewById(R.id.btnDiv);
  72.         ans = findViewById(R.id.txtAnswer);
  73.  
  74.         add.setOnClickListener(this);
  75.         sub.setOnClickListener(this);
  76.         mul.setOnClickListener(this);
  77.         div.setOnClickListener(this);
  78.  
  79.  
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement