1. This question involves a system that tracks the status of a server over time. The SystemLogger class is used to record status messages and analyze system performance.
public class SystemLogger
{
private String fullLog; // To be initialized in the constructor
/**
* Initializes the SystemLogger with an empty log.
*/
public SystemLogger()
{
fullLog = "";
}
/**
* Returns the system status at the given time.
* Precondition: time >= 0
* Postcondition: Returns a non-null string such as "OK", "WARNING", or "ERROR".
*/
public String getStatus(int time)
{ /* implementation not shown */ }
/**
* Appends the status at the given time to the fullLog and returns the status,
* as described in part (a).
* Precondition: time >= 0
*/
public String logStatus(int time)
{ /* to be implemented in part (a) */ }
/**
* Simulates the system for the given duration and counts how many times
* the targetStatus occurred, as described in part (b).
* Precondition: duration > 0
* targetStatus is not null
*/
public int analyzePeriod(int duration, String targetStatus)
{ /* to be implemented in part (b) */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}Write the logStatus method, which records the system's status at a specific time. The method must call the helper method getStatus with the parameter time to obtain the current status string.
The method should then update the instance variable fullLog by appending the time, followed by a colon and a space (": "), followed by the status string, and finally a newline character ("
"). The method should return the status string obtained from the helper method.
Example 1
| Method Call | Return Value |
|---|---|
| logStatus(5) | "OK" |
Example 2
| Method Call | Return Value |
|---|---|
| logStatus(10) | "ERROR" |
/** * Appends the status at the given time to the fullLog and returns the status, * as described in part (a). * Precondition: time >= 0 */ public String logStatus(int time)
Write the analyzePeriod method, which simulates the system running for a specific duration and counts the occurrences of a specific status. The method should iterate through time values from 0 up to (but not including) duration.
For each time value, the method must call logStatus to update the log and retrieve the status. The method should return the total number of times the status returned by logStatus is equal to the targetStatus.
Example 1
| Time | logStatus(time) Return Value |
|---|---|
| 0 | "OK" |
| 1 | "ERROR" |
| 2 | "WARNING" |
| 3 | "ERROR" |
/** * Simulates the system for the given duration and counts how many times * the targetStatus occurred, as described in part (b). * Precondition: duration > 0 * targetStatus is not null */ public int analyzePeriod(int duration, String targetStatus)
00:00
public class SystemLogger
{
private String fullLog; // To be initialized in the constructor
/**
* Initializes the SystemLogger with an empty log.
*/
public SystemLogger()
{
fullLog = "";
}
/**
* Returns the system status at the given time.
* Precondition: time >= 0
* Postcondition: Returns a non-null string such as "OK", "WARNING", or "ERROR".
*/
public String getStatus(int time)
{ /* implementation not shown */ }
/**
* Appends the status at the given time to the fullLog and returns the status,
* as described in part (a).
* Precondition: time >= 0
*/
public String logStatus(int time)
{ /* to be implemented in part (a) */ }
/**
* Simulates the system for the given duration and counts how many times
* the targetStatus occurred, as described in part (b).
* Precondition: duration > 0
* targetStatus is not null
*/
public int analyzePeriod(int duration, String targetStatus)
{ /* to be implemented in part (b) */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}