简体   繁体   中英

How to call void method in different class?

public class Equation {
    public static void quadEquation(double a, double b, double c) {
        double solution1 = (-b + (Math.pow(b,2)-(4*a*c))/(2 * a));
        double solution2 = (-b - (Math.pow(b,2)-(4*a*c))/(2 * a));    
    }
}

I have to print solution1 and soltion2 at different class(tester) by calling them.

It is a void method call, so it doesn't returning anything.

In this case how can I call them in tester?

Homework assignment! It is just part of my homework so you guys can freely help this one

I solved it! Thanks for those of you helped me :)

This is how you call a static void method in another class.

== file exercise/Equation.java ==

package exercise;

public class Equation {
    public static void quadEquation(double a, double b, double c) {
        double solution1 = (-b + (Math.pow(b,2)-(4*a*c))/(2 * a));
        double solution2 = (-b - (Math.pow(b,2)-(4*a*c))/(2 * a));  

        // If you want to print the solutions, add some print 
        // statements here!
    }
}

== file OtherClass.java ==

import exercise.Equation;

public class OtherClass {
    public static void main(String[] args) {
        Equation.quadEquation(1.0, 2.0, 3.0);  // calls the method in Equation
    }
}

You should be able to compile and run the above from the command line as follows.

$ # change directory to directory containing "OtherClass.java"
$ javac -cp . exercise/Equation.java OtherClass.java 
$ java -cp . OtherClass
 

The key things are:

  1. If you want to use Equation in another class, it is best if you declare the class in a Java package.

  2. Then you needs to import the Equation into the other class. (This is not necessary if both classes are in the same package.)

  3. Obviously a void method cannot return anything. So if you want some output you from your quadEquation method, then the method needs to print it itself.

Note that the approach described in the previous point is typically a pour design choice. You typically don't want to mix the "concerns" of the code like that. You typically want to treat the calculation and outputting the results as separate problems. For example, you may want your quadEquation method to be usable in contexts where you output the solution or solutions somewhere else ... or not at all.

Other things:

  • If this is supposed to find the roots of a quadratic equation, you have not coded the formula correctly. There should be a "square root" in there; see https://en.wikipedia.org/wiki/Quadratic_formula .

  • You also need to consider the case where the solutions are imaginary numbers. (Unless you have been told to ignore that ...)

  • I see that you have commented to say that the other class is a driver. This may make the above example invalid. You are going to have to read the code of the driver class (or the description of the class) top work out how it is going to call your class. It may well be that the problem of calling your quadEquation has been solved for you by your teacher.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM