0

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());
  1. Is the 5 (number in parentheses) an input for the radius?
  2. If so, why isn't it in the getArea() parentheses?
  3. Also, there are no arguments for Circle() so how can you have a number in the () anyway?

*Code for getArea():

  1. 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.

9
  • Well, this is going to take a few months, so sit tight. Please see the help center to see what kinds of questions you can ask on StackOverflow. Commented Aug 17, 2017 at 19:06
  • I edited it to make the question less general - if that's what you meant by "take a few months". Commented Aug 17, 2017 at 19:08
  • Read the link, I didn't put it in there for fun. Commented Aug 17, 2017 at 19:10
  • I did read the link. Commented Aug 17, 2017 at 19:12
  • 1
    For someone who's been here 5 days it's not standing your ground, it's arrogance. Follow the guidelines. There are plenty of people here who would appreciate if we did their homework or their exams for a job interview etc., but that's not how SO (or the world) works. Commented Aug 17, 2017 at 19:33

3 Answers 3

1

Is the 5 (number in parentheses) an input for the radius?

System.out.println("Area is " + new Circle(5).getArea());

Not exactly.
It is the argument passed to a Circle constructor that should very probably value a radius field.

If so, why isn't it in the getArea() parentheses?

getArea() is an instance method of Circle. It relies on the state of the Circle instance that has already a radius information.
So passing a radius argument to getArea() makes no sense.

It would make sense if you had a utility static method in the Circle class to compute a area according to a radius.

For example :

public static double computeArea(double circleRadius){
    ...
}

You could invoke it in this way :

double area = Cicle.getArea(5.5);

Also, there are no arguments for Circle() so how can you have a number in the () anyway?

Without a Circle constructor that accepts a radius information, the invocation new Circle(5) cannot compile. So it of course requires one.

You should have a Circle constructor defined such as :

public class Circle{
  ...
  private double radius;

  public Circle(double radius){
    this.radius = radius;
  }
}

by the way, could you get rid of the parentheses if there is only one statement inside?

Parenthesis () refers to a specification of the Java language to declare method and constructor.
When you declare a method, you need it in any case.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but I have a question regarding the answer to the third question - from my first piece of source code, it shows that Circle has no arguments, so how does this make sense - It should compile, it was part of a source code from my textbook.
new Circle(5) will never compile if there is not a matching constructor in the Circle class such as Circle(double d). Try it, you could check that.
Quoted from textbook: "For example, new Circle() creates an object of the Circle class using the first constructor defined in the Circle class, and new Circle(25) creates an object using the second constructor defined in the Circle class. A class normally provides a constructor without arguments (e.g., Circle()). Such a constructor is referred to as a no-arg or no-argument constructor." Sorry for the generalness, but how does this match what you're trying to say?
Well. Suppose Circle is a class that declare a radius field. new Circle() is an invocation to a no arg constructor : public Circle(){...} It supposes that radius is valued with a default value while new Circle(5) is an invocation to a constructor that declares one argument, for example : public Circle(double radius){...} , it stores the radius parameter in a radius field.
0

I will try to explain in deep:

public class Circle {
    // this variable can access only inside class
    private final double radius;

    // constructor for class Circle 
    public Circle(double rad) {
        radius = rad;
    }

    // method of class Circle - can be access outside 
    public double area() {
        return radius*radius*Math.PI;
    }
}

when you instantiate class Circle as Circle circle5 = new Circle(5); you got instance of class Circle with encapsulated parameter radius = 5 now, you don't know any details, and just call method double area = circle5.area() to get the area of circle (rad*rad*Pi). This is dramatically decrease complexity. Even in this very simple example. now you can print you result

System.out.println(area);

I think, you got the idea. 1st step: you instantiated class and encapsulated all details into it via constructor; 2nd step: you can use you class (methods of the class) and never aware about implementation detail.

And in you sample, calling constructor without parameters new Circle() instead new Circle(5) - this is typographic mistake.

1 Comment

and try to get rid of public static double computeArea(double circleRadius){ ... } double area = Cicle.getArea(5.5); because static is have nothing with clean OOP
0

First let me explain constructors in Java. There are two types of constructors in java: 1. Explicit and 2. Implicit. Explicit means a constructor with argument and developed by developer. Implicit means, default or non parameter constructor. In your case let me analyse the operation: System.out.println("Area is " + new Circle(5).getArea()); new Circle(5), actually means that invoking a n explicit constructor with input parameter 5. According to your shared knowledge, it can be understood that a radius is set in this explicit constructor. I mean, the following constructor should be exist:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.