private void rpnToResult()
{
// stack is used to hold operands and each calculation
Stack<Double> calcStack = new Stack<Double>();
// RPN is processed, ultimately calcStack has final result
for (String token : this.reverse_polish)
{
// If the token is an operator, calculate
if (isOperator(token))
{
// Pop the two top entries
double e1 = calcStack.pop();
double e2 = calcStack.pop();
// Calculate intermediate results
switch (token) {
case "+":
result = e2 + e1;
break;
case "-":
result = e2 - e1;
break;
case "*":
result = e2 * e1;
break;
case "/":
result = e2 / e1;
break;
case "%":
result = e2 % e1;
break;
default:
break;
}
// Push intermediate result back onto the stack
calcStack.push( result );
}
// else the token is a number push it onto the stack
else
{
calcStack.push(Double.valueOf(token));
}
}
// Pop final result and set as final result for expression
this.result = calcStack.pop();
}
Add unbalanced parenthesis check and in original FRQ, or other error checks. FYI, usually when structuring code with error checking it can greatly impact code structure.
public boolean isBalanced(){
int countOpen=0;
int countClose=0;
for (int i=0; i<this.expression.length(); i++){
if (this.expression.substring(i, i+1).equals("(")){
countOpen++;
}
else {
countClose++;
}
if(countClose > countOpen)
return false;
}
return countOpen == countClose;
}
Build in Power of operator ^:2 ^ 1 = 2, 2 ^ 2 = 4, 2 ^ 3 = 83. Build an API to receive an expression and respond with a result. This is a good opportunity to respond with an error if you built in parenthesis or other error checking.
private final Map<String, Integer> OPERATORS = new HashMap<>();
{
// Map<"token", precedence>
OPERATORS.put("*", 3);
OPERATORS.put("/", 3);
OPERATORS.put("%", 3);
OPERATORS.put("+", 4);
OPERATORS.put("-", 4);
OPERATORS.put("^", 4);
}
private void tokensToReversePolishNotation () {
// codes are hided
case "+":
case "-":
case "*":
case "/":
case "%":
case "^":
// codes are hided
}
private void rpnToResult()
{
//codes hided
case "^": //pow() java use for power rule https://www.javatpoint.com/java-math-pow-method
result = Math.pow(e2,e1);
break;
default:
break;
}
//codes hided
}
api
@RestController
@RequestMapping("/api/calculator")
public class CalculatorApiController {
@GetMapping("{expression}")
public ResponseEntity<String> getExpression(@PathVariable String expression) throws JsonMappingException, JsonProcessingException {
// Backend Year Object
Calculator test = new Calculator(expression);
return ResponseEntity.ok(test.toString()); // JSON response, see ExceptionHandlerAdvice for throws
}
}
Tester Method Result Output
API test result in Postman
FRQ3 Collageboard
public class Delimiters {
// part1 private instances variables global variables
private String openDel;
private String closeDel;
// part2 constructors
public Delimiters(String open, String close)
{
openDel=open;
closeDel=close;
}
// part3 non-constructor methods normal methods
public ArrayList<String> getDelimitersList(String[] tokens){
// ArrayList<DataType> ListName = new ArrayList<DataType>();
ArrayList<String> delimiters = new ArrayList<String>();
for(int i=0; i<tokens.length; i++){
if(tokens[i].equals(openDel) || tokens[i].equals(closeDel)){
delimiters.add(tokens[i]);
}
}
return delimiters;
}
public boolean isBalanced(ArrayList<String> delimiters){
int countOpen=0;
int countClose=0;
for (int i=0; i<delimiters.size(); i++){
if (delimiters.get(i).equals(openDel)){
countOpen++;
}
else {
countClose++;
}
if(countClose > countOpen)
return false;
}
return countOpen == countClose;
}
}
note, different between Array, Array list, list
String str
for(int i=0; i<str.length(); i++)
str.substring(i,i+1) // means get every character in String
01234
"Tianbin".substring(0,1) = "T"
.substring(start , end) [start, end) from start to end-1
Array arr
for(int i=0; i<arr.length; i++)
arr[i] // means get every element in the arr
ArrayList list
for(int i=0; i<list.size(); i++)
list.get(i) // means get every item in the ArrayList
Finish rpnToResult for Calculator
Add unbalanced parenthesis check and in original FRQ, or other error checks. FYI, usually when structuring code with error checking it can greatly impact code structure.
Build in Power of operator ^:2 ^ 1 = 2, 2 ^ 2 = 4, 2 ^ 3 = 83. Build an API to receive an expression and respond with a result. This is a good opportunity to respond with an error if you built in parenthesis or other error checking.
api
Tester Method Result Output
API test result in Postman
FRQ3 Collageboard
note, different between Array, Array list, list