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 get some assistance on making it...
Now of course, i've had a go at it, and i've made an applet to display the z intercepts, y intercept, and vertex (turning point). This is determined by the user inputs of the three coefficients a, b, and c.
First question, is this all the information i will need?
Secondly, i am struggling to figure out how to put this information into a graph and make it a parabola...
Thank you very much for your help!
Ps. my applet is below if you wish to have a look, i've made a crude graph if that helps as well...

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Graph2 extends Applet implements ActionListener {
    TextField aInput, bInput, cInput;
    double a, b, c;
    boolean notFirst;
    Button calcButton;
    int size = 400;
    public void init() {
        setLayout(null);
        resize(600, 500);
        Label labelA = new Label("Value a:");
        labelA.setBounds(412, 40, 50, 20);
        Label labelB = new Label("Value b:");
        labelB.setBounds(412, 70, 50, 20);
        Label labelC = new Label("Value c:");
        labelC.setBounds(412, 100, 50, 20);
        aInput = new TextField("2", 5);
        aInput.setBounds(470, 40, 80, 20);
        bInput = new TextField("5", 5);
        bInput.setBounds(470, 70, 80, 20);
        cInput = new TextField("-12", 5);
        cInput.setBounds(470, 100, 80, 20);
        calcButton = new Button("Solve");
        calcButton.setBounds(460, 130, 45, 20);
        add(labelA);
        add(aInput);
        add(labelB);
        add(bInput);
        add(labelC);
        add(cInput);
        add(calcButton);
        aInput.addActionListener(this);
        bInput.addActionListener(this);
        cInput.addActionListener(this);
        calcButton.addActionListener(this);
        notFirst = false;
    }
    public void makeGraph(Graphics g) {
        g.setColor(Color.lightGray);
        // Draw grid
        for (int y = 0; y <= size; y = y + 10) {
            g.drawLine(1, y, size, y);
        }
        for (int x = 0; x <= size; x = x + 10) {
            g.drawLine(x, 1, x, size);
        }
        g.setColor(Color.red);
        // Draw y axis
        g.drawLine(size / 2, 0, size / 2, size);
        for (int i = 0; i <= size; i = i + 20) {
            g.drawLine(size / 2 - 5, i, size / 2 + 5, i);
        }
        g.setColor(Color.blue);
        // Draw x axis
        g.drawLine(0, size / 2, size, size / 2);
        for (int j = 0; j <= size; j = j + 20) {
            g.drawLine(j, size / 2 - 5, j, size / 2 + 5);
        }
        g.setColor(Color.black);
        // Draw positive x axis numbers
        for (int n = 0; n <= 5; n++) {
            g.drawString("" + n * 2, (size / 2 - 4) + n * 40, size / 2 + 17);
        }
        // Draw negative x axis numbers
        for (int n = 1; n <= 5; n++) {
            g.drawString("-" + n * 2, (size / 2 - 6) - n * 40, size / 2 + 17);
        }
        // Draw positive y axis numbers
        for (int n = 1; n <= 5; n++) {
            g.drawString("" + n * 2, size / 2 - 21, (size / 2 + 5) - n * 40);
        }
        // Draw negative y axis numbers
        for (int n = 1; n <= 5; n++) {
            g.drawString("-" + n * 2, size / 2 - 23, (size / 2 + 6) + n * 40);
        }
    }
    public void paint(Graphics g) {
        int yValue = 180;
        makeGraph(g);
        g.drawString("y = ax\u00B2 + bx + c", 420, 20);
        if (notFirst) {
            double zero = ((( -b) + (Math.sqrt((b * b) - (4 * (a * c))))) / (2 * a));
            double zero2 = ((( -b) - (Math.sqrt((b * b) - (4 * (a * c))))) / (2 * a));
            g.drawString("a = " + a, 420, yValue);
            g.drawString("b = " + b, 420, yValue + 20);
            g.drawString("c = " + c, 420, yValue + 40);
            if (a == 0) {
                g.drawString("a Cannot Be 0", 420, yValue + 80);
            } else {
                if (((b * b) - (4 * a * c)) < 0) {
                    g.drawString("No Real Solution", 420, yValue + 80);
                } else {
                    g.drawString("Zero = (" + zero + ", 0)", 420, yValue + 80);
                    g.drawString("Zero = (" + zero2 + ", 0)", 420, yValue + 100);
                    g.drawString("Y Intercept = (0, " + c + ")", 420, yValue + 120);
                    double turnX = -b / (2 * a);
                    double turnY = c - (Math.pow(b, 2)) / (4 * a);
                    g.drawString("Vertex = (" + turnX + ", " + turnY + ")", 420, yValue + 140);
                    double d = (Math.pow(b, 2)) - 4 * a * c;
                    g.drawString("Discriminant = " + d, 420, yValue + 160);
                    if (a < 0) {
                        g.drawString("Parabola = Down", 420, yValue + 180);
                    } else {
                        g.drawString("Parabola Direction = Up", 420, yValue + 180);
                    }
                }
            }
        }
    }
    public void actionPerformed(ActionEvent e) {
        notFirst = true;
        if (e.getSource() == calcButton) {
            a = Double.parseDouble(aInput.getText());
            b = Double.parseDouble(bInput.getText());
            c = Double.parseDouble(cInput.getText());
            if (aInput.getText().equals("")) {
                a = 0;
            }
            repaint();
        }
    }
}

-Crawf

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

Couple of things...

There is a package called swingx which may be of interest to you. However, like all the java api it requires a lot of reading to get the basic gist of how it works.

First question, is this all the information i will need?

What to map a quadratic equation? It depends really how advanced do you want it? Will your equations always be in the form:-

[tex]ax^2 + bx + c[/tex]

or could they possibly be in the form:-

[tex] (x + 7 ) ( x - 3 ) [/tex]

whereby after expanding and simplifying yields another quadratic equation?

If you are handing the second example as well your current program is insufficient. My guess is you would have to write an equation parser or something similar to handle those cases.

Secondly, i am struggling to figure out how to put this information into a graph and make it a parabola...

Remember you should be obtaining coordinates, once you have those coordinates you just join them up. The smaller the distance between the coordinates the more accurate your graph will be.

the input will always be in the form ax^2+bx+c, its only meant to be simple...

As for obtaining the coordinates, just how should i go about this? and which coordinates will i need to find?

Sorry if i'm asking too much!

Thanks for the reply too!

-Crawf

Member Avatar for iamthwee

the input will always be in the form ax^2+bx+c, its only meant to be simple...

As for obtaining the coordinates, just how should i go about this? and which coordinates will i need to find?

Sorry if i'm asking too much!

Thanks for the reply too!

-Crawf

It's pretty simple. Think about.

[tex] y = ax^2 +bx +c [/tex]

So, let's assume we have an arbitary function, where x does not belong to the set of complex numbers...

[tex]y=x^2 +x-2[/tex]

Now we choose a range for our x values. Say -10 to 10. And we substitute these values into out function to obtain the y value.

Thus:-

[tex](-10)^2 +(-10)-2 =88[/tex] x = -10 , y = 88
[tex](-9)^2 + (-9)-2 =70[/tex] x = -9, y = 70

And so on. Plotting the coordinates. Simple as pie

aha! i get you!! it really is simple!

thats awesome! thanks very much for that! you've been very helpful! :)

You are a champion!

-Crawf

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.