The lab will be done in pairs.
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).cd
followed by the path
to the folder. Use Tab
key for auto-complete.
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). Make sure that you test your porgrams well. Consider different cases. Your testing will be taken into account in your grade.
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.
Finish the Postfix calculator problem, as started in class.
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.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;
}
}
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.