-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathP4.kt
65 lines (60 loc) · 2.43 KB
/
P4.kt
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
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
class Calculator : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_calculator)
val num1 = findViewById<EditText>(R.id.num1)
val num2 = findViewById<EditText>(R.id.num2)
val add = findViewById<Button>(R.id.add)
val sub = findViewById<Button>(R.id.sub)
val mul = findViewById<Button>(R.id.mul)
val div = findViewById<Button>(R.id.div)
val ress = findViewById<TextView>(R.id.ress)
add.setOnClickListener() {
val t1 = num1.text.toString()
val t2 = num2.text.toString()
if (t1.isEmpty() || t2.isEmpty()) {
Toast.makeText(this, "Enter both values !!", Toast.LENGTH_SHORT).show()
} else {
val result = t1.toFloat() + t2.toFloat()
ress.setText("Result :\n ${t1} + ${t2} = ${result}")
}
}
sub.setOnClickListener() {
val t1 = num1.text.toString()
val t2 = num2.text.toString()
if (t1.isEmpty() || t2.isEmpty()) {
Toast.makeText(this, "Enter Both Values !!", Toast.LENGTH_SHORT).show()
} else {
val result = t1.toFloat() - t2.toFloat()
ress.setText("Result :\n ${t1} - ${t2} = ${result}")
}
}
mul.setOnClickListener() {
val t1 = num1.text.toString()
val t2 = num2.text.toString()
if (t1.isEmpty() || t2.isEmpty()) {
Toast.makeText(this, "Enter both values !!", Toast.LENGTH_SHORT).show()
} else {
val result = t1.toFloat() * t2.toFloat()
ress.setText("Result :\n ${t1} * ${t2} = ${result}")
}
}
div.setOnClickListener() {
val t1 = num1.text.toString()
val t2 = num2.text.toString()
if (t1.isEmpty() || t2.isEmpty()) {
Toast.makeText(this, "Enter both values !!", Toast.LENGTH_SHORT).show()
} else {
val result = t1.toFloat() / t2.toFloat()
ress.setText("Result :\n ${t1} / ${t2} = ${result}")
}
}
}
}