Loading...
Loading...
A college gate log has two kinds of entries: P name for a visitor (Person) and S name roll for a student (Student extends Person). Two Persons are equal when the names match; two Students when the name and roll match. A Person and a Student are never equal, even with the same name, so use getClass() in equals(). Print each distinct entry once, at its first appearance, then the count.
P name or S name roll.Person <name> or Student <name> <roll> per distinct entry, then Distinct: <count>.Example
Input
5 P Diya S Diya 7 S Diya 7 P Diya S Diya 8
Output
Person Diya Student Diya 7 Student Diya 8 Distinct: 3
Person Diya and Student Diya 7 have the same name but different classes.
Try the problem first. Open one hint at a time.
In Person.equals(), getClass() != obj.getClass() returns false for a Person compared with a Student.
Student.equals() can start with super.equals(obj), which already checks the class and the name, then compare the roll.
Let toString() in Person use getClass().getSimpleName(), and add the roll in Student with super.toString().
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor