Axel
Axel

Reputation: 35

Java: Creating a program to find surface area and volume of a cylinder

I am trying to make a program with two methods which calculate and return the surface area and volume of a cylinder.

When i run the program the system outputs 0.00 for both and i'm unsure of what i'm doing wrong.

Here is my code:

public class Circle {
public static int height; 
public static int radius; 
public static double pi = 3.14; 

public Circle(int height, int radius) { 
    height = 10; 
    radius = 5; 
}


public static double getSurfaceArea() {
    int radiusSquared = radius * radius; 
    double surfaceArea = 2 * pi * radius * height + 2 * pi * radiusSquared; 
    return surfaceArea; 
}

public static double getVolume() { 
    int radiusSquared = radius * radius; 
    double volume = pi * radiusSquared * height; 
    return volume; 
}


public static void main(String[] args) { 
    System.out.println("The volume of the soda can is: " + getVolume() + "."); 
    System.out.println("The surface area of the soda is: " + getSurfaceArea() + "."); 
}

}

Thanks in advance.

Upvotes: 0

Views: 16432

Answers (2)

samouray
samouray

Reputation: 578

You have to add this line of code to your main:

Circle c = new Circle(10,5);

so it would be like so:

public static void main(String[] args) { 
    Circle c = new Circle(10,5);
    System.out.println("The volume of the soda can is: " + c.getVolume() + "."); 
    System.out.println("The surface area of the soda is: " + c.getSurfaceArea() + "."); 
}

and change your circle constructor method to this:

public Circle(int height, int radius) {
this.height = height;
this.radius = radius;
}

Upvotes: 1

Dan12-16
Dan12-16

Reputation: 351

I believe this is what you are looking for:

public class Circle {
public static int height; 
public static int radius; 
public static double pi = 3.14; 

public Circle(int height, int radius) { 
    this.height = height; 
    this.radius = radius; 
}


public static double getSurfaceArea() {
    int radiusSquared = radius * radius; 
    double surfaceArea = 2 * pi * radius * height + 2 * pi * radiusSquared; 
    return surfaceArea; 
}

public static double getVolume() { 
    int radiusSquared = radius * radius; 
    double volume = pi * radiusSquared * height; 
    return volume; 
}


public static void main(String[] args) { 
Circle circle = new Circle(10,5);
    System.out.println("The volume of the soda can is: " + circle.getVolume() + "."); 
    System.out.println("The surface area of the soda is: " + cirlce.getSurfaceArea() + "."); 
}
}

Upvotes: 0

Related Questions