Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 algsStack class.
Stack ADT
Write a program called Balanced.java in the package ds 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: abcdfgreab
Output: yes
Input:
Output: no
To implement the checking, you should do the following:
Scan each character from the input and ignore any nonenclosure characters.
When a left enclosure character is encountered eg a left square bracket
push it onto the stack you are maintaining. When a right enclosure character
eg 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 intxt one may run your program from command line
as follows:
java dsBalanced intxt
To read the string from the file, you should add the following two lines at the start
of your main function:
In in new Inargs;
String str inreadString;
You can test your program using the two files intxt and intxt provided with
the assignment.
Notice: On DL there is a document Contents Misc Eclipseprogramarguments about running your program from Eclipse while also specifying a file
path. This way, you can use the debugging features eg set breakpoints and
inspect variables provided by Eclipse.
Submission instructions
Name your file Balanced.java and put it into the ds package all code files
will be put into this package for future assignments Submit only the java
file into the DL 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 algsIn class for input and the algsStdOut 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 algsStack class. Do not use any other
implementations of stacks.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started