1

so i have to create a java program that calculates the diameter and area of a circle using methods: setRadius()and getRadius(). The setRadius() also has to be used to calculate the two values and set the radius. I used a constructor to assign radius as 1, but I also have to compute the diameter/area for a larger circle. When running the code, it says that it "cannot find symbol" with the diameter in the System print line. This is the code I have so far:

class My_Circle extends Main{
    float radius = 1;
    
    public float getRadius() {
        return radius;
    }
    
    public void setRadius(float newRadius) {
        this.radius = newRadius;
        float diameter = radius*2;
        float area = radius*radius*3.14159265359f;
    }
}

public class Main {
    public static void main(String args[]) {
        My_Circle large = new My_Circle();
        My_Circle original = new My_Circle();
        
        large.setRadius(100);
        original.setRadius(original.getRadius());
        
        System.out.println("Large Circle Diameter: " + diameter);
        
    }
}
  • 1
    `diameter` is not visible from your `main` method. – Arnaud Dec 23 '21 at 08:08
  • @Arnaud a dumb question but, how do i fix that? – Niall Howalk Dec 23 '21 at 08:10
  • By stepping back. Ask yourself: you made `radius` a **field** of your whole class. But then you also defined **local** variables in this or that method. Ask yourself: what **should** be visible where?! You see ... both local variables diameter and area **only** exist in `setRadius()` method. – GhostCat Dec 23 '21 at 08:18
  • Hint: maybe, instead of computing those two values in that setMethod, you could add 2 other methods: `getDiameter()` and `getArea()`. And then, when you need the diameter of a Circle object, just call those method (or you can add those two numbers as fields to your class, and compute them once). And note: you could use a constructor instead of a setter. And: there is **no** reason at all at that your Circle class extends Main. Remember: you have to understand *every character* that you put into your source code. You dont extend Circle from Main because you *can*. – GhostCat Dec 23 '21 at 08:21
  • You only use those constructs that **make sense** to be used. Example: if you had a "Shape" base class, then it would make sense to extend Circle from that. But your Main class is just the entry point, the "driver" that is used to invoke some code in the end. No need to make Circle a Main. – GhostCat Dec 23 '21 at 08:22
  • Finally: read about java naming conventions. Class names go UpperCase, and you only use "_" in SOME_CONSTANT. – GhostCat Dec 23 '21 at 08:22

2 Answers2

0

diameter is out of scope in the Main class, because diameter is only declared when you call setRadius() You need to declare diameter and area the same way you declared radius.

class My_Circle extends Main{
float radius = 1;
float diameter = radius*2;
float area = radius*radius*3.14159265359f;

Additionally, you need to create a getter method for diameter (like you did for radius)

  • This is bad advise. Your additional two fields there ... would be using a radius of 1. So when the setter gets called, the other values would stay at their initially computed number. So, if you really want to help, give a full example that addresses *all* issues in the code. – GhostCat Dec 23 '21 at 08:24
0

Firstly, your class My_Circle should not inherit from Main (remove the extends). Then you cannot get the variable diameter because it is not referenced anywhere in the Main class. If you want the diameter of the large circle just do large.getRadius() * 2 or add diameter variable in the same way you did for the radius. This way you will be able to do large.getDiameter().

Marinos33
  • 56
  • 1
  • 8
  • *Then you cannot get the variable diameter because it is not referenced anywhere in the Main class.* Plain wrong. It is not about **referencing** in another place, it is about the fact that a **local** variable from one method isn't in the scope of another method. Please understand that all content here should have decent quality. If you lack the knowledge to clearly express a correct answer, consider to not write that down as answer. – GhostCat Dec 23 '21 at 08:26