Reputation: 79
I have a class hierarchy which is show below:
public class Rectangle2
{
// instance variables
private int length;
private int width;
/**
* Constructor for objects of class rectangle
*/
public Rectangle2(int l, int w)
{
// initialise instance variables
length = l;
width = w;
}
// return the height
public int getLength()
{
return length;
}
public int getWidth()
{
return width;
}
public String toString()
{
return "Rectangle - " + length + " X " + width;
}
public boolean equals( Object b )
{
if ( ! (b instanceof Rectangle2) )
return false;
Box2 t = (Box2)b;
Cube c = (Cube)b;
return t.getLength() == getLength()
&& t.getWidth() == getWidth()
&& c.getLength() == getLength()
&& c.getWidth() == getWidth() ;
}
}
.
public class Box2 extends Rectangle2
{
// instance variables
private int height;
/**
* Constructor for objects of class box
*/
public Box2(int l, int w, int h)
{
// call superclass
super(l, w);
// initialise instance variables
height = h;
}
// return the height
public int getHeight()
{
return height;
}
public String toString()
{
return "Box - " + getLength() + " X " + getWidth() + " X " + height;
}
public boolean equals( Object b )
{
if ( ! (b instanceof Box2) )
return false;
Rectangle2 t = (Rectangle2)b;
Cube c = (Cube)b;
return t.getLength() == getLength()
&& t.getWidth() == getWidth()
&& c.getLength() == getLength() ;
}
}
.
public class Cube extends Box2 {
public Cube(int length)
{
super(length, length, length);
}
public String toString()
{
return "Cube - " + getLength() + " X " + getWidth() + " X " + getHeight();
}
public boolean equals( Object b )
{
if ( ! (b instanceof Cube) )
return false;
Rectangle2 t = (Rectangle2)b;
Box2 c = (Box2)b;
return t.getLength() == getLength()
&& t.getWidth() == getWidth()
&& c.getLength() == getLength()
&& c.getWidth() == getWidth()
&& c.getHeight() == getHeight() ;
}
}
I created the equals()
method so that when the instances of one class would equals the other it would print some like "The dimensions of this class equals the dimensions of that class." This would be an example: https://i.sstatic.net/Kyyau.png
The only problem is I am not getting that output. Also could I just inherit the equals()
method from the Box2 class when I am doing the equals()
method for the Cube class?
Upvotes: 1
Views: 742
Reputation: 64
Your cast to Cube
inside of Box2.equals()
will fail and throw a ClassCastException
whenever you pass a Box2
that is not also a Cube
. You repeat this error throughout your code.
I recommend fixing the braces of your if
statement, though it should work as expected.
I wouldn't expect you to get any output, actually. You don't have any print()
or println()
calls in your code.
Also, inside Cube.equals()
, you should cast b
to a Cube
, and call each function from the declared Cube
object. You should do the same in nearly any equals(Object)
method.
In your implementation, also, since Rectangle2
and Cube
do not override getWidth()
or getLength()
, t.getWidth()
and c.getWidth()
will call the same functions and thus return the same output every time. Likewise for getLength()
.
For example, your Rectangle2
class should look something like this.
public class Rectangle2 {
private final int length;
private final int width;
public Rectangle2(int length, int width) {
this.length = length;
this.width = width;
}
private int getLength() { return length; }
private int getWidth() { return length; }
public String toString() {
return "Rectangle - "+length+" X "+width;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Rectangle2)) {
return false;
}
final Rectangle2 r = (Rectangle2) o;
return this.getLength() == r.getLength() &&
this.getWidth() == r.getWidth() &&
this.getHeight() == r.getHeight();
}
}
and your Box2
class should look something like this.
public class Box2 extends Rectangle2 {
private final int height;
public Box2(int length, int width, int height) {
super(length, width);
this.height = height;
}
private int getHeight() { return height; }
public String toString() {
return "Box - "+length+" X "+width"+ X "+height;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Box2)) {
return false;
}
final Box2 b = (Box2) o;
return this.getLength() == b.getLength() &&
this.getWidth() == b.getWidth() &&
this.getHeight() == b.getHeight();
}
}
and your Cube
class should look something like this
public class Cube extends Box2 {
private final int height;
public Cube(int length) {
super(length, length, length);
}
public String toString() {
return "Cube - "+length+" X "+width"+ X "+height;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Cube)) {
return false;
}
final Cube c = (Cube) o;
return this.getLength() == c.getLength(); // length == width == height
}
}
You should then be able to add a call to System.out.println()
to print the desired output to the console.
You should declare your fields as final
since they are immutable.
Lastly, if you have other classes with similar names, you should find more meaningful ways to differentiate the class names than numbers. Otherwise, remove the 2
from the names Rectangle2
and Box2
.
Upvotes: 1
Reputation: 4727
You can't put code after the last return
statement. It won't be reachable.
You'll need to assign the "equal" return to a local variable, print it and then return it:
boolean equal = getLength() == getLength()
&& t.getWidth() == getWidth()
&& c.getLength() == getLength()
&& c.getWidth() == getWidth()
&& c.getHeight() == getHeight() ;
if (equal) {
System.out.prinltn("equal");
}
return equal;
And the equals
method is inherited, but you're overriding it. If you want to call the super class equals
method in the body of the extended class, you'll nedd to call super()
on the begining of the overrided method.
public boolean equals( Object b ) {
boolean equal = super.equals(b);
...
Upvotes: 0
Reputation: 3720
In my opinion, your equals methods should look like this (just change classes):
public boolean equals( Object b ) {
if (b instanceof Rectangle2) {
Rectangle2 rectangle2 = (Rectangle2)b;
return this.getLength() == rectangle2.getLength()
&& this.getWidth() == rectangle2.getWidth()
&& this.getHeight() == rectangle2.getHeight();
} else {
return false;
}
}
If you want to print result of this method, assign value to any variable, print it and return that value.
Upvotes: 0