![]() |
CS 111- Introduction to Computer Science
Homework #14 |
1. Extend AtaxxState with clone():
Extend class AtaxxState.java
according to this specification.
This specification differs from the previous specification in that
AtaxxState is now cloneable with a deep clone/copy. In general, one makes an object cloneable (i.e.
able to be copied) by (1) declaring the class to implement Cloneable (e.g.
public class YourClassNameHere implements Cloneable { ... }), and (2) creating a method
like the following:
public Object clone() {
try {
YourClassNameHere copy = (YourClassNameHere) super.clone(); // shallow clone - fields are merely assigned
// make deep copies of the reference (i.e. Object) fields of this object to the copy object.
return copy;
}
catch (CloneNotSupportedException e) { // shouldn't get here if you've done part (1)
throw new InternalError(e.toString());
}
}
2. Testing AtaxxState: Test your class using the provided extended GUI. Usage: java Ataxx. Use the "u" key to "undo" moves.
3. Extend AtaxxGame: Extend class AtaxxGame.java according to this specification. Test input with an output transcript (including input) is provided. You should, of course, do additional testing. For this extension the "undo" command will undo a previous move. If there was no previous move, print "Cannot undo." Use the Stack class and AtaxxState.clone() for this implementation.
Note: There are often better, more memory-efficient means of implementing undo functionality. The purpose of this exercise is to provide experience with deep cloning/copying and the stack data structure.