0% found this document useful (0 votes)
30 views

PR1 - W02 - Variables and Datatypes

PR1 - W02_Variables and Datatypes

Uploaded by

Thảo Phương
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

PR1 - W02 - Variables and Datatypes

PR1 - W02_Variables and Datatypes

Uploaded by

Thảo Phương
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JAVA PROGRAMMING

Tutorial 02
Activity 1: Pow Table
Write a program that displays the following table. Cast floating-point
numbers into integers.
a b pow(a, b)
1 2 1
2 3 8
3 4 81
Activity 2: Runway Length
Given an airplane’s acceleration a and take-off speed v, you can compute
the minimum runway length needed for an airplane to take off using the
following formula:
v2
length =
2𝑎
Write a program that prompts the user to enter v in meters/second (m/s)
and the acceleration a in meters/second squared (m/s2) and displays the
minimum runway length. Here is a sample run:

Activity 3: Distance of Two Points


Write a program that prompts the user to enter two points (x1, y1) and
(x2, y2) and displays their distance between them. The formula for
computing the distance is √(𝑥2 − 𝑥1 )2 + (𝑦2 − 𝑦1 )2 . Note that you can
use Math.pow(a, 0.5) to compute √2. Here is a sample run:
Activity 4: Triangle Area
Write a program that prompts the user to enter three points (x1, y1), (x2,
y2), (x3, y3) of a triangle and displays its area. The formula for
computing the area of a triangle is
s = (side1 + side2 + side3) / 2.
area = √𝑠(𝑠 − 𝑠𝑖𝑑𝑒1)(𝑠 − 𝑠𝑖𝑑𝑒2)(𝑠 − 𝑠𝑖𝑑𝑒3)
Here is a sample run

Activity 5: Counting Monetary Units


Develop a program that changes a given amount of money into smaller
monetary units. The program lets the user enter an amount as a double
value representing a total in dollars and cents, and outputs a report listing
the monetary equivalent in the maximum number of dollars, quarters,
dimes, nickels, and pennies, in this order, to result in the minimum
number of coins. Here are the steps in developing the program:
1. Prompt the user to enter the amount as a decimal number, such as
11.56.
2. Convert the amount (e.g., 11.56) into cents (1156).
3. Divide the cents by 100 to find the number of dollars. Obtain the
remaining cents using the cents remainder 100.
4. Divide the remaining cents by 25 to find the number of quarters.
Obtain the remaining cents using the remaining cents remainder
25.
5. Divide the remaining cents by 10 to find the number of dimes.
Obtain the remaining cents using the remaining cents remainder
10.
6. Divide the remaining cents by 5 to find the number of nickels.
Obtain the remaining cents using the remaining cents remainder 5.
7. The remaining cents are the pennies.
8. Display the result.
Activity 6: Financial Compound Value
Suppose you save VND 1.000.000 each month in a savings account with
the annual interest rate of 5%. Thus, the monthly interest rate is 0.05 / 12
= 0.00417. After the first month, the value in the account becomes
1000000 * (1 + 0.00417) = 1004170
After the second month, the value in the account becomes
(1000000 + 1004170) * (1 + 0.00417) = 2012527.3889
After the third month, the value in the account becomes
(1000000 + 2012527.3889) * (1 + 0.00417) = 3025089.6281
and so on.
Write a program that prompts the user to enter a monthly saving amount
and displays the account value after the sixth month.
Activity 7: Integer Digits Sum
Write a program that reads an integer between 0 and 1000 and adds all
the digits in the integer. For example, if an integer is 932, the sum of all
its digits is 14.
Hint: Use the % operator to extract digits and use the / operator to remove
the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93.
Here is a sample run:

Activity 8: Displaying the Current Time


Write a program that displays the current time in GMT (Greenwich Mean
Time) in the format hour:minute:second, such as 13:19:8.
Hint: The currentTimeMillis method in the System class returns the
current time in milliseconds elapsed since midnight, January 1, 1970
GMT, as shown in Figure 2.2. This time is known as the UNIX epoch.
The epoch is the point when time starts, and 1970 was the year when the
UNIX operating system was formally introduced.
Submission
For this tutorial, you are required to submit all your Java programs as a
single .zip file. Please follow the guidelines below to ensure your
submission is well-organized and easy to review:
1. Package Structure:
Create a separate package for each exercise. For example, if you
have exercises named "Counting Monetary Units" and
"Financial Compound Value", they are all related to financial, so
just create a package named financial.
Place all the Java packages and files related to each exercise
inside the corresponding package. Here is an example:

2. Zipping Your Work:


Once you have organized your code into packages, compress the
entire project folder into a .zip file.
The name of the zip file should follow the format:
F2024_PR1C_StudentID_StudentName_TutorialWeekNumber.zi
p (e.g., F2024_PR1C_123456789_JohnDoe_TutorialWeek01.zip).
3. Submission:
Upload the .zip file to this tutorial’s submission box on the course
website via the FIT Portal.

You might also like