Question

In: Computer Science

I am working java project. I need to write methods for text game. I'm working on...

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) {
   }
  
  
  
}

Solutions

Expert Solution

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;
}
}


Related Solutions

I'm working in Java and am working on a project where I need to find an...
I'm working in Java and am working on a project where I need to find an average. The catch is that for some of the values there is no data because they will be entered at a later date. I have variables assigned so that for each entry if there is an input I'll have it say _____available = 1, otherwise the variable will equal 0. I'll use an example to make this more clear. Let's say I am trying...
I'm working on a project where I need to design a transmission line tap for a...
I'm working on a project where I need to design a transmission line tap for a station generator. The parameters include sizing three switches for the tap where at least one is a loop break switch. From where to start? What kind of calculations are needed to size the switches? What are those switches?
Here is the problem I am working on. I need to write a py program that:...
Here is the problem I am working on. I need to write a py program that: Calculates a Cartesian Product Output, A x B of 2 lists. I want to limit each list to 5 numbers. It is kind of working, but I can't help think that there is a simpler way to express this problem. Please advise... Here is my code: def CartesianProductOutput(A,B):     lst = []     for x in A:         t = []         t.append(x)         for y in B:             temp...
I am trying to write a simple game in Java named GuessNumber.java to run on the...
I am trying to write a simple game in Java named GuessNumber.java to run on the command line. This program asks user to pick a number and the computer guesses the number repetitively until the guess is correct and will allow the user to set the lower and upper bound as command line parameters from where the picks the number. If no command line parameters are specified then the program uses a default of 1 to 99.
I am working on a project where I need to close the solenoid valve (24DCV) at...
I am working on a project where I need to close the solenoid valve (24DCV) at a different rate of time (variable closer time). It needs close very fast (at least within 50ms). I am using NI DAQ: USB-6211. Which relay should I use? Will digital relay timer work? Looking forward to your suggestions.
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
IN JAVA: I am using binary and linear search methods in java. How can I Generate...
IN JAVA: I am using binary and linear search methods in java. How can I Generate a new array with 10000 elements, and initialize the elements to random values using Math.random() and how can i sort the array using Arrays.sort(). I just need examples, no exact code is necessary. The array must be of doubles.
I am working on a project for my Computer Science course. I am trying to create...
I am working on a project for my Computer Science course. I am trying to create a Battleship game where a user names two coordinates on a grid and is then told whether this results in a "Hit" or a "Miss". Once the ship has been hit a certain number of times (based on the size of the ship) the ship is sunk. I have written all the code but it now fails to execute when I try to run...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT