This is a java lab question2.3. Please help me to get the answer ASAP. Create an
ID: 3840069 • Letter: T
Question
This is a java lab question2.3. Please help me to get the answer ASAP.
Create an implementation for the class DoorLock described below. Declare an integer constant, called MAXNUMBER_OF_ATTEMPTS, that you will initialize to the value 6 Instance variables. The class Door Lock must have the necessary instance variables to: I Store an object of the class Combination II To represent the property of being opened or closed III To represent its state (the door lock is enabled or disabled) IV To count the number of unsuccessful attempts at opening the door; The class has a single constructor, DoorLock(Combination which initializes this instance with a combination. When a door lock is first created, the door lock is closed. Also, when the object is first created, it is enabled and the number of failed attempts at opening it should be Zero Implement the instance method public boolean isOpen() that returns true if this door lock is currently opened and false otherwise Implement the instance method public boolean is Enabled() that returns true if this door lock is currently enabled and false otherwise. Implement the instance method public void enable(Combination c) that sets the instance variable enabled to true if the parameter c is equals to the combination of this object; Finally, implement the instance method public boolean open(Combination) such that I An attempt is made at opening this door lock only if this door lock is enabled II If the parameter combination is equals to the combination of this door lock, set the state of the door to be open, and the number of failed attempts should be reset to zero III otherwise, i e. if the wrong Combination was supplied, the number of failed attempts should be incremented by one IV If the number of failed attempts reaches MAX_NUMBER_OF_ATTEMPTS, this door lock should be disabled.Explanation / Answer
DoorLock.java
public class DoorLock
{
//declare an integer constant and initialize to 6
public static final int MAX_NUMBER_OF_ATTEMPTS = 6;
//instance variables
private Combination combination; //variable which is an object of Combination class
private boolean opened; //variable which represents door is opened or closed
private boolean enabled; //variable which represents the state of the door
private int numOfFailedAttempts; //variable to hold count of unsuccessful attempts
//constructor that takes object of Combination class as parameter
public DoorLock(Combination c)
{
combination = c;
opened = false; //door is closed
enabled = true; //door is enabled
numOfFailedAttempts = 0; //set number of unsuccessful attempts to 0
}
//method that returns a boolean based on whether the door is currently opened or not
public boolean isOpen()
{
return opened;
}
//method that returns a boolean based on whether the door is enabled or not
public boolean isEnabled()
{
return enabled;
}
//method enable that takes an object of Combination class as parameter
//set the instance variable isEnabled to true if the parameter c is equal to the combination of this object
public void enable(Combination c)
{
//check if combination is equal to c, if yes set isEnabled to true
if (combination.equals(c))
{
enabled = true;
}
}
//method open that takes object of Combination class as object and tries to open the door
public boolean open(Combination c)
{
//before making an attempt to open the door, first check if door is enables
if (enabled)
{
//if combination is equal to c
if (combination.equals(c))
{
//the open the door by setting opened to true
opened = true;
//reset number of failed attempt to zero
numOfFailedAttempts = 0;
}
else
{
//increment the failed attempts
numOfFailedAttempts++;
//check if number of failed attempts reached max number of attempts
if (numOfFailedAttempts >= MAX_NUMBER_OF_ATTEMPTS)
{
//disable the door by setting enabled to false
enabled = false;
numOfFailedAttempts = 0;
}
}
}
return enabled && opened;
}
}
As the implementation of the Combination class is not provided, create and empty class with the name Combination.java so as to ensure that DoorLock.java compiles successfully
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.