0% found this document useful (0 votes)
102 views3 pages

Tutorial 2

The document provides instructions and examples for 4 programming activities: 1) Write a program to calculate and display BMI given weight and height variables. 2) Print the largest of 3 integers. 3) Convert units of currency (xu to quan, dong, hao, xu). 4) Write a program to classify a number as positive, negative, zero and small or large.

Uploaded by

Dung Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views3 pages

Tutorial 2

The document provides instructions and examples for 4 programming activities: 1) Write a program to calculate and display BMI given weight and height variables. 2) Print the largest of 3 integers. 3) Convert units of currency (xu to quan, dong, hao, xu). 4) Write a program to classify a number as positive, negative, zero and small or large.

Uploaded by

Dung Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming 1

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:

My weight: 51.0 (kg)


My height: 1.63 (m)
My BMI: 19.195302796492154 (kg/m2)

Instructions:

- Choose the suitable data types for your variables.


- Pay attention to the priority of calculation in your formula.
- Use 3 println statements for 3 lines of the program output.

Activity 2
Given three integers: a, b and c, print out the largest number.

Sample result

Among 3, 6 and 2, the largest is 6.


Activity 3

Task

1 quan = 100 dong


1 dong = 10 hao
1 hao = 10 xu
You have 483,274 xu. Convert them into quan, dong, hao and xu.

Expected result:

48 quan, 32 dong, 7 hao, 4 xu

Instructions:

Refer to the exercises in a recent lecture for solutions to a similar problem.

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.

Expected result (following is the results of multiple runs)

(0.0) The number is zero.


(0.9) A small positive number.
(1.0) A positive number.
(1000000.0) A positive number.
(1000000.1) A large positive number.
(-0.1) A small negative number.
(-1.0) A negative number.
(-1000000.0) A negative number.
(-1000000.1) A large negative number.
Extra Info

Real numbers in Java

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.

The System.out.print() statement


Do you know that you can display a text or value without moving onto the next line in Java? Try
out the System.out.print() statement, like so:
int n = 30;
System.out.print("There are ");
System.out.print(n);
System.out.println(" students in my class.")

Submission
Submit a zip file containing all Java programs to this tutorial’s submission box in the course
website on FIT Portal.

You might also like