Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

I have some problems with manipulating an object.

Thread view: 
mohed.haidar@gmail.com - 31 Dec 2007 22:55 GMT
Hello guys, happy new year.
 Well, a very basic question. I make a class objekt that lookes
something like this :

public class circle {

 public static final PI = 3.14159;
 public double r;
 public double area(double r) {
       return r*r*PI;
 }
}

and then from somewhere else I do :

circle.r = 5;
double area = circle.area();  <--- this gives me a compilation error,
it wants me to specify circle.area(5). I thought

that it was legitamet code to leave it at (). Is'nt it what object
oriented code was all about, i have already defined the objects
variable r to bee 5, shouldent need to further specify
circle.area(5).
 Thank you for any response in advance.
Owen Jacobson - 01 Jan 2008 00:02 GMT
On Dec 31, 2:55 pm, mohed.hai...@gmail.com wrote:
> Hello guys, happy new year.
>   Well, a very basic question. I make a class objekt that lookes
[quoted text clipped - 4 lines]
>   public static final PI = 3.14159;
>   public double r;

This defines a member variable named 'r'.

>   public double area(double r) {

This defines a method taking a parameter named 'r', unrelated to the
member variable.  You probably meant
 public double area () {
or more conventionally
 public double getArea () {

>         return r*r*PI;
>   }
[quoted text clipped - 11 lines]
> variable r to bee 5, shouldent need to further specify
> circle.area(5).

In Java, local variable names and parameter names hide member
variables with the same name.  The following program is exactly
equivalent to your original program, but illustrates why you get an
error:

public class circle {
 public static final PI = 3.14159;
 public double r;
 public double area(double r2) {
       return r2*r2*PI;
 }
}

-o
Patricia Shanahan - 01 Jan 2008 00:12 GMT
> On Dec 31, 2:55 pm, mohed.hai...@gmail.com wrote:
>> Hello guys, happy new year.
[quoted text clipped - 4 lines]
>>
>>   public static final PI = 3.14159;

In addition to Owen's comments, PI needs a type. On the other hand, you
should probably get rid of it. Math.PI is a far closer approximation to pi.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.