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
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top