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

java A Door Class A computer game usually has many different objects that can be

ID: 3840953 • Letter: J

Question

java

A Door Class

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( not for Jupitrian).

            Implement a Door class and describe below as well as a TestDoor that instantiates three Door objects labeled “Enter”,”Exit”, and “Treasure”. The “Enter” door should be left unlocked and opened. The “Exit” door should be left closed and locked. The “Treasure” door should be left open but locked.

A Door class

A Door class can:

Display an inscription

Be either open or closed, and

Be either locked or unlocked.

Here are some rules about how Doors work:

Once the writing on a Door is set, it cannot be changed

You may open a Door if and only if it is unlocked and closed

You may close a Door if and only if it is open

You may lock a Door if and only if it is unlocked, and unlock a Door if and only if it is locked. You should be able to check whether or not a Door is closed, check whether or not it is locked, and look at the writhing on the Door if there is any.

The instance variables of a Door class are:

String inscription

Boolean locked, and

Boolean closed.

The methods (all public) should be:

Door(String c);            // Constructor – initializes a Door with inscription c , 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

lock();                          // Locks a Door if it is unlocked

unlock();                      // Unlocks a Door if it is locked

Appropriate error messages should be displayed, if any conditions of the methods are violated.

Explanation / Answer

Door.java

---------------

public class Door {

   String inscription;
   Boolean locked;
   Boolean closed;
  
   public Door(String c, Boolean locked, Boolean closed) {
       this.inscription=c;
       this.locked=locked;
       this.closed=closed;
   }
  
   public Boolean isClosed() {
       return closed;
   }
  
   public Boolean isLocked() {
       return locked;
   }
  
   public void open() {
       if(isClosed()&&!isLocked()){
           this.closed=false;
           System.out.println("Door opened.");
       }else{
           System.out.println("Can't Open Door.");
       }
   }
  
   public void close() {
       if(!isClosed()){
           this.closed=true;
           System.out.println("Door closed.");
       }else{
           System.out.println("Door can't be closed if not open.");
       }
   }
  
   public void lock() {
       if(!isLocked()){
           this.locked=true;
           System.out.println("Door locked");
       }else{
           System.out.println("Can't lock");
       }
   }
  
   public void unlock() {
       if(isLocked()){
           this.locked=false;
           System.out.println("Door unlocked.");
       }else{
           System.out.println("Can't unlock as not locked");
       }
   }
}

TestDoor.java

--------------------

public class TestDoor {

   public static void main(String[] args) {
       Door enter = new Door("Enter",false,false);
       Door exit = new Door("Exit",true,true);
       Door treasure = new Door("Treasure",true,false);      
   }

}