PolyGrapher - Graphing Polynomials
Develop a Java program that uses the Algoritharium to graph polynomials.
Requirements for the graph
The graph:
- must be drawn with x increasing to the right and y increasing upwards
(10 pts.)
- must ask for user input for the coefficients of an n-degree
polynomial (10 pts.)
- must ask for user input for the range [a, b]
over which the polynomial is to be evaluated (10 pts.)
- must display grid lines every 10 pixels in both the X and Y directions
in a light color on an 800 by 600 image (10 pts.)
- must plot the polynomial in the figure at nearly 800 points (15
pts.)
- must be scaled so as to occupy nearly all the space available with
all features of the graph displayed over the plot range (10 pts.)
- must print to the console the polynomial's toString, range
[a, b] min, max, scale factor (10 pts.)
- must plot the polynomial's derivative function in the same figure
using the original polynomial's scale factor, range, nPoints and minimum value
in a different color (15 pts.)
- must print to the console the derivative's toString (10 pts.)
- may display abscissa and ordinate axes about the origin in black
whenever contained in the function's plot over the given range (5 bonus
pts.)
- may have a 20 pixel border on three sides for text / aesthetics
(2 bonus pts.)
Program Design
Your program consists of two classes:
The PolyGrapher class
This class must contain methods with the following signatures:
- public void plot(): When invoked, this method
- Clears all existing graphs off the screen
- prompts the user for the values of the coefficients for a polynomial
- instantiates a polynomial with the data obtained in the previous step
- prompts the user for the range [a, b] over
which the polynomial is to be evaluated
- invokes getGraphData() with the polynomial, range and number of
points to plot and returns a data array with yMin, yMax and sf.
- invokes graph() with the polynomial, data array with yMin, yMax and
sf, and a color not used for the grid lines or axes.
- public double[] getGraphData(Polynomial p, double a, double b, int nPoints) When invoked, this method determines the values
nPoints, yMin, yMax,
and sf
- Evaluates the polynomial at the appropriate values for x
compute maximum and minimum values of the function on [a, b].
- Computes the scaling factor sf. sf = graphHeight / ( max - min ).
- public void plotAndDifferentiate(): When invoked, this
method
- invokes plot()
- invokes the current polynomial's differentiate method to
get a new polynomial, the derivative of the current polynomial
- invokes graph() with the new polynomial, same data,
and a new color.
- private double[] graph(Polynomial p, double[] data, Color c): When
invoked, this method displays the line graph of the polynomial in an
800x600 image plotted from a through b on nPoints using the color c.
- Evaluates the polynomial at the appropriate values for x
- Displays the line graph of the polynomial in an 800x600 image.
- Returns the scale factor and minimum value of the plot.
- Other methods as desired to make your code more readable/useable.
The Polynomial class
This class models a polynomial and must contain methods with the following signatures:
- A constructor: public Polynomial(double [] coefficients)
- public double evaluate(double x) - returns the value of the polynomial at the specified value for x.
- public Polynomial differentiate() - returns a
new polynomial, the derivative of this polynomial.
- public String toString() - returns a string
representation of this polynomial
Sample output:
2.0x^3 + 1.5x^2
a = -10.0, b = 10.0, n = 760, dx = 0.02631578947368421, min = -1850.0, max =
2150.0, sf = 0.14975.
6.0x^2 + 3.0x
derivative's a = -10.0, b = 10.0, n = 760, dx = 0.02631578947368421, derivMin =
-0.3739612188365653, sf = 0.14975.

Extra Credit:
2.0x^5 + 1.5x^4 - 50.0x
a = -2.0, b = 2.0, n = 760, dx = 0.005263157894736842, min = -53.56482705060258,
max = 69.18823017586938, sf = 4.724933236733445.
10.0x^4 + 6.0x^3 - 50.0
derivative's a = -2.0, b = 2.0, n = 760, dx = 0.005263157894736842, derivMin =
-50.13665967111977, sf = 4.724933236733445.
