You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.9 KiB
124 lines
3.9 KiB
3 months ago
|
<!doctype html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta charset="utf-8">
|
||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
|
<title>My Learning Website</title>
|
||
|
<link href="/styles/styles.css" rel="stylesheet" type="text/css">
|
||
|
<link href="/android/styles/styles.css" rel="stylesheet" type="text/css">
|
||
|
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
|
||
|
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
|
||
|
<!--[if lt IE 9]>
|
||
|
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
|
||
|
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
|
||
|
<![endif]-->
|
||
|
</head>
|
||
|
<body>
|
||
|
|
||
|
<div class="banner">
|
||
|
<h1 class="courselink"><a href="https://www.linkedin.com/learning/kotlin-essential-training-2018">Kotlin Essential Training</a></h1>
|
||
|
<h2 class="lecturer">LinkedIn Learning : David Gassner : March 2018</h2>
|
||
|
<h2 class="episodetitle">Code Sample</h2>
|
||
|
</div>
|
||
|
|
||
|
<article>
|
||
|
<h2 class="sectiontitle">Chapter 5 - Section 3</h2>
|
||
|
<pre class="inset">
|
||
|
import Operation.*
|
||
|
|
||
|
fun main(args: Array<String>) {
|
||
|
try {
|
||
|
val number1 = MathLib.getInput("Number 1: ")
|
||
|
val number2 = MathLib.getInput("Number 2: ")
|
||
|
|
||
|
print("Select an operation (${MathLib.OPERATIONS}): ")
|
||
|
val operation = readLine()
|
||
|
|
||
|
val result: Double? =
|
||
|
when (operation) {
|
||
|
ADD.operator -> MathLib.addValues(number1, number2)
|
||
|
SUBTRACT.operator -> MathLib.subtractValues(number1, number2)
|
||
|
MULTIPLY.operator -> MathLib.multiplyValues(number1, number2)
|
||
|
DIVIDE.operator -> MathLib.divideValues(number1, number2)
|
||
|
else -> throw Exception("Unknown operation")
|
||
|
}
|
||
|
println("The answer is $result")
|
||
|
} catch (e: NumberFormatException) {
|
||
|
println("${e.message} is not a number")
|
||
|
} catch (e: Exception) {
|
||
|
println(e.message)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
enum class Operation(val operator: String) {
|
||
|
ADD("+"), SUBTRACT("-"), MULTIPLY("*"), DIVIDE("/")
|
||
|
}
|
||
|
|
||
|
|
||
|
class MathLib {
|
||
|
companion object {
|
||
|
|
||
|
// const val ADD = "+"
|
||
|
// const val SUBTRACT = "-"
|
||
|
// const val MULTIPLY = "*"
|
||
|
// const val DIVIDE = "/"
|
||
|
const val OPERATIONS = "+ - * /"
|
||
|
|
||
|
fun addValues(number1: Double, number2: Double) = number1 + number2
|
||
|
fun subtractValues(number1: Double, number2: Double) = number1 - number2
|
||
|
fun multiplyValues(number1: Double, number2: Double) = number1 * number2
|
||
|
fun divideValues(number1: Double, number2: Double) = number1 / number2
|
||
|
|
||
|
fun getInput(prompt: String): Double {
|
||
|
print(prompt)
|
||
|
val string: String? = readLine()
|
||
|
val number = string!!.toBigDecimal()
|
||
|
return number.toDouble()
|
||
|
}
|
||
|
}
|
||
|
}</pre>
|
||
|
</article>
|
||
|
|
||
|
<div class="btngroup">
|
||
|
<button class="button" onclick="window.location.href='setting.html';">
|
||
|
Chapter 1
|
||
|
</button>
|
||
|
<button class="button" onclick="window.location.href='getstarted.html'">
|
||
|
Chapter 2
|
||
|
</button>
|
||
|
<button class="button" onclick="window.location.href='variables.html'">
|
||
|
Chapter 3
|
||
|
</button>
|
||
|
<button class="button" onclick="window.location.href='flow.html'">
|
||
|
Chapter 4
|
||
|
</button>
|
||
|
<button class="button" onclick="window.location.href='classes.html'">
|
||
|
Chapter 5
|
||
|
</button>
|
||
|
<button class="button" onclick="window.location.href='data.html'">
|
||
|
Chapter 6
|
||
|
</button>
|
||
|
<button class="button" onclick="window.location.href='inheritance.html'">
|
||
|
Chapter 7
|
||
|
</button>
|
||
|
<button class="button" onclick="window.location.href='appendixa.html'">
|
||
|
Appendix A
|
||
|
</button>
|
||
|
<button class="button" onclick="window.location.href='appendixb.html'">
|
||
|
Appendix B
|
||
|
</button>
|
||
|
<button class="button" onclick="window.location.href='code.html'">
|
||
|
Code Samples
|
||
|
</button>
|
||
|
<button class="button" onclick="window.location.href='/android/android.html'">
|
||
|
Android Page
|
||
|
</button>
|
||
|
<button class="button" onclick="window.location.href='/index.html'">
|
||
|
Home
|
||
|
</button>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|