From charlesreid1

No edit summary
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 9: Line 9:
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=
=The Java Classes=
 
We can define a simple polynomial class that implements
 
==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.
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.
Line 17: Line 21:


public class SimplePolynomial {
public class SimplePolynomial {
private double[] coeffs;
protected double[] coeffs;


/** Constructor: assemble a polynomial from its coefficients. */
public SimplePolynomial(double[] coeffs) {
public SimplePolynomial(double[] coeffs) {
this.coeffs = Arrays.copyOf(coeffs,coeffs.length);
this.coeffs = Arrays.copyOf(coeffs,coeffs.length);
}
}


/** Constructor: assemble a polynomial from another polynomial. */
public SimplePolynomial(SimplePolynomial p) {
public SimplePolynomial(SimplePolynomial p) {
this(p.coeffs);
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) {  
public double eval(double x) {  
int n = coeffs.length-1;
int n = coeffs.length-1;
Line 41: Line 40:
}
}


public double degree() {
return this.coeffs.length;
}
}
</pre>
A few things to note. First, the coefficients array is protected. This is because any child class, or class that derives from the Polynomial class, will need to be able to access the coefficients of the polynomial to do useful things. Another thing to note is that the evaluation method for this polynomial is an efficient and fast implementation of polynomial evaluation that uses a nested representation of a polynomial and a cumulative algorithm to calculate the result.
==Subclass: Quadratic Class==
To create a quadratic class that implements the same methods (degree and evaluate) and constructors, we first specify that the SimpleQuadratic class extends the SimplePolynomial class:
<pre>
public class SimpleQuadratic extends SimplePolynomial {
</pre>
There are a few other things that the Quadratic class needs to do that the Polynomial class does not:
* Modify the constructor to check to make sure there are 3 coefficients, and that the leading coefficient is nonzero
* Implement a method to compute the roots of the quadratic using the quadratic formula
Here is a class that will do that:
<pre>
import java.util.Arrays;
public class SimpleQuadratic extends SimplePolynomial {
public SimpleQuadratic(double a, double b, double c) {
super(new double[] {a,b,c});
}
public double[] getRoots() {
// Use the quadratic formula
double x1, x2;
double a = this.coeffs[2];
double b = this.coeffs[1];
double c = this.coeffs[0];
double discriminant = b*b - 4*a*c;
if(discriminant>=0) {
x1 = (-b + Math.sqrt(discriminant))/(2*a);
x2 = (-b - Math.sqrt(discriminant))/(2*a);
return new double[] {x1,x2};
} else {
// Raising an exception would be better,
// but will make code more cluttered.
return new double[2];
}
}
}
}
</pre>
</pre>


=Subclass: Quadratic Class=
There are a few things to note about this solution. The first is that the SimpleQuadratic class defines a new constructor that takes 3 arguments. Note that we should probably do a better job of making sure the user doesn't do something crazy like creating a new Quadratic from an existing 8th-degree polynomial, but that would require us  to introduce some exceptions and litter our code with exception handling, so let's ignore that distraction for now.


The code implements one new constructor taking three arguments, a, b, and c, and populates an array with these coefficients and passes it to the super class constructor. It also implements one new method to compute the two roots of the polynomial. It checks to make sure the discriminant is non-negative. If the discriminant is negative, it will return an empty array. Note that it would be better to raise an exception, and catch the exception where we actually call the getRoots() method, but again, we don't want to clutter our code with exception-handling.


=Flags/Categories=


{{PolynomialsFlag}}


[[Category:CS]]
[[Category:CS]]
[[Category:Polynomials]]
[[Category:Math]]
[[Category:Java]]
[[Category:Java]]

Latest revision as of 21:49, 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).

The Java Classes

We can define a simple polynomial class that implements

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 {
	protected double[] coeffs;

	public SimplePolynomial(double[] coeffs) {
		this.coeffs = Arrays.copyOf(coeffs,coeffs.length);
	}

	public SimplePolynomial(SimplePolynomial p) {
		this(p.coeffs);
	}

	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;
	}

	public double degree() { 
		return this.coeffs.length;
	}
}

A few things to note. First, the coefficients array is protected. This is because any child class, or class that derives from the Polynomial class, will need to be able to access the coefficients of the polynomial to do useful things. Another thing to note is that the evaluation method for this polynomial is an efficient and fast implementation of polynomial evaluation that uses a nested representation of a polynomial and a cumulative algorithm to calculate the result.

Subclass: Quadratic Class

To create a quadratic class that implements the same methods (degree and evaluate) and constructors, we first specify that the SimpleQuadratic class extends the SimplePolynomial class:

public class SimpleQuadratic extends SimplePolynomial {

There are a few other things that the Quadratic class needs to do that the Polynomial class does not:

  • Modify the constructor to check to make sure there are 3 coefficients, and that the leading coefficient is nonzero
  • Implement a method to compute the roots of the quadratic using the quadratic formula

Here is a class that will do that:

import java.util.Arrays;

public class SimpleQuadratic extends SimplePolynomial {

	public SimpleQuadratic(double a, double b, double c) {
		super(new double[] {a,b,c});
	}

	public double[] getRoots() {
		// Use the quadratic formula
		double x1, x2;

		double a = this.coeffs[2];
		double b = this.coeffs[1];
		double c = this.coeffs[0];

		double discriminant = b*b - 4*a*c;

		if(discriminant>=0) {
			x1 = (-b + Math.sqrt(discriminant))/(2*a);
			x2 = (-b - Math.sqrt(discriminant))/(2*a);
			return new double[] {x1,x2};
		} else {
			// Raising an exception would be better,
			// but will make code more cluttered.
			return new double[2];
		}

	}
}

There are a few things to note about this solution. The first is that the SimpleQuadratic class defines a new constructor that takes 3 arguments. Note that we should probably do a better job of making sure the user doesn't do something crazy like creating a new Quadratic from an existing 8th-degree polynomial, but that would require us to introduce some exceptions and litter our code with exception handling, so let's ignore that distraction for now.

The code implements one new constructor taking three arguments, a, b, and c, and populates an array with these coefficients and passes it to the super class constructor. It also implements one new method to compute the two roots of the polynomial. It checks to make sure the discriminant is non-negative. If the discriminant is negative, it will return an empty array. Note that it would be better to raise an exception, and catch the exception where we actually call the getRoots() method, but again, we don't want to clutter our code with exception-handling.

Flags/Categories