eleven357
eleven357

Reputation: 51

my geometry program (Calculating area and volume of spheres, cylinders, and cones)

public class Geometry {

public static void main(String[] args) {
    input(0.0, 0.0);
    sphereVolume(0.0, 0.0);
    sphereSurface(0.0, 0.0);
    cylinderVolume(0.0, 0.0);
    cylinderSurface(0.0, 0.0);
    coneVolume(0.0, 0.0);
    coneSurface(0.0, 0.0);
    output(0.0, 0.0);
}
/**
 * @param radius
 * @param height
 */
public static void input(double radius, double height) {

    Scanner sc = new Scanner(System.in);

    System.out.print("Enter radius r: ");
    radius = sc.nextInt();

    System.out.print("Enter height h: ");
    height = sc.nextInt();
}

public static double sphereVolume(double radius, double height) {
    double volume = (4 / 3) * Math.PI * Math.pow(radius, 3.0);
    return volume;
}

public static double sphereSurface(double radius, double height) {
    double surface = 4 * Math.PI * Math.pow(radius, 2.0);
    return surface;
}

public static double cylinderVolume(double radius, double height) {
    double volume = Math.PI * Math.pow(radius, 2.0) * height;
    return volume;
}

public static double cylinderSurface(double radius, double height) {
    double surface = 2 * Math.PI * radius * height + 2 * Math.PI * Math.pow(radius, 2.0);
    return surface;
}

public static double coneVolume(double radius, double height) {
    double volume = (1 / 3) * Math.PI * Math.pow(radius, 2.0) * height;
    return volume;
}

public static double coneSurface(double radius, double height) {
    double surface = Math.PI * radius * (radius + Math.pow(( Math.pow(radius, 2.0) + Math.pow(height, 2.0)), .5));
    return surface;
}

public static void output(double radius, double height) {
    System.out.printf("Volume of sphere: %f\n", sphereVolume(0.0, 0.0));
    System.out.printf("Surface area of Sphere: %f\n", sphereSurface(0.0, 0.0));
    System.out.printf("Volume of cylinder: %f\n", cylinderVolume(0.0, 0.0));
    System.out.printf("Surface area of cylinder: %f\n", cylinderSurface(0.0, 0.0));
    System.out.printf("Volume of cone: %f\n", coneVolume(0.0, 0.0));
    System.out.printf("Surface area of cone: %f\n", coneSurface(0.0, 0.0));
}

My problem is that my output results in 0.0 everytime, regardless of the input. I'm sure it is probably something simple, but I can't seem to figure it out. Can you please help me out? Thanks a lot :) I appreciate your time.

EDIT

So I am still getting 0.0 for my output even though I changed my code like this:

    public static void output(double radius, double height) {
    System.out.printf("Volume of sphere: %.13f\n", sphereVolume(radius, height));
    System.out.printf("Surface area of Sphere: %.13f\n", sphereSurface(radius, height));
    System.out.printf("Volume of cylinder: %.13f\n", cylinderVolume(radius, height));
    System.out.printf("Surface area of cylinder: %.13f\n", cylinderSurface(radius, height));
    System.out.printf("Volume of cone: %.13f\n", coneVolume(radius, height));
    System.out.printf("Surface area of cone: %.13f\n", coneSurface(radius, height));
}

When I am calling my functions in main(), looks like this:

public static void main(String[] args) {
    input(0.0, 0.0);
    sphereVolume(0.0, 0.0);
    sphereSurface(0.0, 0.0);
    cylinderVolume(0.0, 0.0);
    cylinderSurface(0.0, 0.0);
    coneVolume(0.0, 0.0);
    coneSurface(0.0, 0.0);
    output(0.0, 0.0);
}

Do I need to do something different here?

Upvotes: 1

Views: 12670

Answers (5)

user5000
user5000

Reputation: 37

when you using double, it should be sc.nextDouble().

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter radius r: ");
double radius = sc.nextDouble();

System.out.print("Enter height h: ");
double height = sc.nextDouble();
}

Upvotes: 0

mohammad
mohammad

Reputation: 1

Create a class Cylinder; the class has attributes radius, and height. It has methods that calculate the surface area of a cylinder called surfaceArea and a method to calculate the volume in addition to the needed set and get methods

Note : use the following formulas for calculations : volume = (pi)r^2 * h where h is the height. surface area of a cylinder=2 * pi * radius * radius + 2* pi * radius * height Cylinder - height : float - radius : float <>Cylinder(r:float,h :float) + volume() : float + surfaceArea( ) : float + setRad ( r : float) + getRad( ) : float + setHeight(h : float) + getHeight() : float

Upvotes: 0

Mysticial
Mysticial

Reputation: 471369

You're dividing integers:

public static double coneVolume(double radius, double height) {
    double volume = (1 / 3) * Math.PI * Math.pow(radius, 2.0) * height;
    return volume;
}

Integer division rounds down in Java. Therefore, 1 / 3 is evaluating to 0.

Change it to:

double volume = (1. / 3.) * Math.PI * Math.pow(radius, 2.0) * height;

Here's the other occurrence:

double volume = (4 / 3) * Math.PI * Math.pow(radius, 3.0);

change to:

double volume = (4. / 3.) * Math.PI * Math.pow(radius, 3.0);

EDIT :

You're also calling all your functions with 0.0. So of course the results will be zero.

System.out.printf("Volume of sphere: %f\n", sphereVolume(0.0, 0.0));
System.out.printf("Surface area of Sphere: %f\n", sphereSurface(0.0, 0.0));
System.out.printf("Volume of cylinder: %f\n", cylinderVolume(0.0, 0.0));
System.out.printf("Surface area of cylinder: %f\n", cylinderSurface(0.0, 0.0));
System.out.printf("Volume of cone: %f\n", coneVolume(0.0, 0.0));
System.out.printf("Surface area of cone: %f\n", coneSurface(0.0, 0.0));

EDIT : Another Suggestion

You probably don't need all those Math.pow() calls since they're all called with 2 or 3. I'd say it's probably more concise and readable (and faster?) to just do:

radius * radius + height * height

instead of

Math.pow(radius, 2.0) + Math.pow(height, 2.0)

EDIT 2: More fixes:

Change your main to this. And get rid of the input function:

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.print("Enter radius r: ");
    double radius = sc.nextInt();

    System.out.print("Enter height h: ");
    double height = sc.nextInt();

    output(radius, height);
}

Upvotes: 4

Pops
Pops

Reputation: 30848

Are you typing your input by hand and relying on your Scanner? If so, that's your problem. The values from the Scanner are being assigned to your parameters in input(), and not actually being saved anywhere.

Your other methods are all being called with inputs of zero, for which outputs of 0.0 would actually be correct (although that doesn't mean you're bug-free).

Upvotes: 0

user973572
user973572

Reputation: 169

I am not a Java guy but shouldn't you be storing the input into some sort of global variables? Then using those variables as the arguments to your function calls instead of 0.0? For instance, this code:

public static void output(double radius, double height) {
    System.out.printf("Volume of sphere: %f\n", sphereVolume(0.0, 0.0));
    System.out.printf("Surface area of Sphere: %f\n", sphereSurface(0.0, 0.0));
    System.out.printf("Volume of cylinder: %f\n", cylinderVolume(0.0, 0.0));
    System.out.printf("Surface area of cylinder: %f\n", cylinderSurface(0.0, 0.0));
    System.out.printf("Volume of cone: %f\n", coneVolume(0.0, 0.0));
    System.out.printf("Surface area of cone: %f\n", coneSurface(0.0, 0.0));
}

will just print out the results of those function calls with 0.0 as the inputs, not radius/height user input.

And, yes, the integer math isn't helping.

Upvotes: 0

Related Questions