Class method and constructor mess in java

I have a class that is required to create a simple method with two input parameters. This method must be used when creating other methods in other classes. My question is, does he, as I wrote, seem right? and when calling this method in another class, what would it look like?

public class IntlDensity {

    static double p = PhysicalConst.pi; 

    public IntlDensity(double m, double r) {
    }

    public double irho (double mass, double rad) {
       return(mass/(((4.0/3)*p*Math.pow(rad, 3))));
    }
}
+4
source share
5 answers

What you probably wanted to do is:

public class PhysicalBody {

    private double mass;
    private double rad;

    public PhysicalBody(double m, double r) {
        this.mass = m;
        this.rad = r;
    }

    public double getIrho() {
       return(mass/(((4.0/3)*PhysicalConst.pi*Math.pow(rad, 3))));
    }

    public double getMass() { return mass; }
    public double getRad() { return rad; }
}

, mass rad, , irho . , , , . , .

.

, , , - , , , PhysicalConst. - .

, , - .

+3
  • , .
  • ,
  • Pi Math,
  • Irho , - .

.. irho, .

+2

, , , .

, "", - . , , , IntlDensity.

- Test-Driven Development. , - API , .

, , , , , , :

public class IntlDensity {
    private double mass;
    private double rad;

    public IntlDensity(double mass, double rad) {
      this.mass = mass;
      this.rad = rad;
    }

    public double irho() {
       return mass/(((4.0/3) * PhysicalConst.PI * Math.pow(rad, 3)));
    }
}

:

IntlDensity id = new IntlDensity(5, 10);
System.out.println(id.irho());

, , . .

+1

static -

public class IntlDensity {
  private static final double PI = PhysicalConst.pi;     // A local PI constant?

  public static double irho(double mass, double rad) {   // Static
    return (mass / (((4.0 / 3) * PI * Math
        .pow(rad, 3))));
  }
}
0

Is there a reason you want to instantiate an object IntlDensity? If not, you can just make the method static and call it

...
IntlDensity.irho(5,2);

public class IntlDensity {

    static double p = PhysicalConst.pi; 

    public static double irho (double mass, double rad) {
       return(mass/(((4.0/3)*p*Math.pow(rad, 3))));
    }
}
0
source

All Articles