Reference type

Report a typo

Creating snapshots of the internal state of an object is pretty straightforward, but sometimes its fields may be of reference types. That's why you should be careful not to overlook such situations. This time your task is to implement the Memento class which acts as a storage for the single field of the Originator class, as well as the corresponding methods to get and restore the state. The Caretaker logic is already implemented.

Write a program in Java 17
import java.util.*;

class Originator {
private int[][] array;

public Originator(int[][] array) {
this.array = array.clone();
}

public void setNumber(int position, int number) {
array[position / array[0].length][position % array[0].length] = number;
}

public int[][] getArray() {
return array.clone();
}

public Memento getMemento() {
// TODO implement this method
}

public void setMemento(Memento memento) {
// TODO implement this method
}

static class Memento {
// TODO implement this class
}
}

class Caretaker {
private final Originator originator;
private Originator.Memento snapshot = null;

public Caretaker(Originator originator) {
this.originator = originator;
___

Create a free account to access the full topic