1. This question involves the LogManager class, which is used to process and format log entries from a server. The LogManager class contains a helper method, getNextEntry. You will write two methods in the LogManager class.
public class LogManager
{
private String filterKeyword;
/**
* Initializes the LogManager with a filter keyword.
*/
public LogManager(String keyword)
{
filterKeyword = keyword;
}
/**
* Returns the next log entry to be processed, or null if no entries remain.
* Postcondition: Returns a valid string or null.
*/
public String getNextEntry()
{ /* implementation not shown */ }
/**
* Processes a single log entry based on the instance variable filterKeyword,
* as described in part (a)
* Precondition: entry is not null.
*/
public String processEntry(String entry)
{ /* to be implemented in part (a) */ }
/**
* Generates a report containing processed versions of the next numEntries log entries,
* as described in part (b)
* Precondition: numEntries > 0
*/
public String generateReport(int numEntries)
{ /* to be implemented in part (b) */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}Write the processEntry method, which formats a log entry based on the instance variable filterKeyword. If the entry contains the filterKeyword, the method should return the substring of the entry that appears after the first occurrence of the keyword. If the resulting substring is empty (meaning the keyword is at the end of the entry), the method should return "EMPTY". If the entry does not contain the filterKeyword, the method should return the original entry followed by " - NO MATCH".
Example 1
| Method Call | Return Value |
|---|---|
| processEntry("System ERROR: Disk Full") | ": Disk Full" |
| processEntry("Critical ERROR") | "EMPTY" |
| processEntry("User login successful") | "User login successful - NO MATCH" |
Example 2
| Method Call | Return Value |
|---|---|
| processEntry("Memory WARN low") | " low" |
| processEntry("All systems nominal") | "All systems nominal - NO MATCH" |
/** * Processes a single log entry based on the instance variable filterKeyword, * as described in part (a) * Precondition: entry is not null. */ public String processEntry(String entry)
Write the generateReport method, which creates a single string containing the processed versions of the next numEntries log entries. The method should obtain each entry by calling the helper method getNextEntry. For each entry obtained, the method should call processEntry to format it and then append the result to the report followed by a newline character "
". If getNextEntry returns null before numEntries is reached, the method should stop processing and return the report generated so far.
Example
| Method Call | Return Value |
|---|---|
| generateReport(3) | " missing Update complete - NO MATCH EMPTY\n" |
/** * Generates a report containing processed versions of the next numEntries log entries, * as described in part (b) * Precondition: numEntries > 0 */ public String generateReport(int numEntries)
00:00