Lab · Java Programming (Core to Spring Boot)
Lab: electricity bills for a housing society
The brief
The secretary of a housing society in Nagpur splits the electricity bill flat by flat. You write SocietyBill.java: a method that charges by slab, a switch for the fixed charge, a loop that reads every flat's meter units, and a safe average that does not break when there are no readings.
Why it matters
Slab tariffs are everywhere in India: electricity, water, income tax, mobile data. Billing and payroll developers write this kind of method (if/else ranges, a switch on a type, a loop over readings) all the time, and the edge cases (the boundary unit, the empty month) are where real bills go wrong.
Skills you practise
- if / else if ranges
- switch expressions
- static methods and overloading
- while loop with a sentinel
- printf
- guarding a division
You need
- JDK 21 (Eclipse Temurin from adoptium.net or Oracle JDK; JDK 17 or any newer version also works)
- A text editor or IDE (VS Code with the Extension Pack for Java, or IntelliJ IDEA Community Edition) and a terminal
What you will have at the end
SocietyBill.java with energyCharge, fixedCharge and two bill methods, a sentinel loop and a safe average, plus the output of both runs.
How it is marked (rubric, 100 points)
| Correct billsEvery check passes, including the slab boundaries, the fixed charges and the empty month. | 45 |
|---|---|
| Good use of control flowif/else if for ranges, a switch expression for the type, a while loop that never bills the sentinel. | 25 |
| MethodsSmall static methods with clear names; bill(int) reuses bill(int, String) instead of copying it. | 15 |
| Tested the edge casesYou tried 100, 101, 200 and 201 units and the empty month, and can say what each prints. | 15 |
0/4 steps done
Step 1 of 4
Charge by slab
energyCharge gives Rs 240.00 for 80 units, Rs 525.00 for 150 and Rs 1470.00 for 320.
In a slab tariff, only the units INSIDE each slab are charged at that slab's rate (these are example rates). 150 units is 100 × 3.00 plus 50 × 4.50 = 525. The common bug is charging all 150 units at the higher rate (675). Use if / else if / else from the lowest slab up, so each branch only runs when the earlier ones did not.
if (units <= 100) {
charge = units * 3.00;
} else if (units <= 200) {
charge = 100 * 3.00 + (units - 100) * 4.50;
} else {
// ...
}$ javac SocietyBill.java
echo -1 | java SocietyBillYour notes: SocietyBill.java
Write down each command you ran. It goes into your project summary.