Search code examples
javamethodsparameterscall

How to call for a method with parameters?


Okay so, I want to make a program that can find both the discriminant of a quadratic equation as well as the number of roots. I so far created the program to find the discriminant, but i'm having trouble calling the method to find the number of roots. Can someone please explain to me how this is done? Thanks.

 public class quadMethods
 {
     public static void main (String args[])
     {
         new quadMethods ();
     }


     public quadMethods ()
     {
         System.out.println ("The discriminant is: " + discrim (1, 6, 8));
         System.out.println ("The number of roots is: " + numRoots (1, 6, 8));
     }
  public double discrim (double a, double b, double c)
     { //assumes ax^2+bx+c=0
         //returns the discriminant of the quadratic equation
         //b*b-4*a*c
         //replace the return 1
    double discriminant = b*b-4*a*c;    
         return discriminant;
     }


     public int numRoots (double a, double b, double c)
     { //assumes ax^2+bx+c=0
         //returns the number of roots for the quadratic equation
         //call discrim method, make an if to return 0, 1, or 2.
         //replace the return 1
  return 1;
     }
 }

Solution

  • Oh by the way, the answer I was looking for was:

    public double discrim (double a, double b, double c)
    { 
      return (b*b)-(4*a*c);
    }
    
    
    public int numRoots (double a, double b, double c)
    { 
    double d = discrim (a,b,c);
    if (d>0)
     return 2;
    else if (d<0)
     return 0;
    else
     return 1;
    }