Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.calcapp2;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- public EditText p1, p2;
- public Button b_add, b_minus, b_mul, b_del;
- public TextView textView;
- int a, b;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- p1 = findViewById(R.id.arg1);
- p2 = findViewById(R.id.arg2);
- b_add = findViewById(R.id.add);
- b_minus = findViewById(R.id.subtr);
- b_mul = findViewById(R.id.mul);
- b_del = findViewById(R.id.divide);
- textView = findViewById(R.id.answer);
- }
- public boolean init() {
- boolean ans = true;
- try {
- a = Integer.parseInt(p1.getText().toString());
- } catch (NumberFormatException e) {
- ans = false;
- }
- try {
- b = Integer.parseInt(p2.getText().toString());
- } catch (NumberFormatException r) {
- ans = false;
- }
- if (!ans) {
- textView.setText("Input Error");
- }
- return ans;
- }
- public void onClickAdd(View view) {
- boolean f = init();
- if (f) {
- textView.setText(Integer.toString((a + b)));
- }
- }
- public void onClickMinus(View view) {
- boolean f = init();
- if (f) {
- textView.setText(Integer.toString((a - b)));
- }
- }
- public void onClickMul(View view) {
- boolean f = init();
- if (f) {
- textView.setText(Integer.toString((a * b)));
- }
- }
- public void onClickDel(View view) {
- boolean f = init();
- if (f) {
- if (b != 0) {
- textView.setText(Integer.toString((a / b)));
- } else {
- textView.setText("Div by zero");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement