DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Help on making a Graph for applet

1. Registered User
Join Date
May 2006
Posts
28

## 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

2. Senior Member
Join Date
Dec 2004
Location
San Bernardino County, California
Posts
1,468
Here is a link to a discussion and reference to what appears to be an API. Perhaps you can use the API, or learn enough to discern what functionality you need to supply to your applet.

http://www.informit.com/guides/conte...seqNum=74&rl=1

3. Registered User
Join Date
Mar 2006
Posts
35
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=c...ent&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.

4. Registered User
Join Date
May 2006
Posts
28
thanks blastmaster, ive actually been looking around the net for some time looking for a good, simply and easy graph...i'm yet to find one! And thanks for your examples too, but i already have a simpler quadratic equation solver, but thanks non the less!

-Crawf

5. Registered User
Join Date
May 2006
Posts
28
hmmmm...i still am having trouble...ive got the coordinates, like the root zeros and the y intercept...but i have no idea how to graph this...does anyone know of a SIMPLE example of a graph? Yes i know you will all suggest Google, but i'm searching everywhere...i just cant seem to find what i'm looking for!

Any help will be really appreciated! thanks!

-Crawf

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source