CS110T-Lab7-student-1446 wed
CS110T-Lab7-student-1446 wed
Lab 7: Loops I
Lab Objectives:
In this lab, the student will practice:
✔ Reading data values from the user.
✔ Compiling and executing Java applications.
✔ While loop and do-while loop.
Lab Exercise 1: Program Output
What is the output of the following codes?
a)
public class Lab6 {
public static void main(String[] args) {
int count = 1;
while (count <= 10)
{
count *= 2;
}
count = count - 10;
System.out.println(count);
}
}
b)
public class Lab6 {
public static void main(String[] args) {
int x = -5;
while (x < 0)
{
x++;
System.out.print(x + " ");
}
}
}
c)
public class Lab6 {
public static void main(String[] args) {
boolean flag = true;
int count = 0;
do {
System.out.print(count + " ");
count++;
if (count == 5)
flag = false;
} while (flag);
}
}
2
Lab Exercise 2: Code writing (1)
Problem Description: Write a Java program that asks the user to input letters one by
one and builds a word from those letters. The program should stop asking for more
letters when the user types a dot (.), and then print the completed word. Use while
loop.
Output:
Enter a letter or a dot to stop: h
Enter a letter or a dot to stop: e
Enter a letter or a dot to stop: l
Enter a letter or a dot to stop: l
Enter a letter or a dot to stop: o
Enter a letter or a dot to stop: .
The word you formed is: hello
b) Problem description: Write a Java program that prompts the user to enter their daily
expenses in SR. The program will keep asking the user to enter an expense unless they
enter -1 to stop. Use a while loop to implement this. Once the user finishes entering the
expenses, the program should:
3
Output Sample:
Enter your daily expense in SR (enter -1 to stop): 50.75
Enter your daily expense in SR (enter -1 to stop): 25.30
Enter your daily expense in SR (enter -1 to stop): 10.00
Enter your daily expense in SR (enter -1 to stop): 100.50
Enter your daily expense in SR (enter -1 to stop): -1
Summary of Expenses:
- Total Expenses: 186.55 SR
- Expenses Average: 46.64 SR
c) Problem description: Write a java program that ask the user to enter the number of
product items, then enters the price for each product, then the system will calculate the
discount and print the product price after the discount. Use while loop to do this code.
• The DiscountRate is 10% (or 0.1).
• Total price after the discount = total price * DiscountRate
• Terminate the loop when the user enters 0.
Output Sample:
Enter the number of items you want to purchase: 2
Enter the price for item 1 or 0 to exit: 30
Enter the price for item 2 or 0 to exit: 70
Enter the price for item 2 or 0 to exit: 0
The total price after discounts is: $90.0
4
LAB 7 Assignment Questions
A) Write java program that prompts the user to enter the number they wanted to calculate the
summation for (Hint: for example, ‘summation 3 equals to 3+2+1’).
• The entered number must be more than 0.
• If the summation is odd, then the system must print “The sum is odd.”. else, the system
must print “The sum is even.”.
Output Sample:
Enter the limit (greater than 0): 5
Summation: 15
The sum is odd.
B) Write java program that shows the user a list of calculation operation, after the user choose one
from the list, the system will ask the user to enter the numbers to calculate the chosen
operation.
• The while loop will have the next mathematical operations:
1. Addition.
2. Subtraction.
3. Multiplication.
4. Division.
• If the user enters (5), then the system will exit from the program.
Use the while and switch to do the program.
Output Sample:
Choose from the list:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice (1-5): 3
Enter first number: 3
Enter second number: 7
Result: 21.0