CSci 2101 Lab 3. Stacks.

Due Monday, September 19th at 11:59pm (by e-mail)

30 points

The lab is done in pairs.

Using a stack to check for matching parentheses

Implement the program according to the comments in the strating code (see below). Please include all of your test cases and describe their results (in comments).

Your program will be graded based on correctness of the results, completeness of test cases, clear documentation (if some cases are not handled properly, document them), code quality, and on the quality of error messages for invalid strings.

You might find the break statement useful. It allows you to break out of a loop, i.e. stop the loop execution.


import java.util.Scanner;
import java.util.Stack;

public class Lab2Stack {

	/**
	 * A solution for the matching parentheses problem.
	 * A given string consists of characters (without white spaces) and 
	 * may have any number of parentheses and curly braces. The program 
	 * checks if all parentheses and braces form matching 
	 * non-overlapping pairs. There may be any symbols in-between 
	 * parentheses and/or braces. 
         *
         * For instance, the following two 
	 * strings are valid sequences: 
	 * ab(c{}de(fghi))jk
	 * (a(b)c)d{e(f)}g
         *
	 * The following sequences are not valid:
	 * ab(c{de(fghi))jk (missing a closing curly brace)
	 * (a(b))c)d{e(f)}g (an extra closing parenthesis after c)
	 * (a{b)c} (overlapping parentheses and braces)
	 * }{ (a closing brace before an opening one)
	 * a(b(c) (missing a closing parenthesis)
         *
	 * The program prints the given string and the word "valid"
	 * or "invalid". If the string is invalid, additionally a brief 
	 * message is printed indicating what the problem is.
	 */
	public static void main(String[] args) {
		Stack<Character> symbols = new Stack<Character>();
		
		// reading input. Assumption: no whitespaces
		Scanner read = new Scanner(System.in);
		System.out.println("Please enter your input string");
		String input = read.next(); // reads until a whitespace or new line
		
		// checking the input/output
		System.out.println(input);
		
		// this variable might be helpful
		boolean isValid = true;
		

	}

}

How to submit

Submit the java file(s) by e-mail to me. The subject of the message must be 2101 Lab 3. Make sure to CC your group partner.


CSci 2101 course web site.