-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmi_calc.scala
More file actions
46 lines (37 loc) · 1.1 KB
/
bmi_calc.scala
File metadata and controls
46 lines (37 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import scala.swing._
import scala.swing.event._
object BmiCalculator extends SimpleSwingApplication {
override def main(args: Array[String]) = super.main(args)
def calcBMI(weight: Double, height: Double): Double = {
require(weight > 0 && height > 0)
val bmi = (weight)/ (height*height)
return bmi
}
def top = new MainFrame {
preferredSize = new Dimension(700, 200)
title = "BMI Calculator"
val button = new Button {
text = "Click me to calc"
}
val label = new Label { text = " no bmi value yet"}
object weight extends TextField {columns = 7}
object height extends TextField {columns = 7}
contents = new FlowPanel {
contents += weight
contents += new Label("weight(kg)")
contents += height
contents += new Label("height(metres)")
contents += label
contents += button
border = Swing.EmptyBorder(40,20,20,20)
}
listenTo(weight, height,button)
reactions += {
case ButtonClicked(b) =>
val w = weight.text.toDouble
val h = height.text.toDouble
val nubmi = calcBMI(w,h)
label.text = "*** BMI = " + nubmi + "***"
}
}
}