How do I use a method I have made? JAVA

感情迁移 提交于 2019-12-25 14:00:15

问题


I have created this method to find the discrimiant using the function b^2-4ac

public class Discriminant {
public double Disc (double a, double b, double c){
        double disc;
        disc = Math.pow(b, 2) - 4*a*c;
        return disc ;
    }


}

Then i made another method to find all positive roots of a function

public class PlusRoot {

    public double plusRoot (double a, double b, double c){
        double proot;
        proot = -b + Math.sqrt(disc)/ 2*a;
        return proot;
    }
}

But it is not working because it says disc cannot be resolved as a variable, both methods are in the same package... how do I fix this?


回答1:


You have created a constructor and a method in two different classes, and you havent' declared them static - this means that to call them, you will need to create the appropriate instances first.

Instead, make the constructor a method, mark them both as static and move them into the same class, preferably in the same class as your main() method; then they'll be in the same namespace and you can just call them by their names.




回答2:


"how do I fix this?" <- have you tried fixing it :) ?

Do you have a main method in your program ??

public static void main (String args[]) {
   // stuff here
}

If not then you need this to even run the program.

There are few ways you can fix this. But firstly i would suggest renaiming your method ... Java methods should start with a Lower Case !

First:

private Discriminent disc;

public double plusRoot (double a, double b, double c){
        disc = new Discriminent();

        double proot;
        proot = -b + Math.sqrt(disc.Disk(variables here))/ 2*a;
        return proot;
}

Second:

public static void main(STring args[]) {
  Discriminent disc = new Discriminent();
  double x = disc.Disck(values here);

  PlustRoot pr = new PlusRoot(disc);

}

and inside your PlusRoot you can have a constructor which takes disc as parameter 

public class PlusRoot {
  public double disc; 
  public PlusRoot(double disc) {
    this.disc = disc;
  }

  // and then you can call disc within that instance by doing 'this.disc';

  so: proot = -b + Math.sqrt(this.disc)/ 2*a;


}

and many more ways you can fix this :)




回答3:


The reason for the error is that you do not have the variable disc. You need to initialize it before using it. You would need a line like

double disc = 0.0;

However, I am also guessing your intention was to use the return value of your Disc method. With how your two classes are set up now. You would have to instantiate a Discriminant object and then call the method Disc method like so before you use the variable disc.

Discriminant discriminant = new Discriminant();
double disc = discriminant.Disc(a, b, c); //or whatever 3 doubles you wish to use.

Look into different scope variables in java to better understand this issue.



来源:https://stackoverflow.com/questions/33790992/how-do-i-use-a-method-i-have-made-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!