Tutorial 2
Tutorial 2
Tutorial 2
Activity 1
Task
Write a program in which you declare 2 variables to store your weight (in kilograms) and your
height (in meters), then show your BMI value. The Body Mass Index can be calculated as
follows:
weight
BMI =
heigh t 2
FYI, normal BMI value ranges from 18.5 to 25 kg/m2.
Expected result:
Instructions:
Activity 2
Given three integers: a, b and c, print out the largest number.
Sample result
Task
Expected result:
Instructions:
Activity 4
Write a Java program that declares a floating-point number and prints “zero” if the number is
zero. Otherwise, print “positive” or “negative”. Add “small” if the absolute value of the number
is less than 1, or “large” if it exceeds 1,000,000.
By default, a real number values such as 5.0 or 60.2 are treated as double type by Java.
Therefore, the following statement will cause an "incompartible type" error:
float a = 1.65;
The way Java sees it, you are trying to assign a double value to a float variable, and a float
variable cannot hold a double value.
Question: So how do I write float values in Java??
Answer: You can mark a value as float by adding an f right after it.
float a = 1.65f;
Question: So should I use float or double?
Answer: It depends, but in most cases it doesn't hurt to use double type for real numbers.
Consider using float if there are compatibility reasons or memory requirement is crucial.
Submission
Submit a zip file containing all Java programs to this tutorial’s submission box in the course
website on FIT Portal.