Tugas PPB 6 Kontrol Input
TUGAS PPB 6
Membuat Aplikasi Kalkulator Sederhana
Nama: Fayyadh Hafizh
NRP : 5025201164
Kelas: PPB I
Link Github: Tugas 6
Halo temen-temen semuanya! Pada kesempatan kali ini, kita akan mencoba untuk membuat implementasi dari kalkulator sederhana menggunakan konsep state. Kode dan hasil implementasi dapat dilihat dibawah ini.
- Kode Implementasi Aplikasi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fydhfzh.simplecalculator | |
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.fillMaxSize | |
import androidx.compose.foundation.layout.width | |
import androidx.compose.material3.Button | |
import androidx.compose.material3.MaterialTheme | |
import androidx.compose.material3.Surface | |
import androidx.compose.material3.Text | |
import androidx.compose.material3.TextField | |
import androidx.compose.runtime.Composable | |
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.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
import com.fydhfzh.simplecalculator.ui.theme.SimpleCalculatorTheme | |
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 = "Sub") | |
} | |
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 = "Mul") | |
} | |
Spacer(modifier = Modifier.width(16.dp)) | |
Button(onClick = { | |
if(num2.toInt() == 0) { | |
Toast.makeText(applicationContext, String.format("Result is undefined"), Toast.LENGTH_SHORT).show() | |
}else{ | |
var result = num1.toFloat() / num2.toFloat() | |
Toast.makeText(applicationContext, String.format("Result is %.3f", result), Toast.LENGTH_SHORT).show() | |
} | |
}) { | |
Text(text = "Div") | |
} | |
} | |
} | |
} | |
} | |
} |
- Hasil
Komentar
Posting Komentar