-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvalExpression.java
More file actions
54 lines (45 loc) · 1.46 KB
/
EvalExpression.java
File metadata and controls
54 lines (45 loc) · 1.46 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.Scanner;
import java.text.DecimalFormat;
/**
*
*
* @author Adam Bostwick
* @version 9/13/19
*/
public class EvalExpression
{
/** @param args Command line arguments (not used). */
public static void main(String[] args)
{
//define variables
double x = 0;
double result = 0;
String restring = "";
int dotPos = 0;
int theLength = 0;
int afterDot = 0;
double res2 = 0;
Scanner userInput = new Scanner(System.in);
String pattern = "#,##0.0###";
DecimalFormat traditional = new DecimalFormat(pattern);
//take input
System.out.print("Enter a value for x: ");
x = Double.parseDouble(userInput.nextLine());
userInput.close();
//formula
result = (12.4 * Math.pow(x, 6)) - (1.2 * Math.pow(x, 3));
res2 = Math.abs((2.6 * Math.pow(x, 5)) - (6.8 * x) + 7);
result = result + Math.sqrt(res2);
result = result / (Math.pow(x, 4) + 9);
//convert / analyze result
restring = Double.toString(result);
dotPos = restring.indexOf(".");
theLength = restring.length();
afterDot = theLength - (dotPos + 1);
//output result
System.out.print("Result: " + result
+ "\n# of characters to left of decimal point: " + dotPos
+ "\n# of characters to right of decimal point: " + afterDot
+ "\nFormatted Result: " + traditional.format(result));
}
}