-4

I have a class that finds the roots of a polynomial. In the Main function, it is telling me that the variable radicand does not exist. The function rootradicand() works fine, I have tested it with other values.

public double handleRadicand(double a, double b, double c){
    double radicand = b * b - (4 * a * c);
    return radicand;
}

public static void Main(String[] args){
    double a = 4;
    double b = 2;
    double c = 2;
    handleRadicand(a, b, c);
    rootRadicand(radicand);
    System.out.println(radicand);
}

Why is it telling me that radicand does not exist?

Gabe Spound
  • 568
  • 5
  • 28

6 Answers6

1

The problem is you are trying to use local variable of a method in other method, but which is out of scope for that variable.
But since you are returning the value of handleradicand() method you can catch the value in a variable or you can directly call method as argument like this:

rootradicand(handleradicand(a,b,c));
Kashyap Kansara
  • 405
  • 4
  • 10
1

According your screenshot:

public double handleradicand(double a, double b, double c)
{
double rad = b*b - (4*a*c);
return rad;
}

At that time, you need to create a parameter as a container to get the return rad value. For example:in your main method

double tem = handleradicand(a,b,c);
rootradicand(tem);

That should work.

Kun
  • 580
  • 2
  • 13
0

Radican cannot see the value from the method look like you need from getters and setters! How do getters and setters work?

Community
  • 1
  • 1
SaggingRufus
  • 1,814
  • 16
  • 32
0

radicand is defined in a different method (in handleradicand) and is therefore not visible in the method Main. In general variables defined in a method are not visible within another method

yaccob
  • 1,230
  • 13
  • 16
0

Looking at the screenshot, radicand is unavailable in Main because it's not "Global" like you think it is, but is instead a local variable. If you wanted radicand to be a global, it should be declared public static in a class somewhere (which is the closest thing to Global in Java), you'll need to refer to it as NameOfClassRadicandIsIn.radicand.

That said, Java is a language built specifically for a programming style that considers globals to be evil (and not without good reason, problems with globals are just awful things to debug). Fortunately, your code doesn't even need that variable, because you can cut out the middle man with:

double radical = rootradicand(handleradicand(a,b,c));

Though if you do want to keep the variable, simply declaring it in your Main method would work:

double radicand = handleradicand(a,b,c));
double radical = rootradicand(radicand);

Note that I added radical because the line after your call to rootradicand(...) has the exact same problem.

As you learn the language if you do find yourself tempted to try using a global (i.e. public static in java, this should be considered a last resort. You seem to be writing programs that only have one class now, but when you're dealing with multiple classes, any variables you want to expose between them should be declared as private and accessed with getter and setter methods.

Brandon McKenzie
  • 1,655
  • 11
  • 26
0

This is not valid:

return bover2a;
return radover2a;

and

return firstX;
return secondX;

You can't double return in a method in java, if you want this, you need to follow another approach like create a custom object and return the two values inside it.

And you are not assigning the returned value to any variable so every calculation made in handleradicand(), rootradicand(), divideby2a(), findroots() are lost once the call finished. You need something like:

double rad = handleradicand(a, b, c);
rad = rootradicand(rad);
...
Alex S. Diaz
  • 2,637
  • 2
  • 23
  • 35