mkdir ~/cs171/hw4 cp /home/cs171000/Handouts/hw4/*.java ~/cs171/hw4 |
DoubleStack.java Interface of a stack that store double type DoubleArrayStack.java An implementation of DoubleStack interface StringStack.java Interface of a stack that store String type StringArrayStack.java An implementation of StringStack interface Eval1.java Work file for homework 4 testProg1.java Test program for homework 4 Eval2.java Work file for bonus project testProg2.java Test program for bonsu project |
|
|
|
|
|
public class Eval1
{
/* =====================================================
isNumber(s): returns true if s is a "numeric string"
i.e., a string that can be converted
to a double value using:
Double.parseDouble( s )
returns false otherwise
===================================================== */
public static boolean isNumber( String s )
{
return s.matches("-?\\d+(\\.\\d+)?");
// Check if s matches a decimal number
// with optional '-' and decimal.
}
/* =====================================================
evalExpr(s): s is an array of strings that makes up
an expression with + and - operations
Operators must be evaluated left to right:
1 - 1 - 1 = -1
1. evaluates the expression
2. return the final result
===================================================== */
public static double evalExpr( String[] s )
{
// Write this method
}
}
|
|
|
testProg1.java
|
javac testProg1.java
|
To run:
java testProg1 ... ... ... (The ... ... ... represents a numerical expression) |
java testProg1 3.0 + 2.0 result = 5.0 java testProg1 3 + 2.1 - 5.1 - 1 + 2 result = 1.0 java testProg1 4.1 - 2.2 - 7.1 - 3 + 2 result = -6.199999999999999 |
Warning:: you must leave a space between each input item (or else, you program will crash)
|
|
|
java testProg2 2 x 3 result = 6.0 java testProg2 7 - 2 x 3 result = 1.0 java testProg2 2 - 3 - 2 x 4 x 3 - 3 x 3 result = -34.0 java testProg2 3 / 2 x 4 - 3 / 2 x 3 result = 1.5 java testProg2 8 / 2 / 4 + 6 / 2 / 3 result = 2.0 |
cd ~/cs171/hw4 /home/cs171000/turnin Eval1.java hw4 |
But use a different turn in code.
Use the following command (from inside your cs171/hw4 project directory) to turn in your bonus prject (only if you have done it):
cd ~/cs171/hw4 /home/cs171000/turnin Eval2.java hw4b |