-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolynomial.java
89 lines (80 loc) · 3.51 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
// Liz Crittenden
// This program is a template class for program MainApp, used to perform calculations on a quadratic expression
import java.text.*;
class Polynomial{
DecimalFormat df = new DecimalFormat("0.000");
private double numA; // Three private data members
private double numB;
private double numC;
public Polynomial(){ // Constructor 1
numA = 1;
numB = 1;
numC = 1;
}
public Polynomial(double numX, double numY, double numZ){ // Constructor 2
numA = numX;
numB = numY;
numC = numZ;
}
public double getNumA(){ // Accessor methods
return numA;
}
public double getNumB(){
return numB;
}
public double getNumC(){
return numC;
}
public void setNumA(double numX){ // Modifier methods
numA = numX;
}
public void setNumB(double numY){
numB = numY;
}
public void setNumC(double numZ){
numC = numZ;
}
public String toString(){ // toString() method
return df.format(numA) + "x^2 + " + df.format(numB) + "x + " + df.format(numC);
}
public double findDisc(){ // Instance method that finds the discriminant of the quadratic formula
double letterB = Math.pow(numB,2); // Performs exponential function first per order of operations
double discriminant1 = 4 * numA * numC; // Performs the multiplication next per order of operations
double discriminant2 = letterB - discriminant1; // Final value of discriminant
return discriminant2;
}
public double findPos(){ // Instance method that finds the positive solution to the quadratic formula
double numerator = -numB + Math.sqrt(findDisc()); // First value of numerator
double denominator = 2 * numA; // Gives value of denominator
double finalAnswer1 = numerator / denominator; // Positive solution to formula
return finalAnswer1;
}
public double findNeg(){ // Instance method that finds the negative solution to the quadratic formula
double numerator2 = -numB - Math.sqrt(findDisc()); // Second value of numerator
double denominator = 2 * numA; // Gives value of denominator
double finalAnswer2 = numerator2 / denominator; // Negative solution to formula
return finalAnswer2;
}
public Polynomial add(Polynomial poly1){ // Instance method that adds two polynomials together
double numA, numB, numC, numX, numY, numZ;
numA = this.getNumA(); // Gets coefficients from polynomial doing the calling
numB = this.getNumB();
numC = this.getNumC();
numX = poly1.getNumA(); // Gets coefficients from passed in polynomial
numY = poly1.getNumB();
numZ = poly1.getNumC();
Polynomial sum = new Polynomial(numA + numX, numB + numY, numC + numZ); // Adds coefficients together
return sum; // to find the sum
}
public double enterX(double numX){ // Instance method that solves the expression for a given value of "x"
double numX2, answer, term1, term2;
numA = this.getNumA(); // Gets coefficient from polynomial doing the calling
numB = this.getNumB();
numC = this.getNumC();
numX2 = numX * numX; // Performing mathematical calculations to sole the expression
term1 = numA * numX2;
term2 = numB * numX;
answer = term1 + term2 + numC;
return answer;
}
}