OK, here's my assignment: Write static methods
public static double sphereVolume(double r)
public static double sphereSurface(double r)
public static double cylinderVolume(double r, double h)
public static double cylinderSurface(double r, double h)
public static double coneVolume(double r, double h)
public static double coneSurface(double r, double h)
that compute the volume and surface area of a sphere with radius r, a cylinder with circular base with radius r and height h, and a cone with circular base with radius r and height h. Place them into a class Geometry. Then write a program that prompts the user for the values of r and h, calls the six methods, and prints the results.

I've got the geometry class done.. I'm having problem with the tester...I can't seem to call the variable to my tester class. I've coded in baby steps so I've only tried to call one method so far... I'm sure if I learn how to call it, the rest will follow. Says sphereVol can't be resolved. Any help is greatly appreciated.

``````public class Geometry {

public static double sphereVolume(double r){
double sphereVol = (4.0 / 3.0) * Math.PI * (Math.pow(r, 3));
return sphereVol;
}

public static double sphereSurface(double r){
double sphereSurArea = 4.0 * Math.PI * (Math.pow(r, 2));
return sphereSurArea;
}

public static double cylinderSurface(double r, double h){
double cylSurArea = 2 * (Math.PI * (Math.pow(r, 2))) + (2 * Math.PI * r)* h;
return cylSurArea;
}

public static double coneVolume(double r, double h){
double coneVol = 1.0 / 3.0 * Math.PI * (Math.pow(r, 2)) * h;
return coneVol;
}

public static double coneSurface(double r, double h){
double coneSurArea = Math.PI * r * Math.sqrt(Math.pow(r, 2)+Math.pow(h, 2));
return coneSurArea;
}

}

import java.util.Scanner;

public class GeometryTester {

/**
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

Geometry geometry = new Geometry();
geometry.sphereVolume(in.nextDouble());

System.out.println("The Volume of the Cone is " + geometry.sphereVol);

}

}``````

I kept working on it... and I think I got it... but my Surface Area of a Cone calculation is incorrect... can anyone help me with it?

``````public class Geometry {

public static double sphereVolume(double r){
double sphereVol = (4.0 / 3.0) * Math.PI * (Math.pow(r, 3));
return sphereVol;
}

public static double sphereSurface(double r){
double sphereSurArea = 4.0 * Math.PI * (Math.pow(r, 2));
return sphereSurArea;
}

public static double cylinderVolume(double r, double h){
double cylVol = Math.PI * (Math.pow(r, 2)) * h;
return cylVol;
}

public static double cylinderSurface(double r, double h){
double cylSurArea = 2 * (Math.PI * (Math.pow(r, 2))) + (2 * Math.PI * r)* h;
return cylSurArea;
}

public static double coneVolume(double r, double h){
double coneVol = 1.0 / 3.0 * Math.PI * (Math.pow(r, 2)) * h;
return coneVol;
}

public static double coneSurface(double r, double h){
double coneSurArea = Math.PI * r * Math.sqrt(Math.pow(r, 2)+Math.pow(h, 2));
return coneSurArea;
}

}

import java.util.Scanner;

public class GeometryTester {

/**
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

Geometry geometry = new Geometry();
double r = in.nextDouble();
in.nextLine();
double h = in.nextDouble();
System.out.println("The Volume of the Sphere is: " + Geometry.sphereVolume(r));
System.out.println("The surface area of the sphere is: " + Geometry.sphereSurface(r));
System.out.println("The volume of the cylinder is: " + Geometry.cylinderVolume(r,h));
System.out.println("The surface area of the cylinder is:  " + Geometry.cylinderSurface(r,h));
System.out.println("The volume of the cone is:  " + Geometry.coneVolume(r,h));
System.out.println("The surface area of the cone is:  " + Geometry.coneSurface(r,h));

}

}``````
``double coneSurArea = Math.PI * r * Math.sqrt(Math.pow(r, 2)+(Math.PI * (Math.pow(r,2));``

That works for me!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.