Loading...
Loading...
A builder app has three levels: Villa extends House and House extends Building. Each constructor prints one line. Show exactly what Java prints, in the order it runs the constructors, and count how many constructors ran in total.
Give Building a static int calls counter that every constructor in the chain increases by one.
building ADDR, house ADDR rooms or villa ADDR rooms garden.Building at ADDR, House with r rooms and Villa with a g sq ft garden as each constructor runs. Then CONSTRUCTORS RAN c.Example
Input
2 villa MG-Road 5 1200 house Park-St 3
Output
Building at MG-Road House with 5 rooms Villa with a 1200 sq ft garden Building at Park-St House with 3 rooms CONSTRUCTORS RAN 5
A villa runs three constructors, parent first; a house runs two.
Try the problem first. Open one hint at a time.
The first line of every child constructor is super(...), so the parent finishes before the child prints.
Each constructor does calls++ once; a villa therefore adds 3.
Read the counter at the end as Building.calls.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor