Answered step by step
Verified Expert Solution
Question
1 Approved Answer
COMPLETE THIS CODE WITH THE FOLLOWING INSTRUCTIONS: --------------------------------------------------------------------------------------- public class DoorLock { // Constant. public static final int MAX_NUMBER_OF_ATTEMPTS = 3; // Instance variables. private
COMPLETE THIS CODE WITH THE FOLLOWING INSTRUCTIONS: --------------------------------------------------------------------------------------- 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. } }
--------------------------------------------------------------------------------------------------------------------------------------------------------------
HERE IS COMBINATION CLASS THAT IT IS SUPPOSED TO IMPLEMENT:
public class Combination { int c1,c2,c3; public Combination(int c1,int c2, int c3) { this.c1 = c1; this.c2 = c2; this.c3 = c3; } public boolean equals (Combination others) { if (this.c1 == others.c1 && this.c2 == others.c2 && this.c3 == others.c3) return true; else return false; } public String toString() { return c1 + ":" + c2 + ":" + c3; } public static void main(String[] args) { Combination c1 = new Combination(1, 2, 3); Combination c2 = new Combination(1, 2, 3); Combination c3 = new Combination(3, 2, 1); //calling toString function implicitly to print it as a string System.out.println("c1 : " + c1); System.out.println("c2 : " + c2); System.out.println("c3 : " + c3); //calling equals function to check if two combinations are equal or not. System.out.println("c1 equals c2 : " + c1.equals(c2)); System.out.println("c2 equals c3 : " + c2.equals(c3)); } }
THANK YOU!!
Create an implementation for the class DoorLock 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 DoorLock 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; 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 c "equals" 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
Step 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