-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
96 lines (79 loc) · 2.95 KB
/
MainActivity.java
File metadata and controls
96 lines (79 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.example.basiccalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText et1,et2,etResult;
Button btnAdd,btnSub,btnMul,btnClear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = findViewById(R.id.et1);
et2 = findViewById(R.id.et2);
etResult = findViewById(R.id.etResult);
btnAdd = findViewById(R.id.btnAdd);
btnSub = findViewById(R.id.btnSub);
btnMul = findViewById(R.id.btnMul);
btnClear = findViewById(R.id.btnClear);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double n1,n2,add;
try {
n1 = Integer.parseInt(et1.getText().toString());
n2 = Integer.parseInt(et2.getText().toString());
add = n1+n2;
etResult.setText(Double.toString(add));
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(),"Something Went Wrong",Toast.LENGTH_LONG).show();
}
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
et1.setText("");
et2.setText("");
etResult.setText("");
}
});
btnSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double n1,n2,sub;
try {
n1 = Integer.parseInt(et1.getText().toString());
n2 = Integer.parseInt(et2.getText().toString());
sub = n1-n2;
etResult.setText(Double.toString(sub));
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(),"Something Went Wrong",Toast.LENGTH_LONG).show();
}
}
});
btnMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double n1,n2,mul;
try {
n1 = Integer.parseInt(et1.getText().toString());
n2 = Integer.parseInt(et2.getText().toString());
mul = n1*n2;
etResult.setText(Double.toString(mul));
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(),"Something Went Wrong",Toast.LENGTH_LONG).show();
}
}
});
}
}