hi people, I am a newbie to Java OOP,i cam across this example in a text book, but i just cldn't understand how the area is calculated.

hope that someone can explain this simple problem to me

the code is:

public class TestPassingObject
{
// Main method
public static void main ( String [ ] args )
{
// Create a Circle object with default radius 1
Circle myCircle = new Circle ( );


// Print areas for radii 1, 2, 3, 4 and 5
int n = 5;
printAreas ( myCircle, n );


// See myCircle.radius and times
System.out.println ( " \n " + " Radius is " + myCircle.radius );
System.out.println ( " n is " + n );
}


// Print a table of areas for radius
public static void printAreas ( Circle c, int times )
{
System.out.println ( " Radius \t \t Area " );
while ( times >= 1)
{
System.out.println ( c.radius + " \t \t " + c.findArea ( ) );
c.radius ++;
times--;
}
}
}


and the results are :


Radius       Area


1.0          3.14159


2.0          12.56636


3.0          28.27431


4.0          50.26544


5.0          78.53975



Radius is 6.0


n is 5

thanks in advance : )

Recommended Answers

All 9 Replies

The area is found when then findArea() method is called on the circle object. Area of a circle is its radius * Pi.

So, the Cicle object most likely takes it's radius (that must be hard coded to be set at 1 when a new instance of Circle is created) and multiplies it by Pi.

On a side note, i'm not sure if i'm a fan of the radius attribute of the Circle being public. There should be a setter and a getter for the Circle's radius attribute.

Regards,

Nate

hi Nate,

thanks for the reply...i have another question tat is, since i did not defined the formulae for the calculation of the area, didi the method do the calculation on its own? i mean, is there a defined formulae for the calculation of the area?

rgds,
tris

Yes, there is definently a formula that was called. If you have the code, look in the Circle.java file. You should see the formula in the findArea() method. If you don't have the Circle.java file and only have the Circle.class file then download a tool called DJ (a java decompiler) and open the Circle.java file with that tool. It will show you the code for the findArea() method.

Regards,

Nate

thanks, that was what i could not fiigure out initially.right now, i am using bluej as my compiler for java. do u think it has the Circle.java file?

rgds,
tris

Well, something has to. Post your codes import statements. If your code doesn't have any import statements then the Circle.class file and hopefully the Circle.java file will be in the same directory as your TestPassingObject.java file.

Also, if you could post your classpath that would be splended. You can find your classpath by opening a command prompt window and typing set classpath. Then, you will also want to try and find your classpath inside of bluej. I'm not exactly sure how this is done. Did the book you're reading ask you to download any files from the web? If so, which ones?

Regards,

Nate

Area of a circle is its radius * Pi.

Just saw some ones elses code for a circle and found that the area of a circle is it's radius squared times pi. I'm an idiot.

Warmest Regards,

Nate

hi Nate,

yup, i can find a Circle.class file in the same project folder as the TestPassingObject.java file. but i still dun understand what the class file is for? does it contain some predefined code or statement so that whenever i want to calculate the area of a circle, this .class file will be generated?

rgds,
tris

the Circle.class file is the compiled version of the Circle.java file. One way to think about is... .java files are the human readable code while the .class files are created from the .java files for computer readability.

So, if you open Circle.class with notepad, you're get a bunch of garbily gook. But, if you open it with DJ Decompiler like mentioned above, you'll get the human readable code again.

Regards,

Nate

hi Nate,

Thanks for the quick reply, really appreciate. I'm getting some idea about it. Will con't to work on it and post more questions so that people can learn as i learn. Hopefully, i can discuss more questions with you in the future. Nice knowing you :cheesy:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.