forked from wedusk101/NumericalAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomial.java
51 lines (41 loc) · 1021 Bytes
/
Polynomial.java
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
import java.util.Scanner;
public class Polynomial
{
private int coef[]; // coefficients
private int deg; // degree of polynomial (0 for the zero polynomial)
Scanner s=new Scanner(System.in);
private void getDegree()
{
System.out.println("Enter the highest degree for the Polynomial");
deg=s.nextInt();
}
private void getCoef()
{
for(int i =coef.length-1; i >= 0; i--)
{
System.out.println("Enter the Coefficient value when exponent of the term is\t"+i);
coef[i]=s.nextInt();
}
s.close();
}
public Polynomial()
{
getDegree();
coef=new int[deg+1];
getCoef();
}
public double evaluate(int x)
{
double result = 0;
for (int i = coef.length-1; i >= 0; i--)
result = coef[i] + (x * result);
return result;
}
/*public static void main(String[] args) //for testing
{
Polynomial p1;
p1=new Polynomial();
double c=p1.evaluate(2);
System.out.println(c);
}*/
}