Loading...
Loading...
Java · References, Casting & Immutability
A college office logs different entries in one Object[]: record Student(String name, int marks), record Fee(String name, long amount) and plain String notes. Using instanceof patterns, report the number of students and their average marks (rounded down), the total fees, the number of notes, and the top scorer (the first one on a tie).
STUDENT name marks, FEE name amount or NOTE word.Students <count> average <avg>, Fees <total>, Notes <count>, Top <name>. With no students, the average is 0 and the top line is Top none.Example
Input
6 STUDENT Diya 91 FEE Diya 45000 NOTE holiday STUDENT Rohan 64 FEE Arjun 52000 STUDENT Kavya 91
Output
Students 3 average 82 Fees 97000 Notes 1 Top Diya
246 / 3 = 82. Diya and Kavya tie on 91; Diya came first.
Try the problem first. Open one hint at a time.
Store a new Student(...), a new Fee(...) or the note text itself in the Object[].
One loop with if (o instanceof Student s) ... else if (o instanceof Fee f) ... else if (o instanceof String) reaches each kind's accessors without a cast line.
For the top scorer, replace the current one only when the new marks are strictly greater.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor