A question about a few variables in a class

C

Chad

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
 
S

Stefan Ram

Chad said:
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.
 
Joined
Oct 24, 2010
Messages
13
Reaction score
0
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
 
E

Eric Sosman

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.
 
R

Roedy Green

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.
 
L

Lew

Chad said:
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
 
C

Chad

Don't use raw types.



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
 
C

Chad

     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
 
E

Eric Sosman

[...]
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?
 
C

Chad

[...]
      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);
}
}
 
M

markspace

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.
 
E

Eric Sosman

[...]
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?
 
E

Eric Sosman

[...]
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?
 
C

Chad

On 8/13/2011 1:30 PM, Chad 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
 
E

Eric Sosman

[...]
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.
 
L

Lew

markspace said:
Method parameters.

Constructor parameters, not method parameters.
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.
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.
 
L

Lew

Chad said:
Eric said:
[...]
//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?
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top