class Calculator {
fun add(a: Double, b: Double): Double {
return a + b
}
fun subtract(a: Double, b: Double): Double {
return a - b
}
fun multiply(a: Double, b: Double): Double {
return a * b
}
fun divide(a: Double, b: Double): Double {
if (b == 0.0) {
throw IllegalArgumentException("Divider cannot be zero")
}
return a / b
}
}
fun main() {
val calculator = Calculator()
// 더하기 예시
println("10 + 5 = ${calculator.add(10.0, 5.0)}")
// 빼기 예시
println("10 - 5 = ${calculator.subtract(10.0, 5.0)}")
// 곱하기 예시
println("10 * 5 = ${calculator.multiply(10.0, 5.0)}")
// 나누기 예시
try {
println("10 / 5 = ${calculator.divide(10.0, 5.0)}")
println("10 / 0 = ${calculator.divide(10.0, 0.0)}") // 예외 발생 예시
} catch (e: IllegalArgumentException) {
println(e.message)
}
}
계산기.