Loading...
Loading...
Java · References, Casting & Immutability
Each exam result is a record Result(String name, int marks). Its compact constructor throws IllegalArgumentException when the marks are outside 0 to 100. A method grade() returns A for 75 or more, B for 50 to 74 and C below 50. Print every valid result with its grade, and Invalid <name> for each rejected row.
<name> <marks> <grade>, or Invalid <name>.Example
Input
4 Aarav 82 Diya 101 Rohan 50 Kavya 49
Output
Aarav 82 A Invalid Diya Rohan 50 B Kavya 49 C
101 is above 100, so Diya's row is rejected; 50 is a B and 49 a C.
Try the problem first. Open one hint at a time.
The compact constructor is Result { if (marks < 0 || marks > 100) throw new IllegalArgumentException(...); }; Java assigns the fields after it.
Create each record inside try; the catch prints the Invalid line and the loop goes on.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor