0/30 Days Completed 0 Learning Days
Day 7 of 30 · Week 1 — Java Foundations
Week 1 practice: Bill Calculator
Practical review day1 Learn
Review day! This week you learned program structure, variables, operators, decisions, switch and loops.
Today you will combine them in one small, practical program: a shop bill with a discount.
2 See the Code
Java
public class Bill {
public static void main(String[] args) {
double[] prices = {120.0, 45.5, 300.0};
double total = 0;
for (int i = 0; i < prices.length; i++) {
total += prices[i];
}
double discount = 0;
if (total >= 400) {
discount = total * 0.10;
}
System.out.println("Total: " + total);
System.out.println("Discount: " + discount);
System.out.println("Pay: " + (total - discount));
}
}- A loop adds up all prices into total (465.5).
- An if gives a 10% discount when total is 400 or more.
- The final amount is printed using arithmetic.
Output
Total: 465.5 Discount: 46.55 Pay: 418.95
3 Practice
- Challenge 1: Change the rule so bills of 1000 or more get 20% off, and 400–999 get 10%.
- Challenge 2: Use a loop to count how many items cost more than 100 and print the count.
- Challenge 3: Use a switch on a paymentMode number (1 = Cash, 2 = UPI, 3 = Card) to print the payment method.
Try it in your own Java setup or write it in the Practice Notebook.
4 Quick Check
1. Which part of the program repeats for each price?
2. What is 465.5 * 0.10?
3. Which operator adds to total and stores it back?
Step 5
Answer and check the Quick Check above to unlock this.