Answered step by step
Verified Expert Solution
Question
1 Approved Answer
public class DoorLock { // Constant. public static final int MAX_NUMBER_OF_ATTEMPTS = 3; // Instance variables. private Combination combination; private boolean open; private boolean activated;
public class DoorLock { // Constant. public static final int MAX_NUMBER_OF_ATTEMPTS = 3; // Instance variables. private Combination combination; private boolean open; private boolean activated; private int numberOfAttempts; // Constructor. public DoorLock( Combination combination ) { // Your code here } // Access methods. public boolean isOpen() { return open; } public boolean isActivated() { return activated; } // Notice that numberOfAttempts is compared to // MAX_NUMBER_OF_ATTEMPTS only when its value has been // incremented, Also, numberOfAttempts should be set to zero when // activated is false. Problems related to the combined action of // these two variables have caused problems for some students. public boolean open( Combination combination ) { // Put your code here, then remove the line below. return true; } public void activate( Combination c ) { // Put your code here, then remove this comment. } }
Please solve the question 4.DoorLock.java
4. Door Lock You will hand in this exercise. Download and complete the starter file here: Door Lock.java. Create an implementation for the class Door Lock described below. 1. declare an integer constant, called MAX_NUMBER_OF_ATTEMPTS, that you will initialize to the value 3; 2. 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 activation state (the door lock is activated or deactivated), and iv. to count the number of unsuccessful attempts at opening the door; a 3. the class has a single constructor, DoorLock( Combination 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 activated and the number of failed attempts at opening it should be zero; 4. implement the instance method public boolean isOpen() that returns true if this door lock is currently opened and false otherwise; 5. implement the instance method public boolean isActivated() that returns true if this door lock is currently activated and false otherwise; 6. implement the instance method public void activate(Combination c) that sets the instance variable activated to true if the parameter cequals to the combination of this object; 7. finally, implement the instance method public boolean open( Combination combination ) such that i. an attempt is made at opening this door lock only if this door lock is activated ii. if the parameter combination 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 deactivated. 3. Combination You will hand in this exercise. Download and complete the starter file here: Combination.java. Implement a class, called Combination, to store three integer values (ints). 1. declare the necessary instance variables to store the three integer values; 2. create a constructor, public Combination(int first, int second, int third), to initialize the values of this object. 3. implement the instance method public boolean equals(Combination other), such that equals returns true if other contains the same values, in the same order, as this Combination; the order is defined by the order of the parameters of the constructor, given the following: Combination cl, c2, c3; cl = new Combination (1, 2, 3); c2 = new Combination (1, 2, 3) : c3 = new Combination (3, 2, 1); then c1.equals(c2) is true but c1.equals(c3) is false; 4. finally, implement the method public String toString(), to return a String representation of this object, where the first, second and third values are concatenated and separated by." symbols. E.g. Combination cl; cl = new Combination (1, 2, 3); System.out.println(cl); displays "1:2:3. The interface of the class Combination consists therefore of its constructor, the method equals and the method toString. Ideally, the input data should be validated. In particular, all the values should be in the range 1 to 5. However, since we do not yet have the tools to handle exceptional situations, we will assume (for now) that all the input data are valid! Note: Simpler code is better and it is worthwhile spending time cleaning up code even if it was already workingStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started