A question about a few variables in a class

Discussion in 'Java' started by Chad, Aug 5, 2011.

  1. Chad

    Chad Guest

    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
    Chad, Aug 5, 2011
    #1
    1. Advertising

  2. Chad

    markspace Guest

    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.
    markspace, Aug 5, 2011
    #2
    1. Advertising

  3. Chad

    Stefan Ram Guest

    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.
    Stefan Ram, Aug 5, 2011
    #3
  4. Chad

    javabudy

    Joined:
    Oct 24, 2010
    Messages:
    13
    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
    javabudy, Aug 6, 2011
    #4
  5. Chad

    Eric Sosman Guest

    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
    Eric Sosman, Aug 6, 2011
    #5
  6. Chad

    Roedy Green Guest

    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.
    Roedy Green, Aug 7, 2011
    #6
  7. Chad

    Lew Guest

    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/docs/api/java/lang/Comparable.html

    --
    Lew
    Lew, Aug 10, 2011
    #7
  8. Chad

    Chad Guest

    On Aug 10, 9:14 am, Lew <> 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
    Chad, Aug 13, 2011
    #8
  9. Chad

    Chad Guest

    On Aug 6, 4:56 am, Eric Sosman <> 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
    Chad, Aug 13, 2011
    #9
  10. Chad

    Eric Sosman Guest

    On 8/13/2011 1:30 PM, Chad wrote:
    > On Aug 6, 4:56 am, Eric Sosman<> 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
    Eric Sosman, Aug 14, 2011
    #10
  11. Chad

    Chad Guest

    On Aug 13, 5:49 pm, Eric Sosman <> wrote:
    > On 8/13/2011 1:30 PM, Chad wrote:
    >
    > > On Aug 6, 4:56 am, Eric Sosman<>  wrote:
    > >>[...]
    > >>       We don't actually know what Rectangle is because there areno
    > >> 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?
    >


    Below is all 5 million lines of code from the book....

    public class GeometricObject1 {
    private String color = "white";
    private boolean filled;
    private java.util.Date dateCreated;

    public GeometricObject1() {
    dateCreated = new java.util.Date();
    }

    public GeometricObject1(String Color, boolean filled) {
    dateCreated = new.java.util.Date();
    this.color = color;
    this.filled = filled;
    }

    public String getColor() {
    return color;
    }

    public void setColor(String color) {
    this.color = color;
    }

    public boolean isFilled() {
    return filled;
    }

    public void setFilled(boolean filled) {
    this.filled = filled;
    }

    public java.util.Date getDateCreated() {
    return dateCreated;
    }

    public String toString() {
    return "created on " + dateCreated + "\ncolor" + color +
    " and filled" + filled;
    }
    }

    //Rectangle1 isn't a typo
    public class Rectangle1 extends GeometricObject1 {
    private double width;
    private double height;

    public Rectangle1() {
    }

    public Rectangle1(double width, double height, String color,
    boolean filled) {
    this.width = width;
    this.height = height;
    setColor(oolor);
    setFilled(filled);
    }

    public double getWidth() {
    return width;
    }

    public void setWidth(double width) {
    this.width = width;
    }

    public double getHeight() {
    return height;
    }

    public double setHeight(double height) {
    this.height = height;
    }

    public double getArea() {
    return width * height;
    }

    public double getPerimeter() {
    return 2 * (width + height);
    }
    }
    Chad, Aug 14, 2011
    #11
  12. Chad

    markspace Guest

    On 8/14/2011 1:23 PM, Chad wrote:
    > public Rectangle1(double width, double height, String color,
    > boolean filled) {


    Method parameters.


    > this.width = width;
    > this.height = height;


    Instance fields on the left of the assignment. (Note "data fields"
    isn't used in any Java documentation I can recall.)

    > setColor(oolor);
    > setFilled(filled);


    Method invocation, in the super class these methods set more instance
    fields.
    markspace, Aug 14, 2011
    #12
  13. Chad

    Eric Sosman Guest

    On 8/14/2011 4:23 PM, Chad wrote:
    > On Aug 13, 5:49 pm, Eric Sosman<> wrote:
    >> On 8/13/2011 1:30 PM, Chad wrote:
    >>
    >>> On Aug 6, 4:56 am, Eric Sosman<> wrote:
    >>>> [...]
    >>>> We don't actually know what Rectangle is because there are no
    >>>> import statements to tell us what package it's in.[...]

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

    >
    > Below is all 5 million lines of code from the book....
    >[...]
    > //Rectangle1 isn't a typo
    > public class Rectangle1 extends GeometricObject1 {


    Not a typo, fine. Also still not `Rectangle' ...

    Chad, if you ever expect to make any progress in programming
    you will have to learn a few hard truths about exactness. First, of
    course, is that the computer will do exactly what you tell it to --
    and the whole huge activity called "debugging" amounts to exploring
    the gap between what was said and what was meant. That being the
    case, it is counterproductive in the extreme to widen the gap
    gratuitously, as you have done in this thread by persisting in
    sloppiness. You are just making things harder, unnecessarily harder,
    and people may tire of working unnecessarily hard to help you.

    By the way, the status of the thread's question is just as it
    was nine days ago: "Insufficient information to support an answer."
    Not much progress for nine days, is it?

    --
    Eric Sosman
    d
    Eric Sosman, Aug 14, 2011
    #13
  14. Chad

    Eric Sosman Guest

    On 8/14/2011 4:23 PM, Chad wrote:
    > On Aug 13, 5:49 pm, Eric Sosman<> wrote:
    >> On 8/13/2011 1:30 PM, Chad wrote:
    >>
    >>> On Aug 6, 4:56 am, Eric Sosman<> wrote:
    >>>> [...]
    >>>> We don't actually know what Rectangle is because there are no
    >>>> import statements to tell us what package it's in.[...]

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

    >
    > Below is all 5 million lines of code from the book....
    >[...]
    > //Rectangle1 isn't a typo
    > public class Rectangle1 extends GeometricObject1 {


    Not a typo, fine. Also still not `Rectangle' ...

    Chad, if you ever expect to make any progress in programming
    you will have to learn a few hard truths about exactness. First, of
    course, is that the computer will do exactly what you tell it to --
    and the whole huge activity called "debugging" amounts to exploring
    the gap between what was said and what was meant. That being the
    case, it is counterproductive in the extreme to widen the gap
    gratuitously, as you have done in this thread by persisting in
    sloppiness. You are just making things harder, unnecessarily harder,
    and people may tire of working unnecessarily hard to help you.

    By the way, the status of the thread's question is just as it
    was nine days ago: "Insufficient information to support an answer."
    Not much progress for nine days, is it?

    --
    Eric Sosman
    d
    Eric Sosman, Aug 14, 2011
    #14
  15. Chad

    Chad Guest

    On Aug 14, 3:04 pm, Eric Sosman <> wrote:
    > On 8/14/2011 4:23 PM, Chad wrote:
    >
    > > On Aug 13, 5:49 pm, Eric Sosman<>  wrote:
    > >> On 8/13/2011 1:30 PM, Chad wrote:

    >
    > >>> On Aug 6, 4:56 am, Eric Sosman<>    wrote:
    > >>>> [...]
    > >>>>        We don't actually know what Rectangle is because there are no
    > >>>> import statements to tell us what package it's in.[...]

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

    >
    > > Below is all 5 million lines of code from the book....
    > >[...]
    > > //Rectangle1 isn't a typo
    > > public class Rectangle1 extends GeometricObject1 {

    >
    >      Not a typo, fine.  Also still not `Rectangle' ...
    >
    >      Chad, if you ever expect to make any progress in programming
    > you will have to learn a few hard truths about exactness.  First, of
    > course, is that the computer will do exactly what you tell it to --
    > and the whole huge activity called "debugging" amounts to exploring
    > the gap between what was said and what was meant.  That being the
    > case, it is counterproductive in the extreme to widen the gap
    > gratuitously, as you have done in this thread by persisting in
    > sloppiness.  You are just making things harder, unnecessarily harder,
    > and people may tire of working unnecessarily hard to help you.
    >


    I'd like to believe I've more progress, than say, a few unnamed people
    that keep posting questions in comp.lang.c.

    >      By the way, the status of the thread's question is just as it
    > was nine days ago: "Insufficient information to support an answer."
    > Not much progress for nine days, is it?
    >


    Well, I believe that I've gone on record in the past as saying that
    I'm kind of slow. I'm not ashamed of the fact that I got flat out
    rejected by both UCLA and UC Berkeley on numerous occassions. I'm fine
    with the fact that I have to read a section in the school book
    multiple times and ask questions before anything actually sinks in. I
    know there are people in this program that don't have to study and
    still manage to get A's. I'm not one of them.

    Chad
    Chad, Aug 15, 2011
    #15
  16. Chad

    Eric Sosman Guest

    On 8/14/2011 8:09 PM, Chad wrote:
    >[...]
    > Well, I believe that I've gone on record in the past as saying that
    > I'm kind of slow. I'm not ashamed of the fact that I got flat out
    > rejected by both UCLA and UC Berkeley on numerous occassions. I'm fine
    > with the fact that I have to read a section in the school book
    > multiple times and ask questions before anything actually sinks in. I
    > know there are people in this program that don't have to study and
    > still manage to get A's. I'm not one of them.


    All the more reason to cultivate discipline, to make almost
    a fetish of exactness. It may or may not limit the scope of your
    creativity, but it *will* limit the scope of your mistakes. I've
    made a pretty successful career out of being a careful non-genius;
    you can do so too. But "careful" is the word, "meticulous" is
    the byword, "slapdash" is the word you must reject.

    --
    Eric Sosman
    d
    Eric Sosman, Aug 15, 2011
    #16
  17. Chad

    Lew Guest

    markspace wrote:
    > Chad wrote:
    >> public Rectangle1(double width, double height, String color,
    >> boolean filled) {

    >
    > Method parameters.


    Constructor parameters, not method parameters.

    >> this.width = width;
    >> this.height = height;

    >
    > Instance fields on the left of the assignment. (Note "data fields"
    > isn't used in any Java documentation I can recall.)


    Also called instance attributes, although that term is more apt for the methods that cover them, or for public instance fields.

    Another name is instance member fields.

    >> setColor(oolor);
    >> setFilled(filled);

    >
    > Method invocation, in the super class these methods set more instance
    > fields.


    Note that the use of non-final member methods in a constructor of the same class is dangerous.

    --
    Lew
    Lew, Aug 15, 2011
    #17
  18. Chad

    Lew Guest

    Chad wrote:
    > Eric Sosman wrote:
    >>>[...]
    >>> //Rectangle1 isn't a typo
    >>> public class Rectangle1 extends GeometricObject1 {

    >>
    >>      Not a typo, fine.  Also still not `Rectangle' ...
    >>
    >>      Chad, if you ever expect to make any progress in programming
    >> you will have to learn a few hard truths about exactness.  First, of
    >> course, is that the computer will do exactly what you tell it to --
    >> and the whole huge activity called "debugging" amounts to exploring
    >> the gap between what was said and what was meant.  That being the
    >> case, it is counterproductive in the extreme to widen the gap
    >> gratuitously, as you have done in this thread by persisting in
    >> sloppiness.  You are just making things harder, unnecessarily harder,
    >> and people may tire of working unnecessarily hard to help you.

    >
    > I'd like to believe I've more progress, than say, a few unnamed people
    > that keep posting questions in comp.lang.c.


    So the eff what? How does that influence the price of gefulte fish in Beijing?

    >>      By the way, the status of the thread's question is just as it
    >> was nine days ago: "Insufficient information to support an answer."
    >> Not much progress for nine days, is it?

    >
    > Well, I believe that I've gone on record in the past as saying that
    > I'm kind of slow. I'm not ashamed of the fact that I got flat out
    > rejected by both UCLA and UC Berkeley on numerous occassions. I'm fine
    > with the fact that I have to read a section in the school book
    > multiple times and ask questions before anything actually sinks in. I
    > know there are people in this program that don't have to study and
    > still manage to get A's. I'm not one of them.


    Look, no one is saying anyone has to be a genius or a straight-A student, but to get all defensive like that and whine about not being smart instead of taking the coaching is a boneheaded stupid reaction. If you cannot take advice to improve your skills, but instead make pathetic excuses that are utterly irrelevant and do yourself a disservice to boot, why do you even bother asking for help?

    Go back and read Eric's advice again and follow it, and get your ego out ofthe way or get out of programming.

    --
    Lew
    Lew, Aug 15, 2011
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,742
    Thomas Weidenfeller
    Oct 12, 2005
  2. Luc The Perverse

    Few Image class (and related) questions

    Luc The Perverse, Nov 6, 2005, in forum: Java
    Replies:
    3
    Views:
    347
    Luc The Perverse
    Nov 6, 2005
  3. Murali
    Replies:
    2
    Views:
    559
    Jerry Coffin
    Mar 9, 2006
  4. Replies:
    9
    Views:
    936
  5. Eric D.
    Replies:
    3
    Views:
    179
    Jeremy Henty
    Feb 1, 2006
Loading...

Share This Page