Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am facing an issue executing this code, I am not getting Output, can someone help me correct where I am making mistake.

I have read this article to understand the issue but the examples is differ from the one that I was trying.

Thanks!

What I have tried:

Here is the code that I have tried:

public class RectangleDemo
{
   public static void main(String[] args)
   {
      // Create a Rectangle object with default values
      Rectangle rectangle1 = new Rectangle();

      System.out.println("Area of Rectangle is "
                         + rectangle1getArea());
      System.out.println("Perimeter of Rectangle is "
                         + rectangle1getPerimeter());

      // Create a Rectangle object with given set of values
      Rectangle rectangle2 = new Rectangle(3.5, 4.2);

      System.out.println("Area of Rectangle is "
                         + rectangle2getArea());
      System.out.println("Perimeter of Rectangle is "
                         + rectangle2getPerimeter());
   }
}
Posted
Updated 28-Jan-22 3:30am
Comments
Tony Hill 28-Jan-22 9:43am    
Are you using a custom Rectangle class?

Because the standard java class does not have an overloaded constructor that takes doubles as parameters, neither does it have methods called getArea() or getPerimeter.

If you are using a custom class it might be helpful to add that class definition to your question to help us solve your problem.

1 solution

You need to separate the variable name from the method call:
Java
System.out.println("Area of Rectangle is " + rectangle1getArea());
Needs to be
Java
System.out.println("Area of Rectangle is " + rectangle1.getArea());
and so forth.

Without the separation, the system is looking for a method called rectangle1getArea instead of a method called getArea in the Rectangle class.
With it, the system calls the right method, using the right instance of the class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900