1. This question involves a security system that controls access to a secure facility. The system tracks failed attempts and can lock out users. You will write two methods for the SecuritySystem class.
public class SecuritySystem
{
private int failedAttempts; // Tracks consecutive failed attempts
private boolean isLocked; // true if the system is locked out
/**
* Simulates a biometric scan (e.g., fingerprint or retina).
* Returns true if the scan is verified, false otherwise.
*/
public boolean checkBiometric()
{ /* implementation not shown */ }
/**
* Processes an access attempt using the provided code.
* Updates the system state and returns true if access is granted,
* false otherwise, as described in part (a).
*/
public boolean attemptAccess(String code)
{ /* to be implemented in part (a) */ }
/**
* Processes a stream of codes separated by spaces.
* Calls attemptAccess for each code and returns the total number
* of successful accesses, as described in part (b).
* Precondition: codeStream contains at least one code and ends with a space.
*/
public int processLog(String codeStream)
{ /* to be implemented in part (b) */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}Write the attemptAccess method, which processes a request to enter the facility. The method accepts a code as a parameter and proceeds according to the following rules:
Example 1
| Method Call | checkBiometric() Returns | Return Value | System State After Call |
|---|---|---|---|
| attemptAccess("1234") | false | false | failedAttempts = 1, isLocked = false |
| attemptAccess("5678") | false | false | failedAttempts = 2, isLocked = false |
| attemptAccess("9999") | true | true | failedAttempts = 0, isLocked = false |
Example 2
| Method Call | checkBiometric() Returns | Return Value | System State After Call |
|---|---|---|---|
| attemptAccess("0000") | false | false | failedAttempts = 3, isLocked = true |
| attemptAccess("1111") | (not called) | false | failedAttempts = 3, isLocked = true |
| attemptAccess("ADMIN_RESET") | (not called) | true | failedAttempts = 0, isLocked = false |
/** * Processes an access attempt using the provided code. * Updates the system state and returns true if access is granted, * false otherwise, as described in part (a). */ public boolean attemptAccess(String code)
Write the processLog method, which processes a string of codes representing a sequence of access attempts. The parameter codeStream contains multiple codes, each separated by a single space. The string is guaranteed to end with a space.
The method should extract each code from codeStream, call attemptAccess with that code, and count how many times attemptAccess returns true. Finally, the method should return the total count of successful accesses.
Example 1
| Method Call | Return Value |
|---|---|
| processLog("1111 2222 ADMIN_RESET 3333 ") | 1 |
Example 2
| Method Call | Return Value |
|---|---|
| processLog("A1 B2 C3 ") | 3 |
/** * Processes a stream of codes separated by spaces. * Calls attemptAccess for each code and returns the total number * of successful accesses, as described in part (b). * Precondition: codeStream contains at least one code and ends with a space. */ public int processLog(String codeStream)