عقد
عقد
LAB 4_Activity 2
Page 1 of 6
Exercise 1- Apply
Exercise 2 - Apply
Given the following Java code snippets, complete the missing code in the numbered blanks
(in red):
Page 2 of 6
int sum = 0; 4. i
int i = ___1___; 5. 2
while (i <= ___2___ ) {
sum = ___3____ +___4____ ;
i = i + ___5___ ;
}
System.out.println("Sum of even numbers: " + sum);
Page 3 of 6
Exercise 3 - Apply
Develop a Java program that uses a 'while' loop to ask the user to enter the price of 3 items
repeatedly one at a time. The program should calculate and display each price with VAT
included. The VAT rate is fixed at 5%.
Here is a sample run:
Enter price of item 1: 100
The price with vat included is: 105.0 AED
Enter price of item 2: 200
The price with vat included is: 210.0 AED
Enter price of item 3: 300
The price with vat included is: 315.0 AED
Exercise 4 – Apply
Page 4 of 6
2
3
4
5
6
4
3
2
1
Exercise 5 - Apply
Develop a Java program that uses a 'do-while' loop to repeatedly ask the user to enter an
integer. The loop should exit when the user enters the number 0.
Here is a sample run:
Enter a positive integer or 0 to quit: 1
Enter a positive integer or 0 to quit: 10
Enter a positive integer or 0 to quit: 0
Bye now
Page 5 of 6
Homework - Apply
1. Rewrite the solution code for exercise 5 using a while loop instead of a do-while loop.
2. Develop a Java program that validates a user's password. The program should prompt the
user to enter a password, and then check whether it meets the following criterion:
o The password must be at least 8 characters long.
Use a do-while loop to repeatedly prompt the user for a password until a valid password is
entered. Provide feedback on whether the password is valid or invalid after each attempt.
Here is a sample run:
Enter a password: HH12345
Invalid password
Enter a password: gPkfrqanny
Invalid password
Enter a password: HH123456
Valid password
Page 6 of 6