The main goals for this lab are:
You will need to have a TA check off on all your exercises. If you do not complete the lab during the lab session, you must have a TA check off all your exercises during office hours.
During today’s class we introduced checkstyle. Checkstyle is an open-sourced tool that allows one to easily check whether their code complies with customizable rules.
Your task is to modify LookupZip.java
from lecture08
so that the checkstyle passes.
Getting Started:
wget https://raw.githubusercontent.com/BMC-CS-151/class-examples-f23/main/cs151_checks.xml
java -jar checkstyle-8.16-all.jar –c cs151_checks.xml LookupZip.java
to see the checkstyle errors.LookupZip.java
until the above command runs without any errors.Download PostfixEvaluator.java
from
wget https://raw.githubusercontent.com/BMC-CS-151/BMC-CS-151.github.io/main/labs/lab05/PostfixEvaluator.java
This is an implementation of the postfix evaluator we discussed in class with the built-in Java Stack
.
Look through the code to understand how it work. Run PostfixEvaluator.java
to interact with it a bit.
Run the tests we provide in TestPostFixEvaluator
.
You can download TestPostfixEvaluator.java
from
wget https://raw.githubusercontent.com/BMC-CS-151/BMC-CS-151.github.io/main/labs/lab05/TestPostfixEvaluator.java
Make sure the checkstyle passes for TestPostfixEvaluator.java
.
When you run the checkstyle for TestPostfixEvaluator
you will see 7
errors. Fix all them
and then continue.
Once the test file passes the checkstyle, you will run the tests. To run the tests, you will need to make sure you download two jar files:
wget https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar
wget https://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
To compile and run the tests, run the following command:
javac -cp .:junit-4.13.2.jar TestPostfixEvaluator.java
java -cp "./junit-4.13.2.jar:./hamcrest-core-1.3.jar:./" org.junit.runner.JUnitCore TestPostfixEvaluator
You can alternatively use the run_junit.sh
script I created in class: https://github.com/BMC-CS-151/class-examples-s24/blob/main/lec07/Stacks/run_junit.sh
Test it using TestPostfixEvaluator.java. Make sure you understand how it works.
Now, add 4 more tests to TestPostfixEvaluator
. The tests should each combine multiple operators.
Copy PostFixEvaluator.java
to a new file called PostfixStringEvaluator.java
.
Modify PostfixStringEvaluator.java
to convert a postfix expression to a parenthesized infix
expression and display it to the user. Modifications to PostfixStringEvaluator.java
should not be
extensive. The only difference is that when encountering an operator, instead of pushing the
result of the arithmetic operation involving this operator and the top two operands onto the
stack, i.e. a number, you will consruct the string representing that arithmetic operation and
push the string onto the stack instead. Below are some examples:
Sample Input 1
5 6 + 9 *
Output
( ( 5 + 6 ) * 9 )
Sample Input 2
8 9 10 + *
Output
( 8 * ( 9 + 10 ) )
Make sure PostFixStringEvaluator.java
complies with the style guide.
We provide three tests in TestPostfixStringEvaluator
.
You can download TestPostfixStringEvaluator.java
from
wget https://raw.githubusercontent.com/BMC-CS-151/BMC-CS-151.github.io/main/labs/lab05/TestPostfixStringEvaluator.java
Again, make sure that TestPostfixStringEvaluator.java
passes the checkstyle.
There will be 7
checkstyle errors in the file we are providing you.
Now add 4 more tests, make them a bit complicated.
Now we are going to implement 2 stacks using 1 array.
Imlement a DoubleStack
class using a single underlying array
that stores two different stacks (stack 1 and stack 2).
One of the stacks grows upwards
from index 0 upward, and the other stacks grows from the end of the array down. So these two stacks
grow towards each other. Unlike ExpandableArray
you can have empty spaces in your data structure.
The top indexes are denoted by top1
and top2
for stack 1 and
stack 2, respectively. Thus, the DoubleStack
class should have three instance variables:
Make sure theArray
locations 0 to top1 contain elements in stack 1 and theArray locations
theArray.length-1 downto top2 stores the elements in stack 2. You can assume a max stack size of 100 for each stack.
Implement the following methods.
If the runtime of any of the methods besides for printStack
is not
O(1)
, you are writing unnecessary loops.
Before implementing the tests, make sure to create a test fille called TestDoubleStack.java
that uses JUnit tests.
push e onto stack stackId (1 or 2). In other words, it
will push onto stack 1 if stackId==1 and onto stack 2 if stackId==2. Throw an
IllegalStateException
if stack is full. Throw an IllegalArgumentException
if the stackId is not 1 or 2.
pop from stackId, return null if empty. Throw an IllegalArgumentException
if the stackId is not 1 or 2.
top element from stackId, return null if empty. Throw an IllegalArgumentException
if the stackId is not 1 or 2.
return size of stack stackId. Throw an IllegalArgumentException
if the stackId is not 1 or 2.
Throw an IllegalArgumentException
if the stackId is not 1 or 2.
Throw an IllegalArgumentException
if the stackId is not 1 or 2.
In todays lab we covered checkstyle, stacks, and unit testing.
Before leaving, make sure your TA/instructor have signed you out of the lab. If you finish the lab early, you are free to go. If you do not finish the lab in time, you will need to go to office hours so that a TA can check your work.