Question: You are to write a program from scratch that uses a stack. For stack, you must use the algs 4 . Stack class. Stack ADT

You are to write a program from scratch that uses a stack. For
stack, you must use the algs4.Stack class.
Stack ADT
Write a program called Balanced.java in the package ds2 that reads a string from
a file (details will be given later). The string may contain any characters except
whitespace (that is, no spaces, tabs, or newlines). Among the characters will be
enclosures, that is, parentheses, square brackets, and curly brackets. Your
program will scan the string to determine whether those enclosures are balanced
with respect to each other and outputs a yes if balanced and a no if not.
Examples:
Input: ab{(cd[fg])[re]}ab
Output: yes
Input: {([)[]}
Output: no
To implement the checking, you should do the following:
Scan each character from the input and ignore any non-enclosure characters.
When a left enclosure character is encountered (e.g., a left square bracket [),
push it onto the stack you are maintaining. When a right enclosure character
(e.g., a right square bracket ]) is encountered, pop a left enclosure off the stack
and compare to see whether they match, for example, a right square bracket only
matches a left square bracket. If they dont match, print no and exit the
program. If they match, continue scanning the input.
Once the string has been completely scanned, check whether the stack is empty.
If not, print no. Otherwise, print yes.
Reading input from file
The input string should be read from a file. Additionally, the file name is specified
as an argument from the command line. This means that in order to read the
string from a file named in1.txt, one may run your program from command line
as follows:
java ds2.Balanced in1.txt
To read the string from the file, you should add the following two lines at the start
of your main function:
In in = new In(args[0]);
String str = in.readString();
You can test your program using the two files in1.txt and in2.txt provided with
the assignment.
Notice: On D2L, there is a document (Contents -> Misc -> Eclipse-programarguments) about running your program from Eclipse while also specifying a file
path. This way, you can use the debugging features (e.g., set breakpoints and
inspect variables) provided by Eclipse.
Submission instructions
Name your file Balanced.java and put it into the ds2 package (all code files
will be put into this package for future assignments). Submit only the .java
file into the D2L submission slot provided for it by its due date and time.
Make sure your name appears in comments at the top of the program.
You must use the algs4.In class for input and the algs4.StdOut class for
output. Do NOT use the Scanner or File Java classes for input. Do NOT use
System.out for output.
For stack, you must use the algs4.Stack class. Do not use any other
implementations of stacks.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!