Tugas 5
Aplikasi Kalkulator Sederhana
Ahmad Fauzan Alghifari 5025211091 PPB D
Problem Statement
Pada pertemuan ke lima, ditugaskan untuk mencoba kotlin dengan latihan membuat aplikasi kalkulator sederhana, yang mengimplementasikan operasi aritmatika dasar. Yakni penjumlahan, pengurangan, perkalian, dan pembagian. Adapun referensi yang saya gunakan sebagai referensi utama dari penugasan ini dapat diakses pada link berikut
Kode Sumber : Github
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.example.simplecalculator | |
import android.os.Bundle | |
import android.widget.Toast | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.activity.enableEdgeToEdge | |
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.padding | |
import androidx.compose.foundation.layout.width | |
import androidx.compose.material3.Button | |
import androidx.compose.material3.Scaffold | |
import androidx.compose.material3.Text | |
import androidx.compose.material3.TextField | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.tooling.preview.Preview | |
import com.example.simplecalculator.ui.theme.SimpleCalculatorTheme | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// enableEdgeToEdge() | |
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") | |
} | |
Button(onClick = { | |
var result = num1.toInt() - num2.toInt() | |
Toast.makeText(applicationContext, "Result is $result", Toast.LENGTH_SHORT).show() | |
}) { | |
Text(text = "Sub") | |
} | |
Button(onClick = { | |
var result = num1.toInt() * num2.toInt() | |
Toast.makeText(applicationContext, "Result is $result", Toast.LENGTH_SHORT).show() | |
}) { | |
Text(text = "Mul") | |
} | |
Button(onClick = { | |
var result = num1.toInt() / num2.toInt() | |
Toast.makeText(applicationContext, "Result is $result", Toast.LENGTH_SHORT).show() | |
}) { | |
Text(text = "div") | |
} | |
} | |
} | |
} | |
} | |
} |
Tampilan Output:
Video presentasi :
Comments
Post a Comment