I need help with my BreakCheck java method that can be used in Linter program: Here's my code: BreakCheck class: import java.util.*; public class BreakCheck implements Check {     public Optional lint(String line, int lineNumber) {         // Match "break" outside and inside of comments or strings         if (line.matches(".*\\bbreak\\b.*") && !line.matches("(?s).*\\s*//.*\\bbreak\\b.*")) {             return Optional.of(new Error(2, lineNumber, "Line contains 'break' keyword outside of a comment"));         }         return Optional.empty();     } } My LinterMain Class: import java.util.*; import java.io.*; public class LinterMain {     public static final String FILE_NAME = "TestFile.java";     public static void main(String[] args) throws FileNotFoundException{         List checks = new ArrayList<>();         checks.add(new LongLineCheck());         checks.add(new BlankPrintlnCheck());         checks.add(new BreakCheck());         Linter linter = new Linter(checks);         List errors = linter.lint(FILE_NAME);         for (Error e : errors) {             System.out.println(e);         }     } }   My Error Class: import java.util.*; public class Error {     private final int code;     private final int lineNumber;     private final String message;     public Error(int code, int lineNumber, String message) {         this.code = code;         this.lineNumber = lineNumber;         this.message = message;     }     public int getLineNumber() {         return lineNumber;     }     public int getCode() {         return code;     }     public String getMessage() {         return message;     }     public String toString() {         return String.format("(Line: %d) has error code %d\n%s", lineNumber, code, message);     } } Check Class:   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); } and This is a test class for testing the method: public class TestFile {     public static void main(String[] args) {         System.out.println("This is a really really really long line that should fail the long line check");         while (true) {             System.out.println("");             /break;         }     } } 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. Currently, /break is being ignored as error when it should not be ignored because it has / in front. This is the only error I encountered.

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 my BreakCheck java method that can be used in Linter program:
Here's my code:
BreakCheck class:

import java.util.*;

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

My LinterMain Class:

import java.util.*;
import java.io.*;

public class LinterMain {
    public static final String FILE_NAME = "TestFile.java";

    public static void main(String[] args) throws FileNotFoundException{
        List<Check> checks = new ArrayList<>();
        checks.add(new LongLineCheck());
        checks.add(new BlankPrintlnCheck());
        checks.add(new BreakCheck());
        Linter linter = new Linter(checks);
        List<Error> errors = linter.lint(FILE_NAME);
        for (Error e : errors) {
            System.out.println(e);
        }
    }
}

 

My Error Class:

import java.util.*;

public class Error {
    private final int code;
    private final int lineNumber;
    private final String message;

    public Error(int code, int lineNumber, String message) {
        this.code = code;
        this.lineNumber = lineNumber;
        this.message = message;
    }

    public int getLineNumber() {
        return lineNumber;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String toString() {
        return String.format("(Line: %d) has error code %d\n%s", lineNumber, code, message);
    }
}

Check Class:

 

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);
}

and This is a test class for testing the method:

public class TestFile {
    public static void main(String[] args) {
        System.out.println("This is a really really really long line that should fail the long line check");
        while (true) {
            System.out.println("");
            /break;
        }
    }
}

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.

Currently, /break is being ignored as error when it should not be ignored because it has / in front. This is the only error I encountered.

Feedback
1 2 3 4 5
Not yet
TESTCASES
The Error class generates errors correctly, and toString works properly
LongLineCheck correctly checks for long lines
BlankPrintInCheck correctly checks for blank printins
BreakCheck correctly tests for break statements
Failed: /break should not be ignored ==> expected: <true> but was: <false>
Show stacktrace
The Linter class correctly parses through a file, and finds the right errors
my-check.txt is not empty
5/6 passed
x
12
13
14
15
1 //Name: Narnnaphon Saraboon
2 //Class: CSE 122
3 //Instructor: Miya Natsuhara
4 //TAS: Terada Jin
5 //Date: 26 Feb 2023
6
7 import java.util.*;
8
16
17 }
my-ch...
Error.j...
9 public class BreakCheck implements Check {
10
11
Console
}
LongLi...
/home/BreakCheck.java 10:1 Spaces: 4 (Auto)
}
return Optional.empty();
TestFil...
✓ Program exited with code 0
(Line: 9) has error code 1
Line is too long
(Line: 11) has error code 3
Line contains 'System.out.println("")'
(Line: 12) has error code 2
Line contains 'break' keyword outside of a comment
BreakChe...
public Optional<Error> lint(String line, int lineNumber) {
// Match "break" outside and inside of comments or strings
if (line.matches (".*\\bbreak\\b.*") && !line.matches ("(?s).*\\s*//.*\\bbreak\\b.*"))
return Optional.of (new Error(2, lineNumber, "Line contains 'break' keyword outsid
Check....
Blank...
Linter...
O ✿
All changes saved ●
✓ Mark
▶ Run
Transcribed Image Text:Feedback 1 2 3 4 5 Not yet TESTCASES The Error class generates errors correctly, and toString works properly LongLineCheck correctly checks for long lines BlankPrintInCheck correctly checks for blank printins BreakCheck correctly tests for break statements Failed: /break should not be ignored ==> expected: <true> but was: <false> Show stacktrace The Linter class correctly parses through a file, and finds the right errors my-check.txt is not empty 5/6 passed x 12 13 14 15 1 //Name: Narnnaphon Saraboon 2 //Class: CSE 122 3 //Instructor: Miya Natsuhara 4 //TAS: Terada Jin 5 //Date: 26 Feb 2023 6 7 import java.util.*; 8 16 17 } my-ch... Error.j... 9 public class BreakCheck implements Check { 10 11 Console } LongLi... /home/BreakCheck.java 10:1 Spaces: 4 (Auto) } return Optional.empty(); TestFil... ✓ Program exited with code 0 (Line: 9) has error code 1 Line is too long (Line: 11) has error code 3 Line contains 'System.out.println("")' (Line: 12) has error code 2 Line contains 'break' keyword outside of a comment BreakChe... public Optional<Error> lint(String line, int lineNumber) { // Match "break" outside and inside of comments or strings if (line.matches (".*\\bbreak\\b.*") && !line.matches ("(?s).*\\s*//.*\\bbreak\\b.*")) return Optional.of (new Error(2, lineNumber, "Line contains 'break' keyword outsid Check.... Blank... Linter... O ✿ All changes saved ● ✓ Mark ▶ Run
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Math class and its different methods
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