I'm unsure why java won't recognize radius and height

In summary: Also, your PI variable is not initialized. In summary, the code is incorrect because the radius and height variables are declared in the constructor and cannot be accessed at the class level. Additionally, the PI variable is not initialized. These errors should be fixed for the code to run properly.
  • #1
friendbobbiny
49
2
Why is this java code wrong? I'm unsure why java won't recognize radius and height. Both should be fields for any object of the Cylinder class. My declaration for PI may be off, but that doesn't produce an error.






public class Cylinder {
public static final double PI;

public Cylinder (double a, double b)
{ double radius = a;
double height = b;}

double area = radius * radius * PI;
double volume = PI * radius * radius * height;

public static void main(String[]args)
{Cylinder sample1 = new Cylinder (43.0,47.0);
System.out.println("sample1 area is" + sample1.area);
System.out.println("Sample1 volume is" + sample1.volume);
 
Technology news on Phys.org
  • #2
Optimization tip: You've already calculated the area of the base and stored it in the variable 'area'. Why calculate the area again when you calculate the volume of the cylinder? The volume is area * height.
 
  • Like
Likes 1 person
  • #3
friendbobbiny said:
Why is this java code wrong? I'm unsure why java won't recognize radius and height. Both should be fields for any object of the Cylinder class. My declaration for PI may be off, but that doesn't produce an error.


public class Cylinder {
public static final double PI;

public Cylinder (double a, double b)
{ double radius = a;
double height = b;}

double area = radius * radius * PI;
double volume = PI * radius * radius * height;

public static void main(String[]args)
{Cylinder sample1 = new Cylinder (43.0,47.0);
System.out.println("sample1 area is" + sample1.area);
System.out.println("Sample1 volume is" + sample1.volume);
You are declaring radius and height in the constructor and trying to access them outside of it at the class level when you create the area and volume variables. You can't do that.
 
  • Like
Likes Paul Uszak and friendbobbiny

1. What is a Java Cylinder?

A Java Cylinder is a data structure used in programming that stores a collection of objects in a specific order. It is similar to an array in that it can hold multiple objects, but it also allows for efficient insertion and removal of objects.

2. How do you create a Java Cylinder?

To create a Java Cylinder, you first need to import the java.util package. Then, you can use the syntax "Cylinder<DataType> cylinderName = new Cylinder<>();" to declare and initialize a new Java Cylinder object.

3. What are the main methods of a Java Cylinder?

The main methods of a Java Cylinder are add(), remove(), get(), and size(). The add() method adds an object to the end of the Cylinder, remove() removes an object at a specified index, get() returns the object at a specified index, and size() returns the number of objects in the Cylinder.

4. How is a Java Cylinder different from an ArrayList?

A Java Cylinder is different from an ArrayList in that it allows for efficient insertion and removal of objects, while an ArrayList does not. Additionally, a Java Cylinder can only hold objects of a specified data type, while an ArrayList can hold any type of object.

5. When should I use a Java Cylinder?

You should use a Java Cylinder when you need to store a collection of objects in a specific order and want efficient insertion and removal of objects. It is also useful when you need to ensure that all objects in the collection are of the same data type.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
772
  • Programming and Computer Science
Replies
1
Views
750
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
7
Views
17K
Back
Top