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

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

ID: 3692463 • Letter: A

Question

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 {
  
   //private members of class
   private String inscription;
   private boolean locked;
   private boolean closed;
  
   //Constructor that sets the inscription
   public Door(String inscription) {
      
       this.inscription=inscription;
       locked=true;
       closed=true;
   }
  
   //Returns true if the closed is true otherwise returns false otherwise print a message
   public boolean isClosed()
   {
       return closed;
   }
  
   //Returns true if the locked is true otherwise returns false otherwise print a message
   public boolean isLocked()
   {
       return locked;
   }
  
   //Set closed to false if the closed is true and locked is false otherwise print a message
   public void open()
   {
       if(closed &&locked==false)
           closed=false;
       else
           System.out.println("Close the door and lock");
   }
  
   //Set closed to true if the closed is false   otherwise print a message
   public void close()
   {
       if(closed==false)
           closed=true;
       else
           System.out.println("Open the door");
   }
  
   //Set locked to true if locked is false otherwise print a message
   public void lock()
   {
       if(!locked)
           locked=true;
       else
           System.out.println("Unlock the door");
   }
   //Set locked to false if locked is true otherwise print a message
   public void unlock()
   {
       if(locked)
           locked=false;
       else
           System.out.println("lock the door");
   }

}


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


/**Driver program that tests the three door objects*/
//TestDoor.java
public class TestDoor {  
   public static void main(String[] args) {
      
       //Creae an instance of Door class for Enter
       Door door1=new Door("Enter");
       //call open
       door1.open();
       //call unlock
       door1.unlock();
      
       System.out.println("Enter door");
       System.out.println("Locked : "+door1.isLocked());
       System.out.println("Opened : "+door1.isClosed());
      
      
       //Creae an instance of Door class for Exit
       Door door2=new Door("Exit");
       //call close
       door2.close();
       //call lock
       door2.lock();
      
       System.out.println("Exit door");
       System.out.println("Closed : "+door2.isClosed());
       System.out.println("Locked : "+door2.isLocked());
      
       //Creae an instance of Door class for Treasure
       System.out.println("Treasure door");
       Door door3=new Door("Treasure");
       //call open
       door3.open();
       //call lock
       door3.lock();
       System.out.println("Opened : "+door3.isClosed());
       System.out.println("Locked : "+door3.isLocked());
      
   }  
}


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

Sample output:

Close the door and lock
Enter door
Locked : false
Opened : true
Open the door
Unlock the door
Exit door
Closed : true
Locked : true
Treasure door
Close the door and lock
Unlock the door
Opened : true
Locked : true

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