import java.util.Scanner; public class Polynomial { Scanner input = new Scanner(
ID: 3530284 • Letter: I
Question
import java.util.Scanner; public class Polynomial { Scanner input = new Scanner(System.in); double value = input.nextDouble(); private int degree; private double[] coefficients; public Polynomial(int max) {//Polynomial(int max): a constructor that creates a polynomial of degree max whose coefficients are all zero degree = max; coefficients = new double[degree+1]; max = (int)coefficients[0]; for (int i = 0; i < coefficients.length; i++) { if (max < coefficients.length) max = (int)coefficients[i]; } } public int getDegree() { return degree; } public double getCoefficient(int i) { return coefficients[i]; } public void setCoefficient(int i, double value) {//void setCoefficient(int i, double value): sets the coefficient to value (i is in the range 0 to degree, namely there are degree + 1 coefficients in total) value = coefficients[i]; } public double evaluate(double x) {//double evaluate(double x): returns the value of the polynomial for the given value x double result = coefficients[0]; // Compute polynomial value double sum = 0, exponent=degree; double y = 0; for (int i=degree;i>=0;i--) { y = coefficients[i]*Math.pow(x,exponent); exponent--; sum+=y; result = y; } return y; } public static void main(String[]args){ System.out.println("p(x) = " + y.evaluate(3)); } }Explanation / Answer
Create some other class and write thepublic static void main(String[]args) mrthod
in that class and craete an object of polynomail class like
Polynomial y = new Polynomial(5);
now call the methods inside Polynomial on object y.
Also remove the main function inside Polynomial class
Hope this helps.Please rate it FIRST . Reply for any doubts
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.