zeesh91
zeesh91

Reputation: 153

Java OBJECTS and Classes beginner

Hey guys so we just started covering objects and classes in my class. I understand creating a new instance of the class pretty well. Howerver, its the data fields and methods im having trouble comprehending. Here is a sample from my book i wrote out. I dont quite get the part where i commmented: "//construct a circle with radius 1. Reason being b.c i declared the double radius in the SimpleCircle class already, so why would i need to write SimpleCircle again? I somewhat understand the accessors and mutators of this program but would like a bit more simple clarification on all this please.

public class TestSimpleCircle {

    public static void main(String[] args) {

        SimpleCircle circle1 = new SimpleCircle();
        System.out.println("The area of the circle of radius " + circle1.radius + " is " + circle1.getArea());

        // create a circle with radius 25
        SimpleCircle circle2 = new SimpleCircle(25);
        System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea());

        // create a circle with radius 125
        SimpleCircle circle3 = new SimpleCircle(125);
        System.out.println("The area of the circle of radius " + circle3.radius + " is " + circle3.getArea());

        // Modify circle radius of second object
        circle2.setRadius(100);   //or circle2.setRadius(100);
        System.out.println("The area of the circle of radius " + circle2.radius + " is " + circle2.getArea());

    } // end main
} // end class


class SimpleCircle {
    double radius;

    // construct a circle with radius 1
    SimpleCircle() {
        radius = 1;

    }

    // construct a circle with a specified radius
    SimpleCircle(double newRadius) {
        radius = newRadius;

    }

    // return the are of this circle
    double getArea() {
        return radius * radius * Math.PI;

    }

    // return the perimeter of this circle
    double getPerimeter() {
        return 2 * radius * Math.PI;
    }

    // set a new radius for this circle
    void setRadius(double newRadius) {
        radius = newRadius;
    }

} // end class

Upvotes: 1

Views: 251

Answers (2)

the_prole
the_prole

Reputation: 8945

Because you never assigned your field variable a value in the declaration statement. If you did, then you would not need to set it equal to 1 in the constructor. I hope that answers your question.

This

class SimpleCircle {
double radius;

// construct a circle with radius 1
SimpleCircle() {
    radius = 1;

}

Will lead to the same output as this

class SimpleCircle {
double radius = 1;

// construct a circle with radius 1
SimpleCircle() {


}

Upvotes: 0

user2591612
user2591612

Reputation:

The reason you have two SimpleCircle() statements is because the one is the default constructor, which you technically could omit if you changed double radius; to double radius = 1;. The second allows for the developer to pass a parameter to SimpleCircle(double) of type double and assign it to the field called radius. Both SimpleCircle(...) statements are known as constructors.

Sample output of your program:

The are of the circle of radius 1.0 is 3.141592653589793
The are of the circle of radius 25.0 is 1963.4954084936207
The area of the circle of radius 125.0 is 49087.385212340516
The area of the circle of radius 100.0 is 31415.926535897932

You can see in the first sentence, it used the default constructor which sets the radius equal to 1 or 1.0 because it is a double. The rest use the passed in value for radius or the second constructor.

Upvotes: 1

Related Questions