Tuesday, 31 January 2012

Qudratic equation using method

  1. At the top of code, write " import java.lang.Math.*;" this will import a math class and will be helpful to find mathematical problems.
  2.  And then class and main as we already know and now write starting curly braces of main method.
  3. Declare three variables a,b and c of int type.
  4. Take three values of a,b and c by using easyin method as these values are required for quadratic equation.
  5. Write a caller which pass three values a,b and c and choose an appropriate name of if like quadii(a,b,c);
  6. Now, write the closing curly braces of main method.
  7. Write method which take three values of a,b and c like public static void quadii(int x, int y, int z). Here x y and z are different from a b and c these can be same also but here values of a,b and c have been transferred into x y and z.
  8.  Write starting curly braces of method quadii.
  9. declare some variables lets say d e f g
  10. And four double type o u v
  11. d=y*y;
  12. e=4*x*z;
  13. f=d-e;
  14. g=2*x;
  15. o=(double)Math.sqrt(f); ( this will find the square root of f and double will convert the value of f into double )
  16. u=(-y+o)/g;
  17. v=(-y-o)/g;
  18. Now u and v are the two value of quadratic equation and now write output statement.
  19. System.out.println("Solution set = { "+u+" , "+v+"}");
  20. Now write the closing curly braces of method quadii.
Note: main method and user defined method should be inside the body of class at the top.

No comments:

Post a Comment