## CSci 2101 Lab 2. Java methods; stacks.

### 55 points

The lab will be done in pairs.

#### Reminder of the setup:

• Start a terminal window
• If you need to reset your password: type `passwd` on the command line in a terminal window. You will be asked to type your current password and then the new password twice. Note that as you are typing your password, nothing shows up on the screen (as a security measure).
• You might want to create a new folder for this lab.
• In the terminal window type `cd` followed by the path to the folder. Use `Tab` key for auto-complete.
• Start jEdit or gedit. You can starts typing your program in the text editor and use "Save as" to save it into your folder. Don't forget to name the file the same as the class in the program and use .java extension.
• Compile your file: `javac FirstJava.java` (use your file name instead of FirstJava). If there are no errors, run it: `java FirstJava` (again, use your file name instead of FirstJava).

#### 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
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

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

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.