Thahrim Ahmed Thahrim Ahmed - 1 month ago 19
Java Question

java: cannot find symbol; symbol: variable length

I apologize if this is too beginner of a question. I understand that some how my [ Object o ] is out of scope, that is why it cannot find the symbol.

However, I can't seem to fix the problem. I'm completely unsure of where it went wrong. I believe this is the last problem in my Assignment, and it's due midnight.

Any help would be greatly appreciated :)

public class Square implements Shape
{
double length;

public Square(double length)
{
this.length = length;
}

@Override
public String toString()
{
return "Square";
}

@Override
public boolean equals(Object o)
{
if (o == null)
return false;
if(this.getClass() != o.getClass())
return false;
if (o.length == this.length)
return true;
else
return false;
}

@Override
public int hashCode()
{
return (int)length % 50;
}
public double getPerimeter()
{
return length * 4;
}
}



C:\Users\RimZ\IdeaProjects\Assignment1\src\Square.java

Error:(24, 22) java: cannot find symbol
symbol: variable length
location: variable o of type java.lang.Object


if (o.*length* == this.length)
return true;


^ thats where the problem is

Answer

You need to cast your Object into a Square:

Square otherSquare = (Square) o;
...
if (otherSquare.length == this.length)
    return true;