Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

. ExpressionEvaluator.java Requirements: Calculate the following expression for

ID: 3882235 • Letter: #

Question

. ExpressionEvaluator.java Requirements: Calculate the following expression for a value x of type double which is read in from the keyboard, and save the result of the expression in a variable of the type double. You must use the sqrt), abs0 and pow0 methods of the Math class to perform the calculation. You may use a single assignment statement with a single expression, or you may break the expression into appropriate multiple assignment statements. The latter may easier to debug if you are not getting the correct result. 110, 4x10-x + 1/ (3.5x4 + 2.5x2 + 1.5x + 0.5) Next, determine the number of digits to the left and to the right of the decimal point in the unformatted result. [Hint: You should consider converting the type double result into a String using the static method Double.toString(result) and storing it into a String variable. Then, on this String variable use the indexoff".") method from the String class to find the position of the period (i.e., decimal point) and the length) method to find the length of the String. Knowing the location of the decimal point and the length, you should be able to determine the number of digits on cach side of the decimal point. Finally, the result should be printed using the class java.text.Decimal Format so that to the right of the decimal there are at most five digits and to the left of the decimal each group of three digits is separated by a comma in the traditional way. Also, there should also be at least one digit on each side of the decimal (eg.. 0 should be printed as 0.0). Hint: Use the pattern "#,##0.0####" in your

Explanation / Answer

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

//ExpressionEvaluator.java

import java.text.DecimalFormat;

import java.util.Scanner;

public class ExpressionEvaluator {

      /**

      * method to evaluate the expression

      *

      * @param x

      *            - given x value

      * @return the result after applying the equation

      */

      static double eval(double x) {

            /**

            * Dividing the expression into multiple terms. a- expression inside

            * absolute part of the numerator, b - denominator expression

            */

            double a = Math.abs(10.4 * Math.pow(x, 10) - x + 1);

            double b = (3.5 * Math.pow(x, 4)) + (2.5 * Math.pow(x, 2)) + (1.5 * x)

                        + 0.5;

            double result = Math.sqrt(a) / b;

            return result;

      }

      /**

      * method to determine the number of digits in left and right side of the

      * decimal point in a double value, and print the value in a formatted

      * manner

      *

      * @param unFormattedValue

      *            - unformatted value in double

      */

      static void determineDigits(double unFormattedValue) {

            /**

            * Converting to String

            */

            String string = Double.toString(unFormattedValue);

            /* finding the index of decimal point */

            int index = string.indexOf(".");

            /* finding the length of string */

            int length = string.length();

            /* number of digits left= index of the decimal point */

            int numDigitsLeft = index;

            /* number of digits right= length - index of the decimal point+1 */

            int numDigitsRight = length - (index + 1);

            System.out.println("# digits to left of decimal point: "

                        + numDigitsLeft);

            System.out.println("# digits to right of decimal point: "

                        + numDigitsRight);

            /**

            * Defining a DecimalFormat object to format the number in the required

            * order

            */

            DecimalFormat formatter = new DecimalFormat("#,##0.0####");

            /**

            * Displaying the formatted result

            */

            System.out.println("Formatted Result: "

                        + formatter.format(unFormattedValue));

      }

      public static void main(String[] args) {

            /**

            * Scanner object to receive user input

            */

            Scanner scanner=new Scanner(System.in);

            System.out.println("Enter a value for x: ");

            /*receiving value for x*/

            double value=Double.parseDouble(scanner.nextLine());

            /*evaluating expression*/

            double result=eval(value);

            /*printing the result*/

            System.out.println("Result: "+result);

            /*determining number of digits, and formatting the number*/

            determineDigits(result);

      }

}

/*OUTPUT 1*/

Enter a value for x:

98765432109876543210

Result: 9.100255660478933E19

# digits to left of decimal point: 1

# digits to right of decimal point: 18

Formatted Result: 91,002,556,604,789,330,000.0

/*OUTPUT 2*/

Enter a value for x:

120.556

Result: 111.07491894566802

# digits to left of decimal point: 3

# digits to right of decimal point: 14

Formatted Result: 111.07492