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);
		
		System.out.print("Please enter a radius: ");
		Geometry geometry = new Geometry();
		geometry.sphereVolume(in.nextDouble());
		
		System.out.println("The Volume of the Cone is " + geometry.sphereVol);
		
		

	}

}

Dani AI

Generated

Good progress by — the compile error in the first post is a scoping/usage issue, not a math one. The variable sphereVol inside Geometry.sphereVolume is local to that method and cannot be referenced from GeometryTester. Static methods return a value; call them on the class and either capture the return in a local variable or print it directly. Creating new Geometry() is unnecessary for static methods and accessing a method's local variable from outside is impossible.

The cone "surface area" in the later post is returning only the lateral area. For a right circular cone:

  • lateral surface = π r l, where l = sqrt(r^2 + h^2)
  • total surface = lateral + base = π r (r + l)

Example calculation (use in tester when r and h are known):

double l = Math.sqrt(r * r + h * h);
double coneTotalSurface = Math.PI * r * (r + l);

Note that the line from is syntactically broken and also inserts π inside the square root incorrectly; it needs the square root of r^2 + h^2, not of πr^2. A few practical tips: prefer `rr/h*hoverMath.powfor integer exponents (clearer and faster), call static methods asGeometry.methodName(...)for readability, and store input values in named variables if they are reused. UseSystem.out.printf` to format numeric output for easier checking of results. These fixes resolve the "can't be resolved" error and will produce the correct cone surface area depending on whether the assignment expects lateral-only or total surface area.

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);
		
		System.out.print("Please enter a radius: ");
		Geometry geometry = new Geometry();
		double r = in.nextDouble();
		in.nextLine();
		System.out.print("Please enter a height: ");
		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 meeting, networking, learning, and sharing knowledge.