-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolynomial.java
59 lines (44 loc) · 1.48 KB
/
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
52
53
54
55
56
57
58
59
import java.lang.Math;
public class Polynomial{
double[] coefficient;
public Polynomial(){
coefficient= new double[1];
}// this is the end of he no arguements case
public Polynomial(double[] numb){
coefficient = new double[numb.length];
for(int i=0; i < numb.length ; i++ ){
coefficient[i] = numb[i];
}
}// this is the end of he no arguements case
public Polynomial add ( Polynomial numb){
if (coefficient.length > numb.coefficient.length){
// add the the numb to the coefficient and then return the resulting polynomial
for (int i=0; i<numb.coefficient.length; i++){
coefficient[i] += numb.coefficient[i];
}
return new Polynomial(coefficient);
}
else{
// add the coeffieemt to the numb and return the resultingn numb
for (int i=0; i<coefficient.length; i++){
numb.coefficient[i] +=coefficient[i];
}
return numb;
}
}// end of the add method
public double evaluate (double x){
double result=0 ;
for(int i=0; i< coefficient.length; i++){
result += coefficient[i]*Math.pow(x, i);
}
return result;
}
public boolean hasRoot(double x ){
if (evaluate(x)==0){
return true;
}
else{
return false;
}
}
}// this is the end of the class constuctor