Question: I need help asap I am making a webbase calcualtor ( using spring mvc , maven on intelliJ ) I need help on A only

I need help asap
I am making a webbase calcualtor (using spring mvc, maven on intelliJ)
I need help on A only expression because when I run it, it is still asking me to enter something in input B
this is my controller
package com.Junhyeok.webcalculator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class WebCalculatorController{
// This method handles GET requests to the root URL ("/") and displays the calculator form.
@GetMapping("/")
public String showCalculator(){
return "calculator";
}
@PostMapping("/calculate")
public ModelAndView calculate(
@RequestParam("inputA") double inputA,
@RequestParam(value = "inputB", required = false) Double inputB,
@RequestParam("operation") String operation,
Model model){
double result;
switch (operation){
case "add":
result = inputA + inputB;
break;
case "subtract":
result = inputA - inputB;
break;
case "multiply":
result = inputA * inputB;
break;
case "division":
result = inputA / inputB;
break;
case "sin":
result = Math.sin(inputA);
break;
case "cos":
result = Math.cos(inputA);
break;
case "tan":
result = Math.tan(inputA);
break;
case "factorial":
result = factorial((int) inputA);
break;
case "power":
result = Math.pow(inputA, inputB);
break;
case "log":
result = Math.log(inputA)/ Math.log(inputB);
break;
case "root":
result = Math.pow(inputA,1/ inputB);
break;
case "equal":
result = inputA == inputB ?1 : 0;
break;
default:
throw new IllegalArgumentException("Invalid operation: "+ operation);
}
model.addAttribute("result", result);
model.addAttribute("inputA", inputA);
if (inputB != null){
model.addAttribute("inputB", inputB);
}
model.addAttribute("operation", operation);
ModelAndView modelAndView = new ModelAndView("calculator", "model", model);
return modelAndView;
}
private double factorial(double inputA){
if (inputA ==0)
return 1;
else
return inputA * factorial(inputA -1);
}
}
and this is my html
Web Calculator
Result:
I need help asap I am making a webbase calcualtor

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!