Question
Please DO NOT copy and paste from other answers, the output of this should be similar or same to the output of this answer. THANKS.
Please DO NOT copy and paste from other answers, the output of this should be similar or same to the output of this answer. THANKS. A computer game usually has many different objects that can be seen and manipulated. One typical object is a door. Whether a player runs through a castle, attacks the forces of an evil empire, or places furniture in a room a door often comes into play. Each door has an inscription (such as Enter, Exit, Treasure, etc.) and can be in the Open or Closed state, and Locked or Unlocked state.
The objective of this question is to implement a Door class that has the following:
String inscription
boolean locked
boolean closed
and the following methods:
Constructor that instantiates a Door with a given inscription, closed and locked.
isClosed returns true if the Door is closed
isLocked returns true if the Door is locked.
open opens a Door if it is closed and unlocked.
close closes a Door if it is open (but does not lock it).
lock locks a Door if it is unlocked.
unlock unlocks a Door if it is locked (but does not open it).
toString displays the inscription and whether the Door that is locked/unlocked, closed/open.
Add any other methods as necessary. Display appropriate error messages if any conditions of the methods are violated.
Next write a TestDoor class (with a main method) as follows:
In the TestDoor class, use another boolean variable called key. If the key is true, then a Door can be unlocked. If the key is false, the Door cannot be unlocked. When a door object is instantiated, it is initially in the locked and closed state.
Using random number generation, generate a Door object (Enter, Exit or Treasure you can use 0, 1, and 2, respectively) and a key value (true/false you can use 0 or 1). Change the status of the Door according to the following table. Repeat by placing the code in a loop. Each time the loop is entered, a new Door object and a new key value are randomly generated.
Key | Door | Change status to |
True | Enter | Unlock and open |
True | Exit | Unlock and open |
True | Treasure | Unlock and open |
False | Enter | No change |
False | Exit | No change |
False | Treasure | No change |
Your program should keep prompting the user (Play ? (Y or N)) and keep looping until the user enters N.
Note: A player cannot Exit without having gone through the Enter door. Similarly, a player cannot get to the Treasure door without having gone through the Enter door. A player who has gone through Exit must again go through the Enter door. If these conditions are not met, then the output must display a message accordingly.
Heres a sample run of the program (assuming some arbitrarily generated door and key values):
Welcome to the Dungeon.
Play? (Enter Y or N): Y
Door: Exit
Key: True
Sorry. Cannot exit without entering
Play? (Enter Y or N): Y
Door: Treasure
Key: False
Sorry. Cannot get to Treasure without entering
Play? (Enter Y or N): Y
Door: Enter
Key: False
Sorry. Cannot unlock.
Play? (Enter Y or N): Y
Door: Enter
Key: True
ENTER
Play? (Enter Y or N): Y
Door: Treasure
Key: True
GET THE TREASURE
Play? (Enter Y or N): Y
Door: Treasure
Key: False
Sorry. Cannot unlock.
Play? (Enter Y or N): Y
Door: Exit
Key: False
Sorry. Cannot unlock.
Play? (Enter Y or N): Y
Door: Exit
Key: True
EXITED.
Play? (Enter Y or N): N
Goodbye!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started