Hey everyone. I'm working on a graphing program for my class that is supposed to graph a polynomial based on user input. What I'm having trouble with is calling my constructor class into my test class correctly. I just know I'm not doing it correctly, but I'm stuck and need some help. If it helps, here's everything that my programs supposed to do
Here's part of my main classQuote:
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
I know the getGraphData method is all wrong right now so don't mind that. But since the created data type "Polynomial" is one of the parameters I need to know how to invoke it correctly.Code:import algoritharium.*;
import java.util.Scanner;
import java.awt.Color;
public class Polygrapher
{
public static void main(String[] args)
{
new ImageViewer();
}
double aRange, bRange, x;
int function;
double[] c = new double[4];
public void createGraph(int w, int h)
{
Image img = ImageViewer.getImage();
Color[][] c = new Color[h][w];
for(int ci=0;ci<w;ci++)
{
for(int cj=0;cj<h;cj++)
{
int si=ci*img.getWidth()/w;
int sj=cj*img.getHeight()/h;
Color color = img.getPixelColor(si,sj);
c[cj][ci]=color;
}
}
ImageViewer.createImage(c);
}
public void plot()
{
createGraph(800,600);
Scanner input = new Scanner(System.in);
System.out.print("Coefficient for x^3: ");
c[0] = input.nextInt();
System.out.print("Coefficient for x^2: ");
c[1] = input.nextInt();
System.out.print("Coefficient for x: ");
c[2] = input.nextInt();
System.out.print("Constant: ");
c[3] = input.nextInt();
function = (int)(c[0]*(x*x*x) + c[1]*(x*x) + c[2]*x+c[3]);
//Polynomial[] p = new Polynomial[function]; ?????
System.out.print("Starting x: ");
aRange = input.nextInt();
System.out.print("Ending x: ");
bRange = input.nextInt();
getGraphData(p[0],aRange,bRange,np);
graph(p, aRange, Color.GREEN);
}
public double[] getGraphData(Polynomial p, double a, double b, int nPoints)
{
double max;
double min;
a=aRange;
b=bRange;
double graphHeight=600;
double sf=graphHeight/(max-min);
//nPoints=(int)((max-min)*sf); ????????
np=nPoints;
}
and here is my constructor class:
Code:public class Polynomial {
private double[] c;
private int n;
public Polynomial (double [] c){ // constructor
c = new double [c.length];
n = c.length -1;
for (int i =0; i < c.length; i++)
c[i] = c[i];
}
public String toString() {
if (n ==0)
return "" + c[0];
if (n==1)
return c[1] + "x + " + c[0];
String s = c[n] + "x^" + n;
for (int i = n-1; i >= 0; i--) {
if (c[i] == 0)
continue;
else if (c[i] > 0)
s += " + " + (c[i]);
else if (c[i] < 0)
s += " - " + (-c[i]);
if (i == 1)
s += "x";
else if (i > 1)
s += "x^" + i;
}
return s;
}
public double evaluate(double x){
double y=0.0;
int i;
for (i = n; i >= 0; i--)
y = c[i] + x * y;
return y;
}
public Polynomial differentiate(){
double [] deriv;
if (n == 0) {
deriv = new double [1];
deriv [0] = 0;
}
else {
deriv = new double [c.length - 1];
for (int i = 0 ; i < deriv.length; i++)
deriv [i] = (i + 1) * c[i + 1];
}
return new Polynomial(deriv);
}
}
