I need help with this Java program. I got some minor error that I couldn't fix. Checker Classes You will have to implement specific checks to highlight errors found in the source files. We provided an interface Check.java that defines a single method public Optional lint(String line, int lineNumber) . All the checkers you write should implement this interface and hence, you need to implement the lint method. All of these should return an an Error when one is present with a custom message of your choosing to describe what the error means to the user. If the condition a Check is looking for is not present for a line, should return Optional.empty(). Other Classes For each file below, implement the methods and constructors defined. Error.java public Error(int code, int lineNumber, String message) Constructs an Error given the error code, line number and a message. public String toString() Returns a String representation of the error with the line number, error code and message. The representation should be formatted as (replace curly braces with the actual values) (Line: {lineNumber}) has error code {code} {message} public int getLineNumber() Returns the line number of this error public int getCode() Returns the error code of this error public String getMessage() Returns the message for this error Linter.java public Linter(List checks) Constructs a Linter given a list of checks to run through. public List lint(String fileName) throws FileNotFoundException Lints the given files line-by-line by running through all the checks passed into the constructor. You should first open the file (specified by fileName) and read through the file line-by-line. You can use the File and Scanner classes to help open and read the file line-by-line. For each line, run through all the checks and add errors if they are present. Returns a list of all errors found. All of these should return an an Error when one is present with a custom message of your choosing to describe what the error means to the user. If the condition a Check is looking for is not present for a line, should return Optional.empty().   Here's a class I need help with BreakCheck class import java.util.*; public class BreakCheck implements Check {     public Optional lint(String line, int lineNumber) {         // Match "break" outside of comments or strings         if (line.matches("^(?!\\s*//|\\s*/\\*|\\s*\\*).*\\bbreak\\b.*")) {             return Optional.of(new Error(2, lineNumber, "Line contains 'break' keyword outside of a comment"));         }         return Optional.empty();     } } Error Class: import java.util.*; public class BreakCheck implements Check {     public Optional lint(String line, int lineNumber) {         // Match "break" outside of comments or strings         if (line.matches("^(?!\\s*//|\\s*/\\*|\\s*\\*).*\\bbreak\\b.*")) {             return Optional.of(new Error(2, lineNumber, "Line contains 'break' keyword outside of a comment"));         }         return Optional.empty();     } }   TestFile: import java.util.*; public class BreakCheck implements Check {     public Optional lint(String line, int lineNumber) {         // Match "break" outside of comments or strings         if (line.matches("^(?!\\s*//|\\s*/\\*|\\s*\\*).*\\bbreak\\b.*")) {             return Optional.of(new Error(2, lineNumber, "Line contains 'break' keyword outside of a comment"));         }         return Optional.empty();     } }   Linter Class import java.util.*; public class BreakCheck implements Check {     public Optional lint(String line, int lineNumber) {         // Match "break" outside of comments or strings         if (line.matches("^(?!\\s*//|\\s*/\\*|\\s*\\*).*\\bbreak\\b.*")) {             return Optional.of(new Error(2, lineNumber, "Line contains 'break' keyword outside of a comment"));         }         return Optional.empty();     } } Check.java import java.util.*;public interface Check {     public Optional lint(String line, int lineNumber); } LinterMain : import java.util.*; // A common interface for linters that can check a single line of code // Every Check will check for its own type of error on a single line of code. public interface Check {     // Checks for this Check's error condition on this line with this line number.     // If an error exists on this line, returns an Optional with an Error present      // indicating an error occurred. If no errors are present, returns an empty Optional.     public Optional lint(String line, int lineNumber); }   I can't pass this check : BreakCheck correctly tests for break statements: Failed: Make sure that breakCheck does not only check if a line starts with a comment for the case when 'break' is in a comment. ==> expected: but was:

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I need help with this Java program. I got some minor error that I couldn't fix.

Checker Classes

You will have to implement specific checks to highlight errors found in the source files. We provided an interface Check.java that defines a single method public Optional<Error> lint(String line, int lineNumber) . All the checkers you write should implement this interface and hence, you need to implement the lint method.

All of these should return an an Error when one is present with a custom message of your choosing to describe what the error means to the user. If the condition a Check is looking for is not present for a line, should return Optional.empty().

Other Classes

For each file below, implement the methods and constructors defined.

Error.java

public Error(int code, int lineNumber, String message)

Constructs an Error given the error code, line number and a message.

public String toString()

Returns a String representation of the error with the line number, error code and message. The representation should be formatted as (replace curly braces with the actual values)

(Line: {lineNumber}) has error code {code} {message}

public int getLineNumber()

Returns the line number of this error

public int getCode()

Returns the error code of this error

public String getMessage()

Returns the message for this error

Linter.java

public Linter(List<Check> checks)

Constructs a Linter given a list of checks to run through.

public List<Error> lint(String fileName) throws FileNotFoundException

Lints the given files line-by-line by running through all the checks passed into the constructor. You should first open the file (specified by fileName) and read through the file line-by-line. You can use the File and Scanner classes to help open and read the file line-by-line. For each line, run through all the checks and add errors if they are present. Returns a list of all errors found.

All of these should return an an Error when one is present with a custom message of your choosing to describe what the error means to the user. If the condition a Check is looking for is not present for a line, should return Optional.empty().

 

Here's a class I need help with

BreakCheck class

import java.util.*;

public class BreakCheck implements Check {
    public Optional<Error> lint(String line, int lineNumber) {
        // Match "break" outside of comments or strings
        if (line.matches("^(?!\\s*//|\\s*/\\*|\\s*\\*).*\\bbreak\\b.*")) {
            return Optional.of(new Error(2, lineNumber, "Line contains 'break' keyword outside of a comment"));
        }
        return Optional.empty();
    }
}

Error Class:

import java.util.*;

public class BreakCheck implements Check {
    public Optional<Error> lint(String line, int lineNumber) {
        // Match "break" outside of comments or strings
        if (line.matches("^(?!\\s*//|\\s*/\\*|\\s*\\*).*\\bbreak\\b.*")) {
            return Optional.of(new Error(2, lineNumber, "Line contains 'break' keyword outside of a comment"));
        }
        return Optional.empty();
    }
}

 

TestFile:

import java.util.*;

public class BreakCheck implements Check {
    public Optional<Error> lint(String line, int lineNumber) {
        // Match "break" outside of comments or strings
        if (line.matches("^(?!\\s*//|\\s*/\\*|\\s*\\*).*\\bbreak\\b.*")) {
            return Optional.of(new Error(2, lineNumber, "Line contains 'break' keyword outside of a comment"));
        }
        return Optional.empty();
    }
}

 

Linter Class

import java.util.*;

public class BreakCheck implements Check {
    public Optional<Error> lint(String line, int lineNumber) {
        // Match "break" outside of comments or strings
        if (line.matches("^(?!\\s*//|\\s*/\\*|\\s*\\*).*\\bbreak\\b.*")) {
            return Optional.of(new Error(2, lineNumber, "Line contains 'break' keyword outside of a comment"));
        }
        return Optional.empty();
    }
}

Check.java

import java.util.*;public interface Check {
    public Optional<Error> lint(String line, int lineNumber);
}

LinterMain :

import java.util.*;

// A common interface for linters that can check a single line of code
// Every Check will check for its own type of error on a single line of code.
public interface Check {
    // Checks for this Check's error condition on this line with this line number.
    // If an error exists on this line, returns an Optional with an Error present 
    // indicating an error occurred. If no errors are present, returns an empty Optional.
    public Optional<Error> lint(String line, int lineNumber);
}

 

I can't pass this check :
BreakCheck correctly tests for break statements:

Failed: Make sure that breakCheck does not only check if a line starts with a comment for the case when 'break' is in a comment. ==> expected: <true> but was: <false>

As you read through the descriptions of each class below, you may find it helpful to refer back to the diagram
above to understand how these different classes work together.
The program is driven by a class Linter that lints a file given a list of checks. You will need to iterate through files
line by line and run checks on each line of the Java file. Like we mentioned in the background section, these checks
are completely customizable, and we've come up with 3 such checks: BreakCheck, LongLineCheck, and
BlankPrintlnCheck and each check should return an Error. To define these checks in Java, we've defined an
interface called Check that these checks should implement.
Checker Classes
You will have to implement specific checks to highlight errors found in the source files. We provided an interface
Check.java that defines a single method public Optional<Error> lint(String line, int lineNumber). All
the checkers you write should implement this interface and hence, you need to implement the lint
method.
For this assignment, you'll implement the following checks:
1. LongLineCheck
• Should return an error (with a custom message) if the given line length is 100 characters or greater
• Has error code 1
2. BreakCheck
• Should return an error (with custom message) if the given line contains the break keyword outside of
a single line comment (comments that start with //). i.e, we don't care about the word break inside
comments, and only in the actual java code.
▪ Your check should only look for break specifically in all-lowercase (so occurrences of "Break" or
"BReak" outside of a single line comment should not be flagged).
▪ Note that this check is overly-simplistic in that it might flag some false uses of break such as
System.out.println("break"); . You do not need to handle this case specially, you should flag
any use of the word break outside of a single line comment.
• Has error code 2
3. BlankPrintlnCheck
• Should return an error (with custom message) if the given line contains the pattern
o System.out.println("")
o Has error code 3
Transcribed Image Text:As you read through the descriptions of each class below, you may find it helpful to refer back to the diagram above to understand how these different classes work together. The program is driven by a class Linter that lints a file given a list of checks. You will need to iterate through files line by line and run checks on each line of the Java file. Like we mentioned in the background section, these checks are completely customizable, and we've come up with 3 such checks: BreakCheck, LongLineCheck, and BlankPrintlnCheck and each check should return an Error. To define these checks in Java, we've defined an interface called Check that these checks should implement. Checker Classes You will have to implement specific checks to highlight errors found in the source files. We provided an interface Check.java that defines a single method public Optional<Error> lint(String line, int lineNumber). All the checkers you write should implement this interface and hence, you need to implement the lint method. For this assignment, you'll implement the following checks: 1. LongLineCheck • Should return an error (with a custom message) if the given line length is 100 characters or greater • Has error code 1 2. BreakCheck • Should return an error (with custom message) if the given line contains the break keyword outside of a single line comment (comments that start with //). i.e, we don't care about the word break inside comments, and only in the actual java code. ▪ Your check should only look for break specifically in all-lowercase (so occurrences of "Break" or "BReak" outside of a single line comment should not be flagged). ▪ Note that this check is overly-simplistic in that it might flag some false uses of break such as System.out.println("break"); . You do not need to handle this case specially, you should flag any use of the word break outside of a single line comment. • Has error code 2 3. BlankPrintlnCheck • Should return an error (with custom message) if the given line contains the pattern o System.out.println("") o Has error code 3
Expert Solution
Step 1

There is a JAVA program given with the following problem statement:

Checker Classes

You will have to implement specific checks to highlight errors found in the source files. We provided an interface Check.java that defines a single method public Optional<Error> lint(String line, int lineNumber). All the checkers you write should implement this interface and hence, you need to implement the lint method.

All of these should return an Error when one is present with a custom message of your choosing to describe what the error means to the user. If the condition a Check is looking for is not present for a line, should return Optional.empty().

Other Classes

For each file below, implement the methods and constructors defined.

 

I have provided the solution for the error you are getting.

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education