I am learning Classes and Objects, and I am on reference variables, and accessing an object's data and methods. In my textbook, we created a program which calculates the area of a circle, given the radius.
They declare the object reference variable, create an object, and assign the reference to a variable here:
Circle myCircle = new Circle();
They later give an example below of finding the area (getArea()* just returns the area given the radius):
System.out.println("Area is " + new Circle(5).getArea());
- Is the
5(number in parentheses) an input for the radius? - If so, why isn't it in the
getArea()parentheses? - Also, there are no arguments for
Circle()so how can you have a number in the () anyway?
*Code for getArea():
By the way, could you get rid of the parentheses if there is only one statement inside?
double getArea() { return radius * radius * Math.PI; }
Please excuse the horrid formatting - I wasn't able to use Ctrl-K, could someone edit it for me please.