Question

I have a homework assignment where I take input from users for radius and height of a cylinder, then return the volume. I have most of it done I think, but am having trouble bringing it all together. I am getting a syntax error when I try to print it all out. Here is the print line

for (int i = 0; i < volume.length; i++)
{
    System.out.println("for Radius of:" + volume[i].getRadius() + " and Height of:" + volume[i].getHeight() + " the Volume is:" + volume.getVolume());
}

Here is the main class

import javax.swing.*;

//Driver class
public class CylinderTest
{

    public static void main(String[] args)
    {

        Cylinder[] volume = new Cylinder[3];
        for (int counter = 0; counter < volume.length; counter++)
        {
            double radius = Double.parseDouble(JOptionPane
                    .showInputDialog("Enter the radius"));
            double height = Double.parseDouble(JOptionPane
                    .showInputDialog("Enter the height"));
            volume[counter] = new Cylinder(radius, height);
        }

        for (int i = 0; i < volume.length; i++)
        {
            System.out.println("for Radius of:" + volume[i].getRadius() + " and Height of:" + volume[i].getHeight() + " the Volume is:" + volume.getVolume());
        }
    }
}

and here is the Cylinder class

public class Cylinder
{
    // variables
    public static final double PI = 3.14159;
    private double radius, height, volume;

    // constructor
    public Cylinder(double radius, double height)
    {
        this.radius = radius;
        this.height = height;
        this.volume = volume;
    }

    // default constructor
    public Cylinder()
    {this(0, 0);}

    // accessors and mutators (getters and setters)
    public double getRadius()
    {return radius;}

    private void setRadius(double radius)
    {this.radius = radius;}

    public double getHeight()
    {return height;}

    private void setHeight(double height)
    {this.height = height;}

    public double getVolume()
    {return volume;}

    // Volume method to compute the volume of the cylinder
    public double volume()
    {return PI * radius * radius * height;}

    public String toString()
    {return volume + "\t" + radius + "\t" + height; }

}

I am trying to get the volume from the Cylinder class getVolume getter.

Was it helpful?

Solution

It's quite easy, you're missing something here:

volume.getVolume()

should be volume[i].getVolume()

volume is the array, while volume[i] is an instance of your Cylinder class.

As a side note, instead of defining PI in a constant, you can use Math.PI which is already defined (and more accurate).

Updated answer: In your Cylinder class, you are initializing the volume variable to 0. I suggest you remove the volume variable and the getVolume method. And instead of calling the getVolume method you should call the volume() method. Computing the volume is very fast, you don't need to store it as a variable in the class.

OTHER TIPS

Instead of

volume.getVolume()

Use

volume[i].getVolume()

You were using the array instead of the element of the array - and the array didn't have a getVolume() method... That understandably results in a syntax error...

volume is an array (of Cylindar), so it doesn't have your method getVolume. You probably want volume[i].getVolume, since that's the cylindar object you are referencing in the rest of the print statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top