Create a class Polynomial that is used to evaluate a polynom
Solution
import java.util.*;
public class Polynomial
 {
 int max;
 static int array[]= new int[100];
private Polynomial(int max)
 {
 for(int j=0;j<=max;j++)
 {
 array[j]=0;
 }
 }
   
 void secConstant(int i,int value)
 {
 array[i]=value;
 }
 double evaluate(double x)
 {
 double px=0;
 System.out.println(max);
 for(int i=0;i<=max;i++)
 {
 px=px+array[i]*(pow1(x,i));
 }
 return px;
 }
 double pow1(double x,int j)
 {
 double s=1;
 int i;
 for(i=0;i<j;i++)s=s*x;
 return s;
 }
 public static void main(String[] args)
 {
 System.out.println(\"Enter Degree of polynomial\");
 Scanner scan=new Scanner(System.in);
 int m=scan.nextInt(),i,value;
 Polynomial p=new Polynomial(m);
 p.max=m;
 System.out.println(\"Enter Coefficients for polynomial from a0 to an:\");
 for(i=0;i<=m;i++)
 {
 value=scan.nextInt();
 p.secConstant(i, value);
 }
 System.out.println(\"Enter X value\");
 double x=scan.nextInt();
 System.out.println(\"P(x) is:\"+p.evaluate(x));
 }
 }
output:-
run:
 Enter Degree of polynomial
 3
 Enter Coefficients for polynomial from a0 to an:
 3 5 0 2
 Enter X value
 7
 3
 P(x) is:724.0
 BUILD SUCCESSFUL (total time: 11 seconds)


