Skip to main content

Lab 5: CheckStyle, Stacks, Unit Testing

Objectives:

The main goals for this lab are:

  1. Get practice using a checkstyle
  2. Work with Stacks
  3. Get started with JUnit

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.

Exercise 1 - Checkstyle

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:

  1. Download the xml checkstyle from the course GitHub repository: wget https://raw.githubusercontent.com/BMC-CS-151/class-examples-f23/main/cs151_checks.xml
  2. Download the checkstyle-8.16 jar file from here: https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-8.16
  3. Run java -jar checkstyle-8.16-all.jar –c cs151_checks.xml LookupZip.java to see the checkstyle errors.
  4. Modify LookupZip.java until the above command runs without any errors.

Excercise 2 - DoubleStack

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:

  1. E[] theArray,
  2. int top1,
  3. int top2

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.

Methods to implement

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.

2.1 void push(int stackId, E e):

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.

2.2 E pop(int stackId):

pop from stackId, return null if empty. Throw an IllegalArgumentException if the stackId is not 1 or 2.

2.3 E top(int stackId):

top element from stackId, return null if empty. Throw an IllegalArgumentException if the stackId is not 1 or 2.

2.4 int size(int stackId):

return size of stack stackId. Throw an IllegalArgumentException if the stackId is not 1 or 2.

2.5 boolean isEmpty(int stackId)

Throw an IllegalArgumentException if the stackId is not 1 or 2.

2.6 String printStack(int stackId)

Throw an IllegalArgumentException if the stackId is not 1 or 2.

Wrap up

In todays lab we covered checkstyle, stacks, and unit testing.

Signing out

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.