Hi DaniWeb community,

I'm trying to use the MultiCylinder class to instantiate and update the Cylinder objects that will print their radius and height. I also want to allow the user to input their own values for radius and height which will then calculate via the getVolume and getSurface area. I've been playing around with the code and I still get errors. Here is what i have so far.

public class Cylinder 
{
	double volume;
	double surfaceArea;
	double radius;
	double height;
	double result;

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

	public double getRadius ()
	{
		return radius;
	}

	public double getHeight()
	{
		return height;
	}

	public double getsurfaceArea()
	{

		return (2 * Math.PI * radius) * (radius + height);

	}

	public double getVolume()
	{

		return (Math.PI * height * Math.pow(radius, radius));

	}
}

and,

import java.util.Scanner;

public class MultiCylinder {

	public static void main(String[] args) {
		
		Cylinder cyl1  = new Cylinder(0.0 , 0.0);
		Cylinder cyl2  = new Cylinder(0.0 , 0.0);

		Scanner scan  = new Scanner (System.in);

		System.out.println("radius: " + cyl1.getRadius());

		System.out.println("height: " + cyl1.getHeight());

		System.out.println("volume: " + cyl1.getVolume());

		System.out.println("surface area: " + cyl1.getsurfaceArea());

	}

}

Any tips ?

Recommended Answers

All 3 Replies

I think that your formulas are not correct.
http://math.about.com/od/formulas/ss/surfaceareavol_3.htm

Check the documentation for "Math.pow". You are raising radius to the power of radius.

To get data do something like the following:

Scanner scan  = new Scanner (System.in);

//change the delimiter from space to newline
in.useDelimiter("\n");

System.out.println ("Enter Radius:");

if (in.hasNextDouble()){
     //read in value from user
     radius = in.nextDouble();
}//if

Rather than passing the arguments "(0.0,0.0)", you should be passing the values for radius and height that you read from the user.

How would I get an input from the user using the multicylinder class that would calculate the surface area and volume using the get/set methods from the cylinder class?

cgeier already showed you how to get input from the user and how to store that value into a variable. And you'd calculate using the get methods, if anything. That's why it's called "get". You can't calculate anything by using a set method, because set methods by definition are there for setting the value of a variable.

If you want the user to be able to create a Cylinder, you'd do something like cgeier had above, the only difference being that after you stored their input into variables, such as radius, you'd say Cylinder cyl = new Cylinder(radius, whatever); then when you wanted to do your calculations you could do something like

double answer = cyl.getRadius() * cyl.getWhatever();

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.