Applet - Graphing Quadratic Function Help

Reply

Join Date: Jun 2006
Posts: 16
Reputation: crawf is an unknown quantity at this point 
Solved Threads: 0
crawf crawf is offline Offline
Newbie Poster

Applet - Graphing Quadratic Function Help

 
0
  #1
Jun 8th, 2006
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...
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class Graph2 extends Applet implements ActionListener {
  5. TextField aInput, bInput, cInput;
  6. double a, b, c;
  7. boolean notFirst;
  8. Button calcButton;
  9. int size = 400;
  10. public void init() {
  11. setLayout(null);
  12. resize(600, 500);
  13. Label labelA = new Label("Value a:");
  14. labelA.setBounds(412, 40, 50, 20);
  15. Label labelB = new Label("Value b:");
  16. labelB.setBounds(412, 70, 50, 20);
  17. Label labelC = new Label("Value c:");
  18. labelC.setBounds(412, 100, 50, 20);
  19. aInput = new TextField("2", 5);
  20. aInput.setBounds(470, 40, 80, 20);
  21. bInput = new TextField("5", 5);
  22. bInput.setBounds(470, 70, 80, 20);
  23. cInput = new TextField("-12", 5);
  24. cInput.setBounds(470, 100, 80, 20);
  25. calcButton = new Button("Solve");
  26. calcButton.setBounds(460, 130, 45, 20);
  27. add(labelA);
  28. add(aInput);
  29. add(labelB);
  30. add(bInput);
  31. add(labelC);
  32. add(cInput);
  33. add(calcButton);
  34. aInput.addActionListener(this);
  35. bInput.addActionListener(this);
  36. cInput.addActionListener(this);
  37. calcButton.addActionListener(this);
  38. notFirst = false;
  39. }
  40. public void makeGraph(Graphics g) {
  41. g.setColor(Color.lightGray);
  42. // Draw grid
  43. for (int y = 0; y <= size; y = y + 10) {
  44. g.drawLine(1, y, size, y);
  45. }
  46. for (int x = 0; x <= size; x = x + 10) {
  47. g.drawLine(x, 1, x, size);
  48. }
  49. g.setColor(Color.red);
  50. // Draw y axis
  51. g.drawLine(size / 2, 0, size / 2, size);
  52. for (int i = 0; i <= size; i = i + 20) {
  53. g.drawLine(size / 2 - 5, i, size / 2 + 5, i);
  54. }
  55. g.setColor(Color.blue);
  56. // Draw x axis
  57. g.drawLine(0, size / 2, size, size / 2);
  58. for (int j = 0; j <= size; j = j + 20) {
  59. g.drawLine(j, size / 2 - 5, j, size / 2 + 5);
  60. }
  61. g.setColor(Color.black);
  62. // Draw positive x axis numbers
  63. for (int n = 0; n <= 5; n++) {
  64. g.drawString("" + n * 2, (size / 2 - 4) + n * 40, size / 2 + 17);
  65. }
  66. // Draw negative x axis numbers
  67. for (int n = 1; n <= 5; n++) {
  68. g.drawString("-" + n * 2, (size / 2 - 6) - n * 40, size / 2 + 17);
  69. }
  70. // Draw positive y axis numbers
  71. for (int n = 1; n <= 5; n++) {
  72. g.drawString("" + n * 2, size / 2 - 21, (size / 2 + 5) - n * 40);
  73. }
  74. // Draw negative y axis numbers
  75. for (int n = 1; n <= 5; n++) {
  76. g.drawString("-" + n * 2, size / 2 - 23, (size / 2 + 6) + n * 40);
  77. }
  78. }
  79. public void paint(Graphics g) {
  80. int yValue = 180;
  81. makeGraph(g);
  82. g.drawString("y = ax\u00B2 + bx + c", 420, 20);
  83. if (notFirst) {
  84. double zero = ((( -b) + (Math.sqrt((b * b) - (4 * (a * c))))) / (2 * a));
  85. double zero2 = ((( -b) - (Math.sqrt((b * b) - (4 * (a * c))))) / (2 * a));
  86. g.drawString("a = " + a, 420, yValue);
  87. g.drawString("b = " + b, 420, yValue + 20);
  88. g.drawString("c = " + c, 420, yValue + 40);
  89. if (a == 0) {
  90. g.drawString("a Cannot Be 0", 420, yValue + 80);
  91. } else {
  92. if (((b * b) - (4 * a * c)) < 0) {
  93. g.drawString("No Real Solution", 420, yValue + 80);
  94. } else {
  95. g.drawString("Zero = (" + zero + ", 0)", 420, yValue + 80);
  96. g.drawString("Zero = (" + zero2 + ", 0)", 420, yValue + 100);
  97. g.drawString("Y Intercept = (0, " + c + ")", 420, yValue + 120);
  98. double turnX = -b / (2 * a);
  99. double turnY = c - (Math.pow(b, 2)) / (4 * a);
  100. g.drawString("Vertex = (" + turnX + ", " + turnY + ")", 420, yValue + 140);
  101. double d = (Math.pow(b, 2)) - 4 * a * c;
  102. g.drawString("Discriminant = " + d, 420, yValue + 160);
  103. if (a < 0) {
  104. g.drawString("Parabola = Down", 420, yValue + 180);
  105. } else {
  106. g.drawString("Parabola Direction = Up", 420, yValue + 180);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. public void actionPerformed(ActionEvent e) {
  113. notFirst = true;
  114. if (e.getSource() == calcButton) {
  115. a = Double.parseDouble(aInput.getText());
  116. b = Double.parseDouble(bInput.getText());
  117. c = Double.parseDouble(cInput.getText());
  118. if (aInput.getText().equals("")) {
  119. a = 0;
  120. }
  121. repaint();
  122. }
  123. }
  124. }

-Crawf
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Applet - Graphing Quadratic Function Help

 
0
  #2
Jun 8th, 2006
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:-

ax^2 + bx + c

or could they possibly be in the form:-

 (x + 7 ) ( x - 3 )

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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 16
Reputation: crawf is an unknown quantity at this point 
Solved Threads: 0
crawf crawf is offline Offline
Newbie Poster

Re: Applet - Graphing Quadratic Function Help

 
0
  #3
Jun 8th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Applet - Graphing Quadratic Function Help

 
0
  #4
Jun 8th, 2006
Originally Posted by crawf
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.

 y = ax^2 +bx +c

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

y=x^2 +x-2

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

(-10)^2 +(-10)-2 =88 x = -10 , y = 88
(-9)^2 + (-9)-2 =70 x = -9, y = 70

And so on. Plotting the coordinates. Simple as pie
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 16
Reputation: crawf is an unknown quantity at this point 
Solved Threads: 0
crawf crawf is offline Offline
Newbie Poster

Re: Applet - Graphing Quadratic Function Help

 
0
  #5
Jun 8th, 2006
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC