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.
Memento
Reference type
Report a typo
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;
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.