从另一个类调用静态方法

时间:2016-02-24 14:28:06

标签: java math calculator

所以我花了几天时间研究这个项目,我遇到了障碍。我正在尝试使用具有用户输入的特定程序来计算多个形状的表面积或体积。您可能会从Horstmann java概念书中将其视为项目8.5。

我不知道如何将方法从一个文件调用到另一个文件,并且需要弄明白。

以下是代码:

import java.util.Scanner;

public class p85SantoCharlie{


  public static double sphereVolume(double r){

   Scanner in = new Scanner(System.in);

   System.out.println("Please enter your Radius");
   r = in.nextDouble();

   double volume = (4/3) * Math.PI * r * r * r;

   System.out.println(volume);

   return volume;

   }

  public static double sphereSurface(double r){

   Scanner in = new Scanner(System.in);

   System.out.println("Please enter your Radius");
   r = in.nextDouble();

   double surface = 4 * Math.PI * r * r ;

   System.out.println(surface);

   return surface;

   }

  public static double cyliderVolume(double r, double h){

Scanner in = new Scanner(System.in);
Scanner out = new Scanner(System.in);

System.out.println("Please enter your Radius");
r = in.nextDouble();

System.out.println("Please enter your Height");
h = out.nextDouble();

double volume =  Math.PI * r * r * h ;

System.out.println(volume);

return volume;

   }

  public static double cylinderSurface(double r, double h){

Scanner in = new Scanner(System.in);
Scanner out = new Scanner(System.in);

System.out.println("Please enter your Radius");
r = in.nextDouble();

System.out.println("Please enter your Height");
h = out.nextDouble();

double surface = 2 * Math.PI * r * r * h ;

System.out.println(surface);

return surface;

   }  

  public static double coneVolume(double r, double h){

Scanner in = new Scanner(System.in);
Scanner out = new Scanner(System.in);

System.out.println("Please enter your Radius");
r = in.nextDouble();

System.out.println("Please enter your Height");
h = out.nextDouble();

double volume = Math.PI * r * r * h / 3 ;

System.out.println(volume);

return volume;

    }

  public static double coneSurface(double r, double h){

Scanner in = new Scanner(System.in);
Scanner out = new Scanner(System.in);

System.out.println("Please enter your Radius");
r = in.nextDouble();

System.out.println("Please enter your Height");
h = out.nextDouble();

double surface = Math.PI * r * (r + Math.pow(( r * r + h * h), .5));

System.out.println(surface);

return surface;
   }
}

这是主要文件:

    import java.util.Scanner;

public class p85SantoCharlieMain{

   public static void main(String[] args){

   p85SantoCharlie mainProgram = new p85SantoCharlie();

   Scanner in = new Scanner(System.in);

   System.out.println("Please select a shape");

   System.out.println("your choices are: Sphere, Cylinder, and Cone");

   String answer1 = in.next();

   String answer1Caps = answer1.toUpperCase();

   System.out.println("Fantastic! now select a formula");

   System.out.println("your choices are: surface area or volume");

   String answer2 = in.next();

   String answer2Caps = answer2.toUpperCase();



   if (answer1Caps.equals("SPHERE")&& answer2Caps.equals("SURFACE AREA")){

      mainProgram.sphereSurface();

   }



   } 

}

谢谢!

3 个答案:

答案 0 :(得分:1)

如果方法是静态的,您可以使用类名称

来调用它们
p85SantoCharlie.sphereVolume(1.1);

如果它们不是静态的,则初始化新的类实例并使用它来调用

p85SantoCharlie p = new p85SantoCharlie();
p.sphereVolume(1.1);

答案 1 :(得分:0)

三个问题:

首先,p85SantoCharlie中的所有计算方法都是static。您不需要p85SantoCharlie的实例来调用它们。你应该摆脱new p85SantoCharlie()所在的行并调用你的方法,如

p85SantoCharlie.sphereVolume(r);

其次,您已将计算方法声明为参数,但在调用它们时您不知道这些参数。例如,sphereVolume()被声明为double r。但是,在r方法中读取之前,您不知道sphereVolume()的值。这样就行不通了。您需要更改main方法以询问半径,然后将其传递给sphereVolume,如上所示。

第三,所有这些Scanners!除了main中的那个之外,除去它们。在调用计算方法时,您将传递r(或其他)的值。只需使用传入的内容。

答案 2 :(得分:0)

丑陋,难以理解的代码。

Java是一种面向对象的语言。我希望您从Shape界面开始:

public interface Shape {
    double surfaceArea();
    double volume();
}

我希望看到适当进行计算的不同类型的子类。这是一个Sphere实现:

public class Sphere implements Shape {
    private final double radius;

    public Sphere(double radius) {
        if (radius <= 0.0) throw new IllegalArgumentException("radius must be positive");
        this.radius = radius;
    }

    public double surfaceArea() {
        return 4.0*Math.PI*this.radius*this.radius;
    }

    public double volume() {
        return 4.0*Math.PI*this.radius*this.radius*this.radius/3.0;
    }
}

这是一个Cylinder实现:

public class Cylinder implements Shape {
    private final double radius;
    private final double height;

    public Cylinder(double radius, double height) {
        if (radius <= 0.0) throw new IllegalArgumentException("radius must be positive");
        if (height <= 0.0) throw new IllegalArgumentException("height must be positive");
        this.radius = radius;
        this.height = height;
    }

    public double surfaceArea() {
        return 2.0*Math.PI*this.radius*this.radius + this.radius*this.height;
    }

    public double volume() {
        return Math.PI*this.radius*this.radius*this.height;
    }
}

我希望看到您的驱动程序类与Shape对象进行交互以解决问题。

您想要考虑Shape的虚拟构造函数/工厂,因为您不希望使用if/else构造来混淆您的代码。

我的观点:面向对象编程的发明是为了解决两个问题:

  1. 将状态和行为封装到单个抽象数据类型中。
  2. 继承和多态以消除if / else和switch语句。
  3. 请勿使用&#34来填充您的代码;如果是球体和区域&#34;各种决定。让多态来为你处理它。

    新程序员倾向于花太多时间担心用户界面。专注于功能并首先使其工作。不要创建一堆文本问题/答案代码,并解决问题的原因。