In: Computer Science
I am working java project. I need to write methods for text game. I'm working on chest class.
Description : You should start your work by implementing the Chest class (a skeleton is provided for you). It needs to store whether it is locked or not, a String describing the contents, and which key it was locked with. The method stubs for this class are provided with comments, be sure to implement them the way they are described. You should not add any more methods to this class.
public class Chest {
/*
* Instance variables go here, you're responsible for
choosing
* which ones are needed and naming them
*/
/**
* This method is used by the Map class, you won't need
to call it yourself
* It should result in this chest being locked and
storing which key locked it.
*/
public void lock(Key theKey) {
Key rightKey = theKey;
}
/**
* If theKey is the same key that was used to lock this
chest, then
* the chest is unlocked. Otherwise this method does
nothing.
*/
public void unLock(Key theKey) {
}
/**
* Should return true if the chest is locked, false
otherwise
*/
public boolean isLocked() {
}
/**
* Return a string describing the contents of the
chest.
*/
public String getContents() {
}
/**
* Set the contents of the chest to this string. You
should not need to call
* this method in your program (though you have to
implement it anyway).
*/
public void setContents(String contents) {
}
}
I completed the definitions for each method that was mentioned. As asked, I have not added any new methods. Please find the code below. I have removed only the comments which you can easily add at your discretion. I have also uploaded a screenshot of the said code.
public class Chest {
Key rightKey;
boolean lock;
String contents;
public void lock(Key theKey) {
Key rightKey = theKey;
lock = true;
}
public void unLock(Key theKey) {
if(this.rightKey == theKey)
lock = false;
}
public boolean isLocked() {
return lock;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
}