Brandon
Brandon

Reputation: 27

Cannot find symbol in method changeRecL but declared in main method

I am a new programmer and i am getting the error cannot find symbol in my ShapeApp class. My error is

System.out.println("Current length of rectangle is: " + r1.getLength()); ^ symbol: variable r1 location: class ShapeApp

Please try and explain it in a simpler way. Thank you very much and my codes are below.

public class Rectangle
{
protected double length;
protected double width;

public Rectangle() {  }
public Rectangle(double l,double w)
{
   length = l;
   width = w;
}
public void setLength(double l) {length = l;}
public double getLength() {return length;}

public void setWidth(double w) {width = w;}
public double getWidth() {return width;}

public double findArea() {return length * width;}

public String toString() 
{
   return "\tLength " + length + "\tWidth " + width;
}
}



public class Box extends Rectangle
{
private double height;

public Box() {   }
public Box(double l,double w,double h)
{
   super(l,w); 
   height = h;
}
public void setHeight(double h) {height = h;}
public double getHeight() {return height;}

public double findArea() {return ((super.findArea() * 2) + (2 * height * width) + (2 * height * length));}
public double findVolume() {return super.findArea() * height;}

public String toString()
{
return super.toString() + "\tHeight " + height;
}
}





import java.util.*;

public class ShapeApp
{
   public static void main(String[] args)
   { 
   Rectangle r1 = new Rectangle(20,10);
   Box b1 = new Box(10,5,5); 

   int options;

   Scanner input = new Scanner(System.in);

   do{
      displayMenu();
      options = input.nextInt();

            switch(options)
            {
            case 1: changeRecL();
                    break;
            case 2: changeBoxL();
                    break;
            case 3: changeBoxH();
                    break; 
            case 4: displayAreaRec();
                    break;
            case 5: displaySaBox();
                    break; 
            case 6: displayVoBox();
                    break;            
            case 0: System.out.println("Exiting Program");
                    break;
            default: System.out.println("Invalid Option. ");
            }
      }while(options != 0);    
   }

   public static void displayMenu()
   {
   System.out.println("-------------------------------MENU-------------------------------");
   System.out.println("[1] Change the length of rectangle");
   System.out.println("[2] Change the length of box");
   System.out.println("[3] Change the height of box");
   System.out.println("[4] Display the area of rectangle");
   System.out.println("[5] Display the surface area of box");
   System.out.println("[6] Display the volume of box");
   System.out.println("[0] Exit");
   System.out.println("------------------------------------------------------------------");   
   System.out.println("Enter your option:");
   }

   public static void changeRecL()
   {
   Scanner input = new Scanner(System.in);

   System.out.println("Current length of rectangle is: " + r1.getLength());
   System.out.println("Enter new length of rectangle: ");

   double nlength = input.nextDouble();

   r1.setLength(nlength);
   }

   public static void changeBoxL()
   {

   }

   public static void changeBoxH()
   {

   }

   public static void displayAreaRec()
   {

   }

   public static void displaySaBox()
   {

   }

   public static void displayVoBox()
   {

   }

}

Upvotes: 1

Views: 1050

Answers (2)

Eduardo Soriano
Eduardo Soriano

Reputation: 192

When you define an object class you can make several instances of the class as objects. There's not only one Rectangle r1. If you want to use only one rectangle for everything you should implement a Singleton class.

public class Rectangle {
   double len;
   double wid;
   private static Rectangle instance = null;
   protected Rectangle() {
      // Exists only to defeat instantiation.
   }
   public static Rectangle getInstance() {
      if(instance == null) {
         instance = new Rectangle();
      }
      return instance;
   }
public static void setLen (double l){
      len = l;
}
public static double getLen (){
      return len;
    }
public static void setWid (double w){
      wid = w;
    }
public static double getWid(){
      return wid;
    }
}

But, like @almas_shaikh said, it's easier to pass the object instance to the method.

Also, let me remind you that the word length is used by java to determine size of arrays and other objects, as getLength() method. You should use another name to length attribute and method to avoid conflicts.

Upvotes: 0

SMA
SMA

Reputation: 37083

That's because you havent defined r1 in your method changeRecL.

Perhaps you wanted to pass that r1 from main to your method like below:

case 1: changeRecL(r1);

And accept R1 as below in the same:

public static void changeRecL(Rectangle r1)

Upvotes: 1

Related Questions