CSci 2101 Lab 2. Java methods; stacks.

Due Monday, February 6, at 11:59pm (by e-mail)

55 points

The lab will be done in pairs.

Reminder of the setup:

Lab tasks

Make sure that you test your porgrams well. Consider different cases. Your testing will be taken into account in your grade.

Problem 1 (10 points)

Write and test a method that takes a positive integer number and prints out all of its pairs of factors. For instance, when the number is 12, the method should print:

12 can be factored as:
1 and 12
2 and 6
3 and 4
If the number is a perfect square, it's ok to print its root twice:

16 can be factored as:
1 and 16
2 and 8
4 and 4
However, make sure thatt every pair is printed only once. A function that gives you a square root of a number is Math.sqrt. You can use it as Math.sqrt(16) (which results in 4). Check what it returns for numbers that aren't perfect squares.
Call the method from main with different value of parameter, for testing.

Problem 2 (15 points): Postfix calculator

Finish the Postfix calculator problem, as started in class.

Problem 2 (30 points): 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 StackLab {

	 * 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.out.println("Please enter your input string");
		String input =; // reads until a whitespace or new line
		// checking the input/output
		// this variable might be helpful
		boolean isValid = true;



How to submit

Send the java file(s) by email to me: elenam at The subject of the message must be 2101 Lab 2. Make sure to CC your group partner if you worked with another person.

CSci 2101 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.