Loading...
Loading...
Java · Composition & Relationships
Once a result is published, nobody may change its marks. The program below tries: it changes the array it passed in, and it changes the array that getMarks() returned.
Write Result with a private name and a private int[] marks. Copy the array in the constructor and copy it again in getMarks(), so the result never changes. show() prints the total and the best mark.
input i v (set mark i of the array that was passed in), got i v (set mark i of an array returned by getMarks()) or show.show: NAME: total T, best B.Example
Input
Rohan 3 70 85 90 3 input 1 100 got 2 0 show
Output
Rohan: total 245, best 90
Neither change reaches the result: the total stays 245.
Try the problem first. Open one hint at a time.
Copy in: build a new array of the same length and copy each mark (or use Arrays.copyOf).
Copy out: getMarks() returns a new copy every time, never the field itself.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor