Search code examples
javajcreator

How to get rid of the 'cannot find symbol' errors in my code?


I am currently working on a project and i keep getting error messages. I am stuck and have contacted many people (including my instructor), and I have now turned to you guys.

Here is my code so far.

public class Circle

    private int radius = getRadius();
    private double area = getArea();

    public Circle(int r) 
    {
        r = radius;
    }

    public int getRadius()
    {
        return radius;
    }

    public double getArea(int r)
    {
        return area = Math.PI * r * r;  
    }
}

/

java.util.Scanner;

public class CircleTest
{
    public CircleTest()
    {
        int radius = getRadius();
        double area = getArea(r);       
    }   

    public static void main (String[] args) 
    {

        Scanner kboard = new Scanner(System.in);

        System.out.print("Give the radius of a circle. ");
        String area = kboard.nextLine();

        System.out.println("The area of the circle is... " + 
area);
        System.out.println();

        kboard.close();
    }
}

C:\Users\jthom\My Work\Circle\src\CircleTest.java:18: error: cannot find symbol

double area = getArea(r);

symbol: variable r

location: class CircleTest

.

C:\Users\jthom\My Work\Circle\src\CircleTest.java:33: error: cannot find symbol

System.out.println("The area of the circle is... " + area);

symbol: variable area

location: class CircleTest

2 errors


Solution

  • Let's start with class Circle. This is your original code:

    public class Circle
    
        private int radius = getRadius();
        private double area = getArea();
    
        public Circle(int r) 
        {
            r = radius;
        }
    
        public int getRadius()
        {
            return radius;
        }
    
        public double getArea(int r)
        {
            return area = Math.PI * r * r;  
        }
    
    }
    

    For your variables, you don't need "area", as it is calculated. Also, you shouldn't be assigning radius to anything except in the constructor:

        private int radius;
        //private double area = getArea(); <-- don't need this variable at all
    

    You pass in "r" to the constructor, but then incorrectly try to assign the "radius" value to it. This is backwards; you should be assigning the "r" value to "radius" instead:

        public Circle(int r) 
        {
            radius = r;
        }
    

    Finally, in getArea(), you don't need the radius passed in, or "area"; just return the calculated value (using the stored value in "radius", not "r"):

        public double getArea()
        {
            return Math.PI * radius * radius;  
        }
    

    Put all together, your Circle class should look more like:

    public class Circle
    
        private int radius;
    
        public Circle(int r) 
        {
            radius = r;
        }
    
        public int getRadius()
        {
            return radius;
        }
    
        public double getArea()
        {
            return Math.PI * radius * radius;  
        }
    
    }
    

    Over in CircleTest, you should first get the radius from the user, then pass that to the constructor of Circle. Finally, with your instance of Circle, call its getRadius() and getArea() methods.