-2

I making a class and for some reason there is the error "cannot find symbol" for

perimeter = width + length;
return perimeter;

I am not sure why an error should occur here (or whether there is something else wrong with my code. I just started java in school so any tips would be helpful.

 /**
 * A rectangle has a length and a width.  Its perimeter can be calculated.
 */
 public class Rectangle
{
private int length;
private int width;

/**
 * Constructs a rectangle with a specified length and width
 * @param len the length of the rectangle
 * @param wid the width of the rectangle
 */
public Rectangle(int len, int wid )
{
    length = 0;
    width = 0;
}

/**
 * Sets the length and width of the rectangle
 * @param len the new length
 * @param wid the new width
 */
public void setDimensions(int len, int wid)
{
    length = len;
    width = wid;
}

/**
 * Returns the perimeter of the rectangle
 * @return the perimeter of the rectangle
 */
public int calculatePerimeter( )
{
    perimeter = width + length;
    return perimeter;
}
Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Nimitz
  • 3
  • 4

1 Answers1

3

perimeter can't be found there because it hasn't been declared yet.

To declare a variable, you need to specify the type of it, and then its name.

So, for example, do...

int perimeter = width + length;
return perimeter;
Archer
  • 31
  • 3