-1

I know that these types of questions get asked a lot and I've read this one and this one but for some reason I'm still having troubles understand the issue my current program is having.

I'm trying to create a set of classes that defines a series of 3-D shapes that can store its size, and provides access to change the data. It should also be able to calculate circumference, area, and volume. I've only gotten to the point where I'm doing my first chosen shape (a sphere) and I'm getting this error in the println inside the if statement.

    import java.util.Scanner;
public class Shapes
{
    public static void main(String[] args)
    {
        String input;
        double Radius;

        Scanner scan = new Scanner(System.in);

        System.out.println("Choose a shape. 'S' for sphere, 'P' for pyramid, 'C' for cone: ");
        input = scan.next();

        if (input.equals("S"))
        {
            System.out.println("Enter radius: ");
            Radius = scan.nextDouble();
            Sphere calcCircumference;
            Sphere calcArea;
            Sphere calcVolume;
            System.out.println("The circumference is "+Sphere.circumference+", the area is "+Sphere.area+", the volume is "+Sphere.volume+".");
        }

    }
}

public class Sphere
{
    // instance variables
    protected double radius;
    protected double circumference;
    protected double area;
    protected double volume;
    Scanner scan = new Scanner(System.in);

    /**
     * Constructor for objects of class Sphere
     */
    public Sphere()
    {
        // initialise instance variables
        radius = 0;
        circumference = 0;
        area = 0;
        volume = 0;
    }

    /**
     *Gets user entered radius
     */
    public double getRadius(double Radius)
    {
        radius = radius;
        return radius;
    }

    public double calcCircumference()
    {
        circumference = 2*Math.PI*radius;
        return circumference;
    }

    public double calcArea()
    {
        area = 4*Math.PI*Math.pow(radius,2);
        return area;
    }

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

Any help or guidance would be appreciated.

Community
  • 1
  • 1
Elliot
  • 3
  • 1
  • `Sphere.circumference` - which sphere? – user253751 Feb 09 '15 at 05:23
  • You need to create an object of Sphere to acess circumference or define them as static and why are you creating calcCircumference calcArea calcVolume objects of Sphere? Or are you trying to call those functions like this? – Vivek Singh Feb 09 '15 at 05:24

2 Answers2

1
System.out.println("The circumference is "+Sphere.circumference+", the area is "+Sphere.area+", the volume is "+Sphere.volume+".");

You need to create an instance of Sphere before you can access it fields.

Sphere sphere = new Sphere();

You then need to provide the information that the object needs

sphere.radius = Radius; // I'd prefer a setter method

Then you can make use of the information that this instance provides...

System.out.println("The circumference is "+sphere.calcCircumference()+", the area is "+Sphere.calcArea()+", the volume is "+Sphere.calcVolume()+".");

There is no need to keep track of circumference, area or volume, they are calculated fields, you simply need to call the methods you need to get the calculated result.

The fields circumference, area, volume and radius are known as instance fields, they require an instance of the class before you can use them. This means you can have multiple instances of Sphere each with there own unquie values

You might like to take a closer look at Classes and Objects

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

I changed your code a bit, now it is working (may not be the most efficient way):

import java.util.Scanner;

public class Shapes {
    public static void main(String[] args) {
        String input;
        double Radius;

        Scanner scan = new Scanner(System.in);

        System.out
                .println("Choose a shape. 'S' for sphere, 'P' for pyramid, 'C' for cone: ");
        input = scan.next();

        if (input.equals("S")) {
            System.out.println("Enter radius: ");
            Radius = scan.nextDouble();
            Sphere calcCircumference;
            Sphere calcArea;
            Sphere calcVolume;
            System.out.println("The circumference is " + Sphere.circumference
                    + ", the area is " + Sphere.area + ", the volume is "
                    + Sphere.volume + ".");
        }

    }

    public static class Sphere {
        // instance variables
        protected double radius;
        protected static double circumference;
        protected static double area;
        protected static double volume;
        Scanner scan = new Scanner(System.in);

        /**
         * Constructor for objects of class Sphere
         */
        public Sphere() {
            // initialise instance variables
            radius = 0;
            circumference = 0;
            area = 0;
            volume = 0;
        }

        /**
         * Gets user entered radius
         */
        public double getRadius(double Radius) {
            radius = radius;
            return radius;
        }

        public double calcCircumference() {
            circumference = 2 * Math.PI * radius;
            return circumference;
        }

        public double calcArea() {
            area = 4 * Math.PI * Math.pow(radius, 2);
            return area;
        }

        public double calcVolume() {
            volume = (4 * Math.PI * Math.pow(radius, 3)) / 3;
            return volume;
        }
    }
}
fida1989
  • 3,234
  • 1
  • 27
  • 30
  • And when the OP creates an more the one instance of `Sphere`, they will have a lot of jobs with the same values, based on which one called the associated `calcXxx` method...`static` is evil and should be avoid (IMHO) – MadProgrammer Feb 09 '15 at 05:27
  • and making varibales static wont require initialisation to default values in constructor, you can omit those lines. – Vivek Singh Feb 09 '15 at 05:28