-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.java
More file actions
67 lines (56 loc) · 1.92 KB
/
Copy pathPolynomial.java
File metadata and controls
67 lines (56 loc) · 1.92 KB
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
60
61
62
63
64
65
66
67
public class Polynomial {
/************************************************
* Fields:
* double [] coefficients: array of coefficients
***********************************************/
double [] coefficients = null;
/************************************************
* Constructor
* base case constructor
***********************************************/
public Polynomial (){
this.coefficients = new double [0];
}
/************************************************
* Constructor
* initialize coefficient array
***********************************************/
public Polynomial (double coefficents[]){
this.coefficients = coefficents;
}
/************************************************
* method
* adds coefficients of reference variable and input
***********************************************/
public Polynomial add(Polynomial p){
double [] larger = this.coefficients;
double [] smaller = p.coefficients;
if (p.coefficients.length >= this.coefficients.length) {
larger = p.coefficients;
smaller = this.coefficients;
}
for (int i = 0; i < smaller.length; i++) {
larger[i] = larger[i] + smaller[i];
}
Polynomial result = new Polynomial(larger);
return result;
}
/************************************************
* method
* evaluates polynomial at x=d
***********************************************/
public double evaluate(double d){
double sum = 0;
for(int i = 0; i < this.coefficients.length; i++){
sum += this.coefficients[i]*(Math.pow(d, i));
}
return sum;
}
/************************************************
* method
* checks if polynomial at x=d == 0
***********************************************/
public boolean hasRoot (double d){
return (this.evaluate(d) == 0);
}
}