Fiveable
๐Ÿ’ปAP Computer Science A
โ€‹

๐Ÿ’ปAP Computer Science A

FRQ 1 โ€“ Methods and Control Structures
โ€‹
Unit 1: Using Objects and Methods
โ€‹
Practice FRQ 1 of 191/19
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.
</>Java
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. */
}
a. 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:
1. If the system is already locked (isLocked is true), the method returns false immediately without checking the code or biometrics.
2. If the code is equal to "ADMIN_RESET", the system resets: failedAttempts is set to 0, isLocked is set to false, and the method returns true.
3. If the code is not "ADMIN_RESET", the method calls the helper method checkBiometric.
- If checkBiometric returns true, failedAttempts is reset to 0 and the method returns true.
- If checkBiometric returns false, failedAttempts is incremented by 1. If failedAttempts then reaches 3 or more, isLocked is set to true. The method returns false.
You must use the checkBiometric helper method. Do not implement checkBiometric.

Example 1

Assume the system starts with failedAttempts = 0 and isLocked = false.
Method CallcheckBiometric() ReturnsReturn ValueSystem State After Call
attemptAccess("1234")falsefalsefailedAttempts = 1, isLocked = false
attemptAccess("5678")falsefalsefailedAttempts = 2, isLocked = false
attemptAccess("9999")truetruefailedAttempts = 0, isLocked = false
In the third call, the biometric check passed, so the failure count was reset.

Example 2

Assume the system starts with failedAttempts = 2 and isLocked = false.
Method CallcheckBiometric() ReturnsReturn ValueSystem State After Call
attemptAccess("0000")falsefalsefailedAttempts = 3, isLocked = true
attemptAccess("1111")(not called)falsefailedAttempts = 3, isLocked = true
attemptAccess("ADMIN_RESET")(not called)truefailedAttempts = 0, isLocked = false
The first call caused the system to lock. The second call failed immediately because of the lock. The third call used the admin code to unlock the system.
Complete the attemptAccess method.
/**
 * 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)
b. 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.
You must call attemptAccess in your solution.

Example 1

Assume checkBiometric() always returns false for regular codes.
Method CallReturn Value
processLog("1111 2222 ADMIN_RESET 3333 ")1
Processing: "1111" (fails), "2222" (fails), "ADMIN_RESET" (succeeds, unlocks), "3333" (fails). Total successes: 1.

Example 2

Assume checkBiometric() always returns true.
Method CallReturn Value
processLog("A1 B2 C3 ")3
All three codes result in a successful access. Total successes: 3.
Complete the processLog method.
/**
 * 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)






Pep