-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolynomial.java
96 lines (84 loc) · 2.62 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* @author Rakith (Jay) Jayewardene
* Creates a class named Polynomial that extends Function
*/
public class Polynomial extends Function {
/* Stores the operand */
private Function operand;
/* Stores the power */
private double power;
/**
* A constructor that initializes an input Function named operand and double named power
* @param operand and power
*/
public Polynomial(Function operand,double power) {
this.operand = operand;
this.power = power;
}
/**
* Method that gets the power
* @return power in type double
*/
public double getPower() {
return power;
}
/**
* Method that takes a double named power as input and sets the power
*/
public void setPower(double power) {
this.power = power;
}
/**
* A method that takes no input and gets the operand
* @return the operand
*/
public Function getOperand() {
return operand;
}
/**
* A method that takes a Function named operand and sets the operand
*/
public void setOperand(Function operand) {
this.operand = operand;
}
/**
* A method that takes a double named inputValue as input and returns the value
* @return the inputValue as a double
*/
@Override
public double value(double inputValue) {
return Math.pow(this.getOperand().value(inputValue), this.getPower());
}
@Override
public double value() {
return Math.pow(this.getOperand().value(), this.getPower());
}
/**
* A method that takes no input and takes the derivative of the polynomial function
* @return the derivative of the polynomial function in type Function
*/
@Override
public Function derivative() {
return new BinaryOp(new Number(power), new BinaryOp(new Polynomial(getOperand(), 1.0), getOperand().derivative(),BinaryOp.Op.MULT),BinaryOp.Op.MULT);
}
/**
* Method that converts the polynomial function to a String
* @return String representation of the polynomial function
*/
@Override
public String toString() {
return getOperand() + "^" + power;
}
/* A method that takes an input Object called function and compares it to the polynomial function
* @return true if the object is equal to the polynomial function and returns false if it does not
*/
@Override
public boolean equals(Object function) {
if(function instanceof Polynomial) {
if(this.operand.equals(((Polynomial)(function)).getOperand()) && this.power == ((Polynomial)(function)).getPower()) {
return true;
}
}
return false;
}
}