1

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
asked Feb 27, 2014 at 21:17

2 Answers 2

1

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.

answered Feb 27, 2014 at 21:20
Sign up to request clarification or add additional context in comments.

5 Comments

@javaaaaaa: Your question doesn't make sense. There is no instance of radius per se.
i mean why couldnt i just use radius throughout the SimpleCircle class? why did the book create radius = newRadius; ??
All variables are created for each instance, the methods exists only once, but can modify all variables in the instance you are working on.
In the constructor you have one variable as input. SimpleCircle(double newRadius) { then the next line radius = newRadius; assigns the value to the internal variabel instance
The declaration of radius at the beginning of class SimpleCircle means that each SimpleCircle will have a radius. Notice that that is the only time where radius is preceded by double in the class. That is setting aside memory for a double in every instance of the class. The lines radius = 1 or radius = newRadius are assigning a value to the radius. These lines populate that memory with the value in should have in that particular instance of SimpleCircle.
0

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() {
}
answered Feb 27, 2014 at 21:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.