Java Newbie needs help writing getting polynomial coordinates to graph.....using java

Posted on 2006-11-19
Last Modified: 2012-06-21
I have written a program that askes a user for 4 coefficients,the number of points to graph,the starting point of x , and the interval that they're incremented by. Then the program finds f(x) from x for a given number of points. The last and final thing I am trying to get this program to do is to plot the points on a graph and connect them using straight lines. Most of the program is written so this shouldn't be too difficult except for me. The graph can assume that all coefficients passed to it are possitive, and only has an x value given to it between 0 and 5. Any help would be greatly appreciated. I will give you the code for the class and main method below.

import java.util.Scanner;
import java.text.DecimalFormat;

public class Poly
      public static void main (String[] args)
double c4,c3,c2,c1,c0,x,interval,xvalue;
int ptnumber,counter;
String result;

DecimalFormat fmt = new DecimalFormat ("0.##");

Scanner scan = new Scanner (;

System.out.print ("Enter the coefficient of x^4(0.0 if none:");
c4 = scan.nextDouble();

System.out.print ("Enter the coefficient of x^3(0.0 if none:");
c3 = scan.nextDouble();

System.out.print ("Enter the coefficient of x^2(0.0 if none:");
c2 = scan.nextDouble();

System.out.print ("Enter the coefficient of x^1(0.0 if none:");
c1 = scan.nextDouble();

System.out.print ("Enter the constant:");
c0 = scan.nextDouble();

System.out.print ("Enter the starting value of x:");
x = scan.nextDouble();

System.out.print ("Enter the number of points to be graphed(Integer):");
ptnumber = scan.nextInt();

System.out.print ("Enter the interval between points:");
interval = scan.nextDouble();

Polynomial pn = new Polynomial(c4, c3, c2, c1, c0);


System.out.println("x      | f(x)");
counter = 0;
while (counter < ptnumber)

System.out.println(x            +"      | "+fmt.format(pn.evaluate(x)));

x = (x + interval);



import javax.swing.JPanel;
import java.awt.*;
import java.text.DecimalFormat;

import java.text.DecimalFormat;
public class Polynomial
   double coeff4, coeff3, coeff2, coeff1, constant;
      String sign4,sign3,sign2,sign1,sign0;
   public Polynomial(double c4, double c3, double c2, double c1, double c0)
            coeff4 = c4;
            coeff3 = c3;
            coeff2 = c2;
            coeff1 = c1;
            constant = c0;
      public double evaluate(double x)
            double xsq = x*x;
          return (coeff4*xsq*xsq + coeff3*xsq*x + coeff2*xsq + coeff1*x + constant);
      public String toString()

        String result = "";

           if (coeff4 != 0)
               result += coeff4+"x^4";
          if (coeff3 != 0)
            if (coeff3 > 0)
                result += "+";
               result += coeff3+"x^3";
          if (coeff2 != 0)
            if (coeff2 > 0)
                result += "+";
                result += coeff2+"x^2";
           if (coeff1 != 0)
            if (coeff1 > 0)
                result += "+";
               result += coeff1+"x";

         result += constant;

          return result;

  public void draw(Graphics page, int xPixels, int yPixels)
    final int NUM = 11;
    final double INTERVAL = 0.5;
    final double XSTART = 0.0;

    final int XMARGIN = 70;
    final int XUSABLEPIXELS = xPixels - 2*XMARGIN;
    final int YMARGIN =40;
    final int YUSABLEPIXELS = yPixels - 2*YMARGIN;
        // Label the graph
    page.drawString("Graph of f(x) = " + toString(), XMARGIN + 20, YMARGIN);

    // Draw the x axis and put in hash marks
    page.drawLine(XMARGIN, yPixels - YMARGIN, xPixels - XMARGIN, yPixels - YMARGIN);
    for ( int i = 0; i < NUM; i++ )
        page.drawLine(XMARGIN + i * XINTERVALPIXELS, yPixels - YMARGIN, XMARGIN + i * XINTERVALPIXELS, yPixels - YMARGIN + 5);
       DecimalFormat twoPlaces = new DecimalFormat("0.00");

    // Label the x axis
    for (int i = 0; i < NUM; i++)
        page.drawString( twoPlaces.format(XSTART + i * INTERVAL), 3 * XMARGIN / 4 + i * XINTERVALPIXELS, yPixels - YMARGIN/2);

    // Draw the y axis
    page.drawLine(XMARGIN, YMARGIN, XMARGIN, yPixels - YMARGIN);

    // Label the y axis and put in hash marks
    final double YMIN = 0.0;
    final double YINTERVAL = 100.0;

    for (int i = 0; i < NUM; i++)
        page.drawString(twoPlaces.format(YMIN + i * YINTERVAL), XMARGIN / 4, yPixels - i * YINTERVALPIXELS - YMARGIN);
        page.drawLine(XMARGIN, yPixels - YMARGIN - i * YINTERVALPIXELS, XMARGIN - 5, yPixels - YMARGIN - i * YINTERVALPIXELS);


Question by:lusius188
1 Comment

Accepted Solution

saintsairforce earned 500 total points
ID: 17980628
I just ansewered a simular question. This should help you out a lot


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now