Loading...
Loading...
Loading...
Java · References, Casting & Immutability
A college has h hostels. Each Hostel object creates its rooms through an inner class Room, written as hostel.new Room(number). A room's constructor adds 1 to its own hostel's private room count, and its label() reads the hostel's private name. Print each new room's label, then each hostel's count.
hostelIndex roomNumber (hostels are numbered from 1).<hostel>-<room> for each admission in order, then <hostel> <count> for each hostel in input order.Example
Input
2 Ganga Yamuna 3 1 101 2 201 1 102
Output
Ganga-101 Yamuna-201 Ganga-102 Ganga 2 Yamuna 1
Each room adds to the count of the hostel it was created from.
Try the problem first. Open one hint at a time.
Inside Room, plain name and rooms mean the fields of the hostel object the room belongs to.
Create rooms with hostels[index - 1].new Room(number); the hostel before .new is the outer object.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor