Polynomials/Inheritance: Difference between revisions
From charlesreid1
(Created page with "=Inheritance= First, let's talk about what inheritance is. In Java, all code is contained in a class or multiple classes. These classes are essentially collections of relat...") |
No edit summary |
||
| Line 8: | Line 8: | ||
This allows us to utilize behaviors already defined by the polynomial (for example, getting the degree, or evaluating the polynomial at a particular value of x), but also define new behaviors (such as solving the quadratic equation to find roots). | This allows us to utilize behaviors already defined by the polynomial (for example, getting the degree, or evaluating the polynomial at a particular value of x), but also define new behaviors (such as solving the quadratic equation to find roots). | ||
=Superclass: Polynomial Class= | |||
To begin, we can define a Polynomial class that implements a few behaviors that we want all polynomials to have: the ability to evaluate the polynomial at a particular value of x, and the ability to get the degree of the polynomial. | |||
<pre> | |||
import java.util.Arrays; | |||
public class SimplePolynomial { | |||
private double[] coeffs; | |||
/** Constructor: assemble a polynomial from its coefficients. */ | |||
public SimplePolynomial(double[] coeffs) { | |||
this.coeffs = Arrays.copyOf(coeffs,coeffs.length); | |||
} | |||
/** Constructor: assemble a polynomial from another polynomial. */ | |||
public SimplePolynomial(SimplePolynomial p) { | |||
this(p.coeffs); | |||
} | |||
/** Eval the polynomial using an efficient method: | |||
* p(x) = c[0] + x*(c[1] + x*(c[2] + x*(... | |||
*/ | |||
public double eval(double x) { | |||
int n = coeffs.length-1; | |||
double result = coeffs[n]; | |||
for(int j=n-1; j>=0; j--) { | |||
result = result*x + coeffs[j]; | |||
} | |||
return result; | |||
} | |||
} | |||
</pre> | |||
=Subclass: Quadratic Class= | |||
[[Category:CS]] | |||
[[Category:Polynomials]] | |||
[[Category:Math]] | |||
[[Category:Java]] | |||
Revision as of 20:53, 9 April 2017
Inheritance
First, let's talk about what inheritance is.
In Java, all code is contained in a class or multiple classes. These classes are essentially collections of related data (fields), and behaviors that utilize that data (methods). Classes can encapsulate and abstract away complexity, so they're very useful. But managing complexity and eliminating redundancy also requires the ability to share code between classes. This is the idea behind inheritance - defining an object that implements the same fields and the same methods as another class, but with a few modifications.
The inheritance relationship is an "is-a" relationship. To ask if B should inherit from A, ask if B is-an A. For example, suppose we have implemented a class representing Polynomials, and we want to implement a new class that represents quadratics. In deciding whether the Quadratic class should inherit from the Polynomial class, we ask if a Quadratic is a Polynomial, and the answer is yes. So the Quadratic class should inherit from the Polynomial class.
This allows us to utilize behaviors already defined by the polynomial (for example, getting the degree, or evaluating the polynomial at a particular value of x), but also define new behaviors (such as solving the quadratic equation to find roots).
Superclass: Polynomial Class
To begin, we can define a Polynomial class that implements a few behaviors that we want all polynomials to have: the ability to evaluate the polynomial at a particular value of x, and the ability to get the degree of the polynomial.
import java.util.Arrays;
public class SimplePolynomial {
private double[] coeffs;
/** Constructor: assemble a polynomial from its coefficients. */
public SimplePolynomial(double[] coeffs) {
this.coeffs = Arrays.copyOf(coeffs,coeffs.length);
}
/** Constructor: assemble a polynomial from another polynomial. */
public SimplePolynomial(SimplePolynomial p) {
this(p.coeffs);
}
/** Eval the polynomial using an efficient method:
* p(x) = c[0] + x*(c[1] + x*(c[2] + x*(...
*/
public double eval(double x) {
int n = coeffs.length-1;
double result = coeffs[n];
for(int j=n-1; j>=0; j--) {
result = result*x + coeffs[j];
}
return result;
}
}