Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A computer game usually has many different objects that can be seen and manipula

ID: 3637705 • Letter: A

Question


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.

Here’s 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
Good bye!






Explanation / Answer

import java.util.Scanner; public class TestDoor { static Door Door1; static Door Door2; static Door Door3; static Scanner input=new Scanner(System.in); 009 public static void main(String[] args) 010 { 011 Door1=new Door("Enter"); 012 Door1.setOpen(); Door1.setUnlocked(); Door2=new Door("Exit"); Door3=new Door("Treasure"); Door3.setOpen(); boolean done = false; int opt; do { chooseDoor(); opt = input.nextInt(); switch(opt) { case 1: DOOR1(); break; case 2: DOOR2(); break; case 3: DOOR3(); default: System.out.println(); } }while(!done); } public static void Opt() { System.out.println("***ur option***"); System.out.println("1.Open door"); System.out.println("2.Close door"); System.out.println("3.Lock door"); System.out.println("4.Unlock door"); System.out.println("5.Check condition of the door"); System.out.println("6.Quit"); } public static void chooseDoor() { System.out.println("Choose Your Door"); System.out.println("1. Enter"); System.out.println("2. Exit"); System.out.println("3. Treasure"); } public static void DOOR1() { int opt; boolean done = false; do { Opt(); opt=input.nextInt(); for(int i=0;i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote