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

Learning Objectives Writing output to a Text File, Using 1-D Arrays, Using 1-D A

ID: 3806630 • Letter: L

Question

Learning Objectives Writing output to a Text File, Using 1-D Arrays, Using 1-D Arrays as Formal Parameters, More on Modular Programming Definition 1. A univariate polynomial is a mathematical expression in volving a sum of powers in one variable multiplied by coefficients. A poly- nomial in one variable with constant coefficients is given by the expression p(ar) car" n-1 cm-2rn 2 car Co, where the ca's are nu meric values representing the coefficients. The polynomial is said to be an nth-degree polynomial if its highest power is n. For example, 3r4-2z3+r2-1 is a fourth-degree polynomial. To evaluate a univariate polynomial, given a numeric value, the value is substituted for the variable and the expression is evaluated. For example, given the polynomial p (a) 33r4 2a 3 +a2-1, p (-2) 67 since 3 x (-2) 2 x (-2) (-2)2-1 67. A univariate polynomial can be represented as an array of its coefficients in descending powers. For example, the polynomials 4-2a3 r2 -1 and 3a 2 2r 5 are represented as 3, -2, 1,0,-1] and 3r 3,2,-5, respectively. In this project, you will write a program to evaluate a polynomial using Horner's method and without the use of the standard Java Math library Math pow method. The naive way to evaluate a dense polynomial, one with relatively many non-zero coefficients, is to compute the power of each term and multiply it by the coefficient and then sum all the products. A much more efficient approach for evaluating a dense polynomial is Horner's method.

Explanation / Answer

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Scanner;

public class PolynomialEvaluator {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter the degree of the polynomial (deg p) -> ");

       int deg = Integer.valueOf(sc.next());

       System.out.print("Enter coefficients of p(x) in descending powers -> ");

       double[] coeffs = new double[deg+1];

       for (int i =0;i<=deg;i++){

           coeffs[i] = Double.valueOf(sc.next());

       }

       //System.out.println();

       System.out.print("Enter x to compute p(x) -> ");

       double x = Double.valueOf(sc.next());

       //System.out.println();

       System.out.print("Enter the output file name -> ");

       String filename = sc.next();

       System.out.println();

       String polyStr = polyToSring(coeffs);

       System.out.println("p(x) = "+ polyStr);

       double hEval = hornerEval(coeffs, x);

       System.out.println("p("+x+") = " +hEval );

       try{

          PrintWriter writer = new PrintWriter(filename, "UTF-8");

          writer.println(polyStr);

          writer.println(hEval);

          writer.close();

       } catch (IOException e) {

       // do something

       }

   }

   public static double hornerEval(double[] coeffs, double x){

      

       double result = coeffs[0]; // Initialize result

      for (int i=1; i<coeffs.length; i++)

      result = result*x + coeffs[i];

     

      return result;

   }

   public static String polyToSring(double[] coeffs){

       if (coeffs.length-1 == 0) return "" + coeffs[0];

      

if (coeffs.length-1 == 1) return coeffs[0] + "x "+ (coeffs[1]>0?"+ " + coeffs[1]: "- "+ (coeffs[1]- (2*coeffs[1])));

String s = coeffs[0] + "x^" + (coeffs.length-1);

//for (int i = coeffs.length-1; i >= 0; i--) {

for (int i = 1;i<= coeffs.length-1; i++) {

if (coeffs[i] == 0) continue;

else if (coeffs[i] > 0) s = s + " + " + ( coeffs[i]);

else if (coeffs[i] < 0) s = s + " - " + (-coeffs[i]);

if (i == coeffs.length-2) s = s + "x";

else if (i > 1) s = s + "x^" + i;

}

return s;

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote