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

The class below is supposed to model a door with a lock (can only be opened if i

ID: 671297 • Letter: T

Question

The class below is supposed to model a door with a lock (can only be opened if it is not locked). What is the main problem with this implementation? Explain briefly and fix it.(Java)

   public class Door

{  

// Locks the door (it can be locked whether open or not)  

public void lockDoor()  

{

boolean isLocked = true;  

}    

// Unlocks the door (it can be unlocked whether open or not)  

public void unlockDoor()  

{   

isLocked = false;

}    

// Closes the door (it can be closed whether locked or not)  

public void closeDoor()  

{   

boolean isOpen = false;

   }  

// Opens the door, only if it is not locked.

   public void openDoor()  

{    

if (!isLocked)    

{      

isOpen = true;    

}  

}  

// Determines whether the door is open or not  

public boolean isOpen()   

{    

return isOpen;   

}

}

Explanation / Answer

DoorMain class has been created. It contains main() function inwhich object of Door class is created.

by the object of Door class , isOpen() , isClose() function accessed.

class Door

{
   public boolean isLocked;
   public boolean isOpen;
   //Locks the door (it can be locked whether open or not)
   Door()
   {
       isLocked=true;
   }
   public void lockDoor()
   {
       isLocked = true;
   }
   //Unlocks the door (it can be unlocked whether open or not)
   public void unlockDoor()
   {     
       isLocked = false;
       System.out.println("Door is unlocked, it can be unlocked whether open or not");
   }  
   //Closes the door (it can be closed whether locked or not)
   public void closeDoor()
   {     
       isOpen = false;
       System.out.println("Door is closed, it can be closed whether locked or not");
   }
   //Opens the door, only if it is not locked.
   public void openDoor()
   {
       if (!isLocked)
       {
           isOpen = true;
           System.out.println("Door is Open now");
       }
   }  
   //Determines whether the door is open or not
   public boolean isOpen()   
   {
       return isOpen;   
   }
  
}
public class DoorMain {
   public static void main(String str[])
   {
       Door obj=new Door();
       obj.unlockDoor();
       obj.openDoor();
       obj.lockDoor();
       obj.openDoor();
       obj.closeDoor();
      
   }
}

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