Tugas 6 PPB I
Tugas 6 - Membuat Kalkulator Sederhana
Nama: Muhammad Ghani Taufiqurrahman Atmaja
NRP : 5025201110
Kelas: PPB - I
Dalam tugas ini, saya membuat Kalkulator sederhana menggunakan Kotlin dan dibantu dengan Jetpack Compose. Berikut adalah kode program dan hasil pengerjaannya.
Source Code: Repo
Code:
package com.example.tugas6ppb
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var num1 by remember{
mutableStateOf("0")
}
var num2 by remember{
mutableStateOf("0")
}
Column {
TextField(value = num1, onValueChange = {
num1 = it;
})
TextField(value = num2, onValueChange = {
num2 = it;
})
Row{
Button(onClick ={
var result = num1.toInt() + num2.toInt()
Toast.makeText(applicationContext, "Result is $result",Toast.LENGTH_SHORT).show()
}){
Text(text="Add")
}
Spacer(modifier=Modifier.width(16.dp))
Button(onClick ={
var result = num1.toInt() - num2.toInt()
Toast.makeText(applicationContext, "Result is $result",Toast.LENGTH_SHORT).show()
}){
Text(text="Subtract")
}
Spacer(modifier=Modifier.width(16.dp))
Button(onClick ={
var result = num1.toInt() * num2.toInt()
Toast.makeText(applicationContext, "Result is $result",Toast.LENGTH_SHORT).show()
}){
Text(text="Multiply")
}
Spacer(modifier=Modifier.width(16.dp))
Button(onClick ={
var result = num1.toInt() / num2.toInt()
Toast.makeText(applicationContext, "Result is $result",Toast.LENGTH_SHORT).show()
}){
Text(text="Divide")
}
}
}
}
}
}
Comments
Post a Comment