Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > A question about a few variables in a class

Reply
Thread Tools

A question about a few variables in a class

 
 
Chad
Guest
Posts: n/a
 
      08-05-2011
Let's say I have the following class.....


public class ComparableRectangle extends Rectangle implements
Comparable {
public ComparableRectangle(double width, double height) {
super(width, height);
}

public int compareTo(Object o) {
if (getArea() > ((ComparableRectangle)o).getArea())
return 1;
else if (getArea() < ((ComparableRectangle)o).getArea())
return -1;
else
return 0;
}
}


Are 'width' and 'height' data fields in this class? My initial guess
is yes. However, the fact that they are passed to super() makes me
wonder otherwise.

Chad
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      08-05-2011
On 8/5/2011 2:31 PM, Chad wrote:

> Are 'width' and 'height' data fields in this class?


My guess: no. They're method parameters, not fields.



 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      08-05-2011
Chad <> writes:
>public ComparableRectangle(double width, double height) {
>Are 'width' and 'height' data fields in this class?


In the scope where these two identifiers appear above, they
refer to parameters. Whether there are inherited fields with
the same name is unknown.

 
Reply With Quote
 
javabudy javabudy is offline
Junior Member
Join Date: Oct 2010
Posts: 13
 
      08-06-2011
calling super(width, height); suggest that class Rectangle could have a constructor which accept two parameters but nothing can be deducted about field with this much information.

Javin
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-06-2011
On 8/5/2011 5:31 PM, Chad wrote:
> Let's say I have the following class.....
>
>
> public class ComparableRectangle extends Rectangle implements
> Comparable {
> public ComparableRectangle(double width, double height) {
> super(width, height);
> }
>
> public int compareTo(Object o) {
> if (getArea()> ((ComparableRectangle)o).getArea())
> return 1;
> else if (getArea()< ((ComparableRectangle)o).getArea())
> return -1;
> else
> return 0;
> }
> }
>
> Are 'width' and 'height' data fields in this class? My initial guess
> is yes. However, the fact that they are passed to super() makes me
> wonder otherwise.


Looking only at the snippet shown, it's impossible to answer
the question. The ComparableRectangle class itself has no members
named width or height. It might (or might not) inherit such
members from Rectangle or from a superclass of Rectangle. All we
can be sure of is (1) Rectangle has a constructor taking two
double arguments, and (2) Rectangle or a Rectangle ancestor has
a getArea() method returning a primitive number of some kind.

We don't actually know what Rectangle is because there are no
import statements to tell us what package it's in. It clearly
cannot be java.awt.Rectangle, which has public width and height
members but which has no getArea() and no suitable constructor.

--
Eric Sosman
d
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-07-2011
On Fri, 5 Aug 2011 14:31:30 -0700 (PDT), Chad <>
wrote, quoted or indirectly quoted someone who said :

>Are 'width' and 'height' data fields in this class? My initial guess
>is yes


they get passed to super. Most likely the super class will have some
width and height variable with may or may not be private. But there
is nothingh that says super has no save the values. It might just
display them on the console or some such, or compute an area and save
that.

To solve this mystery, you must look at the code of the superclass.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Most of computer code is for telling the computer
what do if some very particular thing goes wrong.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      08-10-2011
Chad wrote:
> Let's say I have the following class.....
>
> public class ComparableRectangle extends Rectangle implements
> Comparable {


Don't use raw types.

> public ComparableRectangle(double width, double height) {
> super(width, height);
> }
>
> public int compareTo(Object o) {
> if (getArea() > ((ComparableRectangle)o).getArea())


Don't cast 'o' because it will be of the correct type if you declare your class correctly, which you didn't.

public int compareTo( ComparableRectangle o )
...

See
http://download.oracle.com/javase/6/...omparable.html

--
Lew
 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      08-13-2011
On Aug 10, 9:14*am, Lew <lewbl...@gmail.com> wrote:
> Chad wrote:
> > Let's say I have the following class.....

>
> > public class ComparableRectangle extends Rectangle implements
> > Comparable {

>
> Don't use raw types.
>
> > * public ComparableRectangle(double width, double height) {
> > * * super(width, height);
> > * }

>
> > * public int compareTo(Object o) {
> > * * if (getArea() > ((ComparableRectangle)o).getArea())

>
> Don't cast 'o' because it will be of the correct type if you declare yourclass correctly, which you didn't.
>
> * public int compareTo( ComparableRectangle o )
> * ...
>
> Seehttp://download.oracle.com/javase/6/docs/api/java/lang/Comparable.html
>


For reasons that elude me, the web based browser for google groups
wouldn't display anything after Aug. 1 until a few days ago. With
that, the example was taken from the book we use at school.

Chad
 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      08-13-2011
On Aug 6, 4:56*am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> On 8/5/2011 5:31 PM, Chad wrote:
>
>
>
>
>
> > Let's say I have the following class.....

>
> > public class ComparableRectangle extends Rectangle implements
> > Comparable {
> > * *public ComparableRectangle(double width, double height) {
> > * * *super(width, height);
> > * *}

>
> > * *public int compareTo(Object o) {
> > * * *if (getArea()> *((ComparableRectangle)o).getArea())
> > * * * *return 1;
> > * * *else if (getArea()< *((ComparableRectangle)o).getArea())
> > * * * *return -1;
> > * * *else
> > * * * *return 0;
> > * *}
> > }

>
> > Are 'width' and 'height' data fields in this class? My initial guess
> > is yes. However, the fact that they are passed to super() makes me
> > wonder otherwise.

>
> * * *Looking only at the snippet shown, it's impossible to answer
> the question. *The ComparableRectangle class itself has no members
> named width or height. *It might (or might not) inherit such
> members from Rectangle or from a superclass of Rectangle. *All we
> can be sure of is (1) Rectangle has a constructor taking two
> double arguments, and (2) Rectangle or a Rectangle ancestor has
> a getArea() method returning a primitive number of some kind.
>
> * * *We don't actually know what Rectangle is because there are no
> import statements to tell us what package it's in. *It clearly
> cannot be java.awt.Rectangle, which has public width and height
> members but which has no getArea() and no suitable constructor.
>



Aye. I broke away from my own personal rule of posting code snippets.
I should know better since it really annoys me when people post code
snippets on comp.lang.c, and then, like magic, expect the group to be
able to magically interpet their line of thought.


Chad
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-14-2011
On 8/13/2011 1:30 PM, Chad wrote:
> On Aug 6, 4:56 am, Eric Sosman<esos...@ieee-dot-org.invalid> wrote:
>>[...]
>> We don't actually know what Rectangle is because there are no
>> import statements to tell us what package it's in. It clearly
>> cannot be java.awt.Rectangle, which has public width and height
>> members but which has no getArea() and no suitable constructor.

>
> Aye. I broke away from my own personal rule of posting code snippets.
> I should know better since it really annoys me when people post code
> snippets on comp.lang.c, and then, like magic, expect the group to be
> able to magically interpet their line of thought.


Just out of curiosity, what *is* the `Rectangle' you used?

--
Eric Sosman
d
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet connects for a few seconds then disconnects for a few sec Cody Wireless Networking 2 06-28-2009 08:24 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
question about class variables and instance variables Eric D. Ruby 3 02-01-2006 07:57 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments