Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java pls! - Details Below Here is the Hyperlinked code - Detailed Comments with explanations would be appreciated! public class DoorLock { // Constant.

In Java pls! - Details Below image text in transcribed Here is the Hyperlinked code - Detailed Comments with explanations would be appreciated! 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. } }

image text in transcribedimage text in transcribed

This is an additional implementation that is requested.

5. SecurityAgent (if there is time) Implement the class SecurityAgent described below. 1. instance variables. A security agent is responsible for a particular door lock. Declare the necessary instance variables such that a Security Agent i. remembers (stores) a Combination and ii. has access to this particular Door Lock, i.e. maintains a reference to a Door Lock object; 2. implement a constructor with no parameter such that when a new Security Agent is created i. it creates a new Combination and stores it, ii. it creates a new Door Lock with this saved Combination. For the sake of simplicity, you may decide to always use the same combination: Combination secret; secret = new Combination(1,2,3); if secret is the name of the instance variable that is used to remember the Combination. Or, you can let your SecurityAgents use their imagination, so that each Security Agent has a new Combination that it only knows. int first, second, third; first = (int) (Math.random()*5) + 1; second = (int) (Math.random()*5) + 1; third = (int) (Math.random()*5) + 1; secret = new Combination(first, second, third); Alternative way to generate a random combination: java.util. Random generator; generator = new java.util. Random(); int first, second, third; first = generator.nextInt(5) + 1; second = generator.nextInt(5) + 1; third = generator.nextInt(5) + 1; secret = new Combination (first, second, third); The valid values are within the interval 1 to 5. 3. implement the instance method public Door Lock getDoor Lock() that returns a reference to the saved Door Lock; 4. implement the instance method public void activateDoor Lock() that simply reactivates the particular Door Lock that this SecurityAgent is responsible for, with the saved secret Combination. Hint: my implementation is approximately 15 lines long, not counting the blank lines. Main program The Security Agency class is provided with this laboratory to test the functionality of the three classes described above. I suggest that you only use it when all your classes have been successfully implemented and tested. The SecurityAgency class consists of a main method that i. Takes one command line argument representing the maximum number of attempts allowed to unlock the door. ii. Creates a SecurityAgent called bob, iii. it asks bob for an access to the Door Lock that bob is in charge of, iv. then it applies a "brute force" approach to unlock the door. v. After three failures, it has to ask bob to re-activate the lock. vi. When the lock has been unlocked, it prints the combination of the lock, as well as the number of attempts that were necessary to open the door lock. Pay attention at providing the required command line argument. Here are examples of successful runs: % java Security Agency 300 Success! Number of attemtps: 266 The combination is: 3:1:3 % java Security Agency 10 Sud ss! Number of attemtps: 1 The combination is: 4:1:5 % java Security Agency 120 Success! Number of attemtps: 115 The combination is: 2:2:1 % java Security Agency 400 Success! Number of attemtps: 383 The combination is: 2:4:5 % java Security Agency 90 Success! Number of attemtps: 89 The combination is: 3:5:1 % java SecurityAgency 8 Failed! Reached the maximum number of attempts before finding the combination! 5. SecurityAgent (if there is time) Implement the class SecurityAgent described below. 1. instance variables. A security agent is responsible for a particular door lock. Declare the necessary instance variables such that a Security Agent i. remembers (stores) a Combination and ii. has access to this particular Door Lock, i.e. maintains a reference to a Door Lock object; 2. implement a constructor with no parameter such that when a new Security Agent is created i. it creates a new Combination and stores it, ii. it creates a new Door Lock with this saved Combination. For the sake of simplicity, you may decide to always use the same combination: Combination secret; secret = new Combination(1,2,3); if secret is the name of the instance variable that is used to remember the Combination. Or, you can let your SecurityAgents use their imagination, so that each Security Agent has a new Combination that it only knows. int first, second, third; first = (int) (Math.random()*5) + 1; second = (int) (Math.random()*5) + 1; third = (int) (Math.random()*5) + 1; secret = new Combination(first, second, third); Alternative way to generate a random combination: java.util. Random generator; generator = new java.util. Random(); int first, second, third; first = generator.nextInt(5) + 1; second = generator.nextInt(5) + 1; third = generator.nextInt(5) + 1; secret = new Combination (first, second, third); The valid values are within the interval 1 to 5. 3. implement the instance method public Door Lock getDoor Lock() that returns a reference to the saved Door Lock; 4. implement the instance method public void activateDoor Lock() that simply reactivates the particular Door Lock that this SecurityAgent is responsible for, with the saved secret Combination. Hint: my implementation is approximately 15 lines long, not counting the blank lines. Main program The Security Agency class is provided with this laboratory to test the functionality of the three classes described above. I suggest that you only use it when all your classes have been successfully implemented and tested. The SecurityAgency class consists of a main method that i. Takes one command line argument representing the maximum number of attempts allowed to unlock the door. ii. Creates a SecurityAgent called bob, iii. it asks bob for an access to the Door Lock that bob is in charge of, iv. then it applies a "brute force" approach to unlock the door. v. After three failures, it has to ask bob to re-activate the lock. vi. When the lock has been unlocked, it prints the combination of the lock, as well as the number of attempts that were necessary to open the door lock. Pay attention at providing the required command line argument. Here are examples of successful runs: % java Security Agency 300 Success! Number of attemtps: 266 The combination is: 3:1:3 % java Security Agency 10 Sud ss! Number of attemtps: 1 The combination is: 4:1:5 % java Security Agency 120 Success! Number of attemtps: 115 The combination is: 2:2:1 % java Security Agency 400 Success! Number of attemtps: 383 The combination is: 2:4:5 % java Security Agency 90 Success! Number of attemtps: 89 The combination is: 3:5:1 % java SecurityAgency 8 Failed! Reached the maximum number of attempts before finding the combination

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Privacy In Statistical Databases International Conference Psd 2022 Paris France September 21 23 2022 Proceedings Lncs 13463

Authors: Josep Domingo-Ferrer ,Maryline Laurent

1st Edition

3031139445, 978-3031139444

More Books

Students also viewed these Databases questions

Question

What is meant by 'Wealth Maximization ' ?

Answered: 1 week ago

Question

c. What were you expected to do when you grew up?

Answered: 1 week ago