class StackDemo {

	public static int evaluatePostfixExpression(String expression) {
		ObjectStack stack = new ObjectStack();
		String[] parts = expression.split(" +");

		for (String part : parts) {
			try {
				Integer number = Integer.parseInt(part);

				stack.push(number);
			} catch (NumberFormatException e) {
				if (part.length() != 1) {
					String message = String.format("Invalid operator: '%s'", part);

					throw new IllegalArgumentException(message);
				}

				operate(stack, part.charAt(0));
			}
		}

		if (stack.size() != 1) {
			throw new IllegalArgumentException("Invalid expression");
		}

		return (int) stack.pop();
	}

}