JAVA
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*Example:
input line for function: 3x^2 + 5x + 3
input line for argument: 2
Solution = 25
Note: You can input either integers or floating point
numbers for your argument or coefficients
*/
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
try{
Scanner input = new Scanner(System.in);
StringBuilder fx = new StringBuilder(input.nextLine());
System.out.println("f(x) = " + fx);
while (fx.toString().contains(" ") || fx.toString().contains("+")){
if (fx.indexOf(" ") != -1)
fx.deleteCharAt(fx.indexOf(" "));
if (fx.indexOf("+") != -1)
fx.deleteCharAt(fx.indexOf("+"));
}
double arg = input.nextDouble();
String[] coefficients = fx.toString().split("x\\^2|x");
if (coefficients.length != 3)
throw new Exception("Invalid function");
System.out.printf("Therefore, f(%.3f) = %.3f", arg, computeQuadratic(arg, coefficients));
}
catch(Exception ex){
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run