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

  • Context: Java 
  • Thread starter Thread starter friendbobbiny
  • Start date Start date
  • Tags Tags
    Cylinders Java
Click For Summary
SUMMARY

The Java code provided for the Cylinder class contains errors related to the scope of the variables radius and height. These variables are declared within the constructor, making them inaccessible outside of it, where area and volume are calculated. Additionally, the static final variable PI is not initialized, which will lead to a compilation error. To optimize the code, the area should be calculated once and reused in the volume calculation.

PREREQUISITES
  • Understanding of Java class structure and object-oriented programming
  • Familiarity with variable scope in Java
  • Knowledge of static variables in Java
  • Basic mathematical operations in Java
NEXT STEPS
  • Learn about variable scope in Java, focusing on instance vs. local variables
  • Explore the use of static variables and constants in Java
  • Study optimization techniques for calculations in Java, such as reusing computed values
  • Investigate best practices for class design in Java, including constructors and field declarations
USEFUL FOR

Java developers, software engineers, and students learning object-oriented programming who are looking to understand variable scope and optimization in class design.

friendbobbiny
Messages
49
Reaction score
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
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   Reactions: 1 person
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   Reactions: Paul Uszak and friendbobbiny

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
17K
Replies
3
Views
3K