Help on making a Graph for applet
Hi!
Im making an applet to graph a quadratic function...now im not going to be rude and say "tell me how to do it!" but i was just wondering if i could ask any of you friendly people if you have any information, links or tutorials on making graphs...i've got the formula alright, i just need to put the x values into a graph!
So, if anyone has any ideas or suggestions that could help, i would really appreciate it!
-Crawf
[454 byte] By [
crawf] at [2007-11-11 8:49:50]

# 2 Re: Help on making a Graph for applet
Here are two examples I found on the net for solving the quadratic blah blah
Not sure if they work havent tried them
example 1
strictfp class QuadSolv
{
public static void main(String[] args)
{
double a, b, c, discr, root1, root2;
// Apllying the quadratic formula
// Obtain sides from user
System.out.println("Applying the quadratic formula");
a = 1d;
b = 2d;
c = 3d;
// Solve the discriminant (SQRT (b^2 - 4ac)
discr = Math.sqrt((b * b) - (4 * a * c));
System.out.println("Discriminant = " + discr);
// Determine number of roots
// if discr > 0 equation has 2 real roots
// if discr == 0 equation has a repeated real root
// if discr < 0 equation has imaginary roots
// if discr is NaN equation has no roots
// Test for NaN
if(Double.isNaN(discr))
System.out.println("Equation has no roots");
if(discr > 0)
{
System.out.println("Equation has 2 roots");
root1 = (-b + discr)/2 * a;
root2 = (-b - discr)/2 * a;
System.out.println("First root = " + root1);
System.out.println("Second roor = " + root2);
}
if(discr == 0)
{
System.out.println("Equation has 1 root");
root1 = (-b + discr)/2 * a;
System.out.println("Root = " + root1);
}
if(discr < 0)
System.out.println("Equation has imaginary roots");
}
}
example 2
import java.lang.Math;
import java.util.*;
import java.io.*;
class Quadratic {
public static void main( String args[] ) {
float fA,fB,fC;
float fRoot1, fRoot2;
float fDiscriminant;
String sLine;
// Print welcome message.
System.out.println("Welcome to The Quadratic Equation Solver");
System.out.println("------------");
// Prompt user for coefficients in quadratic equation.
System.out.println("Please Enter coefficients for equation");
System.out.println("a.x^2 + b.x + c");
System.out.println("Coefficent a : ");
sLine = keyboardInput();
fA = Float.valueOf(sLine).floatValue();
System.out.println("Coefficent b : ");
sLine = keyboardInput();
fB = Float.valueOf(sLine).floatValue();
System.out.println("Coefficent c : ");
sLine = keyboardInput();
fC = Float.valueOf(sLine).floatValue();
// Print details of quadratic equation to screen.
System.out.println("The equation you have entered is : ");
System.out.println(+fA+".x^2 + "+fB+".x + "+fC);
// Check for degenerate roots (i.e., fA = fB = zero).
if ( fA==0 && fB==0 ) {
System.out.println("Cannot solve " + fC +" = 0.0");
return;
}
if ( fA==0 && fB !=0 ) {
fRoot1 = -fC/fB;
System.out.println("Degenerate root : Root = "+ fRoot1);
return;
}
// Compute discriminant of quadratic equation.
fDiscriminant = fdiscriminant(fA,fB,fC);
// Case for two real roots.
if ( fDiscriminant >= 0.0 ) {
fRoot1 = (float)(-fB/2.0/fA-(float)Math.sqrt(fDiscriminant) /
2.0 / fA );
fRoot2 = (float)(-fB/2.0/fA+(float)Math.sqrt(fDiscriminant) /
2.0 / fA);
System.out.println("Two real roots : Root1 : " + fRoot1);
System.out.println(" Root2 : " + fRoot2);
return;
}
// Two complex roots
fRoot1 = (float) (-fB/2.0/fA);
fRoot2 = (float) (Math.sqrt(-fDiscriminant)/2.0/fA);
System.out.println("Two complex roots");
System.out.println("Root1 : " + fRoot1 + "+" + fRoot2 + "i");
System.out.println("Root2 : " + fRoot1 + "-" + fRoot2 + "i");
}
/*
* =============================================================
* Method fdiscriminant() : compute discriminant of quadratic
*
* Input : fA, fB, and fC -- coefficients in quadratic equation
* Output : float fReturn -- discriminant of quadratic equation
* =============================================================
*/
static float fdiscriminant(float fA, float fB, float fC) {
float fReturn;
fReturn= (float)(fB*fB-4.0*fA*fC);
return fReturn;
}
/*
* ===========================================================
* Method keyboardInput() : Get line of input from keyboard
*
* Input : None.
* Output : String sLine -- character string of keyboard input
* ===========================================================
*/
static String keyboardInput() {
String sLine;
DataInputStream in = new DataInputStream(System.in);
try{
sLine = in.readLine();
return sLine;
}
catch (Exception e){
return "error";
}
}
}
And here's an applet that does quadratic graphing with source code avialable
http://mathdl.maa.org/mathDL/3/?pa=content&sa=viewDocument&nodeId=379
It wasn't hard to find on google you must not have made much of an effort but this is more for other peoples benifit.