Loading...
Loading...
A school creates a marks card from an array of marks. Later, other code changes the original array or the array returned by the getter, but the card must not change. Only setMark may change a mark, and only to a value from 0 to 100.
Write a class MarksCard with private name and int[] marks, a constructor and getMarks() that both make defensive copies, String setMark(int subject, int mark) and String report().
outside i v (main changes its own array), copy i v (main changes the array from getMarks()), set i v, or show.set: UPDATED SUBJECT i, NO SUBJECT i or INVALID MARK v. show: name: m1 m2 … TOTAL t. outside and copy print nothing.outside and copy, 1 ≤ i ≤ nExample
Input
Asha 3 70 80 90 4 outside 1 0 copy 2 0 set 3 95 show
Output
UPDATED SUBJECT 3 Asha: 70 80 95 TOTAL 245
Only set changes the card; the two other changes hit copies.
Try the problem first. Open one hint at a time.
Arrays.copyOf(marks, marks.length) makes a new array with the same values.
Copy in the constructor (the caller keeps its array) and in the getter (the caller gets a new one).
setMark checks the subject number before it uses subject - 1 as an index.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor