max_j_r
max_j_r

Reputation: 25

MissingPropertyException error (groovy)

I need to write a program that reads the X and Y coordinates of two points and then outputs the area and perimeter of a rectangle where both points are opposite corners. I however get this error message

groovy.lang.MissingPropertyException: No Such property : x for class: rectangle.

Could anybody please help explain what is going wrong here?

    // First point
Point point1 = new Point()
print "enter first x co-ordinate: "
point1.x = Double.parseDouble(System.console().readLine())
print "enter first y co-ordinate: "
point1.y = Double.parseDouble(System.console().readLine())

// Second point
Point point2 = new Point()
print "enter second x co-ordinate: "
point2.x = Double.parseDouble(System.console().readLine())
print "enter second y co-ordinate: "
point2.y = Double.parseDouble(System.console().readLine())

// Create Rectangle
Rectangle myRectangle = new Rectangle()
myRectangle.upLeft = point1
myRectangle.downRight = point2

// Calculate Perimeter 
double width = myRectangle.downRight.x - myRectangle.upLeft.x
double height = myRectangle.upLeft.y - myRectangle.downRight.y
double perimeter = 2 * (width + height)

// Calculate Area
double area = width x height 

println "Perimeter is " + perimeter
println "Area is " + area


class Point {
    double x
    double y
}

class Rectangle {
    Point upLeft
    Point downRight
}

Upvotes: 0

Views: 151

Answers (2)

Opal
Opal

Reputation: 84854

You've used x instead of * in the following line:

double area = width x height 

should be:

double area = width * height 

Anyways script runs correctly.

Upvotes: 1

Gaël J
Gaël J

Reputation: 15275

The error explains itself :

No Such property : x for class: rectangle.

You are using a variable of type Rectangle and asking for property x but it doesn't exist. It's Point class which have this property.

Upvotes: 0

Related Questions