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

  • Thread starter Thread starter friendbobbiny
  • Start date Start date
  • Tags Tags
    Cylinders Java
AI Thread Summary
The Java code provided has issues related to the scope of the variables `radius` and `height`. These variables are declared within the constructor of the `Cylinder` class, making them local to that constructor and inaccessible outside of it. Consequently, when attempting to calculate `area` and `volume` at the class level, the code fails to recognize `radius` and `height`. Additionally, the declaration of `PI` is incomplete, as it lacks an assigned value. To resolve these issues, `radius` and `height` should be declared as instance fields of the class. Furthermore, the calculation of volume could be optimized by using the already computed area instead of recalculating it.
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 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 Paul Uszak and friendbobbiny
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top