JAVA programming
The task of parsing a file for correctness is an essential part
of any programmer toolkit. The check for a
balanced set of brackets in a file a Stack can be used and the
following algorithm:
• The source file is read character by character
• Each time an opening '{' is read it is pushed onto the
stack
• Each time a closing '}' is read the stack is popped
• If the stack is empty when a closing '}' is read then there must
be a missing '{'
• If the stack is not empty at the end of the file then there must
be a missing closing '}'
Create the following class.
1. BracketChecker - this class contains:
i. a single private instance variable of type
Stack<Character> and initialise it to new
Stack<Character>()
ii. a single method check that takes a single formal parameter of
type String and returns a
boolean, i.e.
public boolean check(String text) { return false; }
2. Modify the main method so that it:
i. Prints the message "Starting Bracket Checker application"
ii. Defines a variable called checker of type BracketChecker,
initialised to a new
BracketChecker object.
iii. Defines a variable called in of type Scanner, initialised to
new Scanner(System.in). This
will allow input from the keyboard.
iv. Calls in.nextLine() and passing the result to the check method
of checker
v. Prints the result of the method call.
1. if it is true then "Syntax Correct"
2. if it is false then "Syntax Error"
Run the project. The following output should be produced (example
input, {{}} is shown in bold below):
Starting Bracket Checker application
{{}}
Syntax Error
NOTE: this program will always print Syntax Error as the stub
method check always returns false
The Tasks
1. Fully Implement the check method in BracketChecker so that it
examines each character (hint:
use .charAt(int index)) in the String passed as a parameter
i. if it is an opening '{' push it onto the stack
ii. if it is a closing '}'
▪ and the stack is empty, then return false
▪ pop the stack, .pop()
iii. any other character is ignored (not pushed onto the
stack)
iv. when the end of the string is reached
▪ if the stack is empty, then return true
▪ if the stack is not empty, then return false
2. Add a while loop to main method in Practical03.java so that
multiple lines can entered for
checking
3. Add a print statement at the end stating Exiting Checker
4. Run the application. The following output should be produced
(example input is indicated in bold):
Starting Bracket Checker application
{{}}
Syntax Correct
{{}
Syntax Error
{"Hello{}World"}
Syntax Correct
Exiting Checker
CODE:
import java.util.*;
class BracketChecker
{
private Stack<Character> stack = new Stack<Character>();
public boolean check(String text)
{
for(int i=0;i<text.length();i++)
{
if(text.charAt(i)=='}')
{
if(stack.empty())
return false;
else
stack.pop();
}
if(text.charAt(i)=='{')
stack.push('{');
}
if(stack.empty())
return true;
else
return false;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Starting Bracket Checker Application");
while(true)
{
String ip = sc.nextLine();
if(ip.equals("")) // If the user does not enter any string or empty string then break the loop
{
System.out.println("Exiting Checker");
break;
}
BracketChecker ob = new BracketChecker();
if(ob.check(ip))
System.out.println("Syntax Correct");
else
System.out.println("Syntax Error");
}
}
}
OUTPUT:

JAVA programming The task of parsing a file for correctness is an essential part of any...
Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces or curly braces, i.e., the characters in ’([{}])’, returns True if the parentheses, brackets and braces match and False otherwise. Your solution must use a Stack. For, example: >>> parenthesesMatch('(){}[]') True >>> parenthesesMatch('{[()]}') True >>> parenthesesMatch('((())){[()]}') True >>> parenthesesMatch('(}') False >>> parenthesesMatch('({])') False >>> parenthesesMatch('((())') False >>> parenthesesMatch('(()))') False >>> Hint: It is not sufficient to just count the number of opening and closing...
Please solve the following problem with programming using proper data structures. (Programming Language: Python) A similar application to the parentheses matching problem comes from hypertext markup language (HTML). In HTML, tags exist in both opening and closing forms and must be balanced to properly describe a web document. This very simple HTML document: Example> Hello, world is intended only to show the matching and nesting structure for tags in the language. Write a program that can check an HTML document...
Using Java programming: Modify the delimiter class (provided below) so that it can "incorrectly" evaluate simple parenthesized math expressions . a) ask the user for the whole expression to solve b) Solve the expression only if there is no mismatch in the delimiters. If there is a mismatch, then output an error. If there is an error, allow the user to reenter a corrected/new expression. c) The program should be able to evaluate expressions that uses only the following basic...
I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...
Programming language: Java
Design an application that has three classes. The first one, named Book describes book object and has title and number of pages instance variables, constructor to initialize their values, getter methods to get their values, method to String to provide String representation of book object, and method is Long that returns true if book has over 300 pages, and returns false othewise. Provide also method char firstChard) that returns first cahracter of the book's title. The second...
*JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> { private int top, size; private E arrS[]; private static final int MAX_STACK_SIZE = 10; public ArrayStack() { this.arrS = (E[]) new Object[MAX_STACK_SIZE]; this.top = size; this.size = 0;...
Reverse Polish (HP) Style Calculator - Part 3 The purpose of this assignment is to build the business calculator using supporting files built in Topics 4 and 5. Create a Java application file named RPN.java containing a main method by using the ForthStack.java and associated files from Topic 5. The application should have one text box for numeric data entry, one text box for numeric display, one text box for error display, and buttons labeled "+", "-", "*", "/", "dup",...
Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...
Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains a Java program. Your program should read the file (e.g., InputFile.java) and output its contents properly indented to ProgramName_Formatted.java (e.g., InputFile_Formatted.java). (Note: It will be up to the user to change the name appropriately for compilation later.) When you see a left-brace character ({) in the file, increase your indentation level by NUM_SPACES spaces. When you see a right-brace character (}), decrease your indentation...
For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...