Java Lab Tutorial 06
Java Lab Tutorial 06
Java Lab Tutorial 06
Objectives:
After the completion of this lab, students should be able to:
Use primitive data types and arithmetic operations.
Declare variables, constant naming, and identify variable scopes.
Demonstrate that integer division yields integer results.
Apply data type conversion (widening and narrowing).
Use shorthand operators (+=, -=, *=, /=, %=, ++, --).
Use the remainder operator (%) to solve computer problems.
Lab Exercises:
Problem Description:
Write a Java program (TempConverter.java) that converts temperature in Celsius
to Fahrenheit. Use the following formula: 𝑓𝑎ℎ𝑟𝑒𝑛ℎ𝑒𝑖𝑡 = (9/5) 𝑐𝑒𝑙𝑠𝑖𝑢𝑠 + 32.
Sample output:
celsius = 24
Fahrenheit equivalent is: 75.2
celsius = 30
Fahrenheit equivalent is: 86.0
Problem Description:
Write a Java program (LenConvertor.java) that converts the length in feet to inch,
centimeter, and mile. Use the following formulas:
𝑖𝑛𝑐ℎ = 𝑓𝑒𝑒𝑡 × 12.
𝑐𝑒𝑛𝑡𝑖𝑚𝑒𝑡𝑒𝑟 = 𝑓𝑒𝑒𝑡 × 30.48.
Sample output:
feet = 4000
feet = 600
Problem Description:
Write a Java program (Separate5Digits.java) that first declares an integer variable
named number and initializes it with an integer value consisting of five digits. Then
the program should separates the number into its individual digits and prints out the
digits separated from one another by three spaces each. For example, if you assign
the value 31468 to the number variable, the program should print 3 1 4 6 8.
Finally, the program adds up the individual digits of the number and displays their
sum. For example: 22
Tips: you need to declare six integer variables as follows:
int number = ?????; // must be initialized (any five digit integer)
int digit1; // first digit
int digit2; // second digit
int digit3; // third digit
int digit4; // fourth digit
int digit5; // fifth digit
Then you need to apply division and reminder operations on the number variable.
Sample output:
number = 31468
Digits in 31468 are 3 1 4 6 8
The sum is 22
number = 90317
Digits in 90317 are 9 0 3 1 7
The sum is 20
Question: What happens when the user assign a value to the number variable that
is less than five digits long? Why? What is the output when the user assign 1763?
Problem Description:
Write a Java program (SplitDoubleNumber.java) that splits a given double number
into the integer and decimal parts. For example, the number 24.4 should be split into
24 and 4.
Tips: use type casting to get the integer part.
Sample output:
Question: Why the decimal part is not 4? How can this problem be fixed?
Problem Description:
Write a Java program (CalRectange.java) that calculates and displays the area and
the circumference of a rectangle that its height and width are given in two variables
of type double. If the height and width are equal, the program should displays the
following message ‘the figure is a square’. Use the following formulas to calculate
the area and the circumference:
𝑎𝑟𝑒𝑎 = ℎ𝑒𝑖𝑔ℎ𝑡 × 𝑤𝑖𝑑𝑡ℎ
𝑐𝑖𝑟𝑐𝑢𝑚𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = 2 × (ℎ𝑒𝑖𝑔ℎ𝑡 + 𝑤𝑖𝑑𝑡ℎ)
Sample output:
height = 4.0
width = 2.0
Area is 8.0
Circumference is 12.0
height = 5.5
width = 5.5
Area is 30.25
Circumference is 22.0
The figure is a square