Search code examples
javaclassmethodsconstructorreturn

Debugging my first class call. Creating a circle with user input


The given problem was to create two Java classes, one with constructor methods and the other with a main method, calling the first.

The second half won't compile due to a "Error, cannot find symbol" with symbols symbol: method getArea() and location location: variable radius of type Circle

Prof said to check there was something off with my usage of printf, but that's not what's causing the compile errors, is it?

I've done some quick searching for similar problems but a lot of issues people with the same errors were having were related to their class and methods being private.

// Class I'm trying to call from

public class Circle
{
    double radius;
    public Circle(double r)
        {
        radius = r;
        }
    public Circle()
    {
        radius = 0.0;
    }
    public void setRadius(double r)
    {
       radius = r;
    }
    public double getRadius()
    {
        return radius;
    }
    public double area()
    {
        return Math.PI * Math.pow(radius, 2);
    }
    public double diameter()
    {
        return radius * 2.0;
    }
    public double circumference()
    {
        return Math.PI * radius * 2.0;
    }
}



// Seperate java program, used to call Circle.java

import java.util.Scanner;
public class CircleDemo
{
    public static void main(String[] args)
    {
        String input;
        double value;
        Circle radius = new Circle();
        System.out.printf("Enter the radius"+
                        "of a circle");
        Scanner keyboard = new Scanner(System.in);
        input = keyboard.nextLine();
        value = Double.parseDouble(input);
        radius.setRadius(value);
        System.out.printf("Area: " + radius.getArea()
        + "\n Diameter: " + radius.getDiameter() +
        "\nCircumference: " + radius.getCircumference());
        System.exit(0);
    }
}

Error output:

CircleDemo.java:18: error: cannot find symbol
        area = radius.getArea();
                     ^
  symbol:   method getArea()
  location: variable radius of type Circle
CircleDemo.java:19: error: cannot find symbol
        diameter = radius.getDiameter();
                         ^
  symbol:   method getDiameter()
  location: variable radius of type Circle
CircleDemo.java:20: error: cannot find symbol
        circumference = radius.getCircumference();
                              ^
  symbol:   method getCircumference()
  location: variable radius of type Circle
3 errors

Solution

  • You're looking for the wrong method names.

    You've defined the method circumference(), but are calling getCircumference(). You've define the method area(), but are calling getArea().

    Either rename the methods in Circle, or change the methods called from main.