-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalci.java
More file actions
80 lines (76 loc) · 2.88 KB
/
Copy pathcalci.java
File metadata and controls
80 lines (76 loc) · 2.88 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
package com.example.calci;
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;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText inp1,inp2;
Button add,sub,mul,div;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inp1 = findViewById(R.id.inp1);
inp2 = findViewById(R.id.inp2);
add = findViewById(R.id.add);
sub = findViewById(R.id.sub);
mul = findViewById(R.id.mul);
div = findViewById(R.id.div);
result = findViewById(R.id.result);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String val1 = inp1.getText().toString();
float value1 = Float.parseFloat(val1);
String val2 = inp2.getText().toString();
float value2 = Float.parseFloat(val2);
float res = value1 + value2;
result.setText(""+res);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String val1 = inp1.getText().toString();
float value1 = Float.parseFloat(val1);
String val2 = inp2.getText().toString();
float value2 = Float.parseFloat(val2);
float res = value1 - value2;
result.setText(""+res);
}
});
mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String val1 = inp1.getText().toString();
float value1 = Float.parseFloat(val1);
String val2 = inp2.getText().toString();
float value2 = Float.parseFloat(val2);
float res = value1 * value2;
result.setText(""+res);
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String val1 = inp1.getText().toString();
float value1 = Float.parseFloat(val1);
String val2 = inp2.getText().toString();
float value2 = Float.parseFloat(val2);
if(value2 == 0)
{
result.setText("Divide by zero ERROR!");
Toast.makeText(MainActivity.this, "Error!!", Toast.LENGTH_SHORT).show();
}
else {
float res = value1 / value2;
result.setText(""+res);
}
}
});
}
}