I have to create a quadrilateral program for school where you put in two points and find the area. For the Public double getArea() part it says "return type double is not compatible with void" How do I fix it?

Main class

public class Point {
    double x;
    double y;

public Point(double pX, double pY){
//   this.x = x;
//   this.y = y;
    x = pX;
    y = pY;
}
public void setX (double pX){
//    this.x = x;
    x = pX;
}
public void setY (double pY){
//    this.y = y;
    y = pY;
}
public double getX(){
    return x;
}
public double getY(){
    return y;
}
    @Override
public String toString(){
    return String.format("(" + x + "," + y + ")");
}
}

Quadrilateral class

Trapezoid(double ptX,double ptY){
super(ptX,ptY);
}

public double getArea(){
    return (x*y)/2;
}

    @Override
public String toString(){
 return String.format ("Trapezoid coords: " + "(" + x + "," + y + ")",
         "Area: " + getArea());

Dani AI

Generated

The compiler message "return type double is not compatible with void" is a clear sign of an overriding mismatch: a superclass (or interface) defines a method named getArea() that returns void, while the subclass defines getArea() returning double. Java does not allow changing primitive return types when overriding. In the posted snippets the Trapezoid constructor calls super(ptX,ptY), which implies Trapezoid extends Point — a modeling error that also explains the compile message. As pointed out, a quadrilateral is composed of points, not a subtype of a single Point.

Practical, low-risk fixes:

  • Use composition: make Trapezoid (or a Quadrilateral) hold four Point objects (or an array/list of vertices). Remove extends Point. Compute area from those vertices.
  • Or build a small shape hierarchy: create abstract class Shape { public abstract double getArea(); } and have concrete shapes implement double getArea() so signatures match.
  • If the superclass really meant a printing method, rename it (e.g., printArea()) rather than change return types.

For a reliable area for any simple quadrilateral, use the shoelace formula: iterate the vertex pairs and return half the absolute cross-sum. Example getArea() implementation (assumes pts[0..3] are ordered clockwise or counterclockwise):

public double getArea() {
    double sum = 0.0;
    for (int i = 0; i < 4; i++) {
        Point a = pts[i];
        Point b = pts[(i + 1) % 4];
        sum += a.getX() * b.getY() - b.getX() * a.getY();
    }
    return Math.abs(sum) * 0.5;
}

Also correct String.format usage: supply a format string with placeholders, then the values. Finally, if the intent is a special trapezoid defined by two bases and a height, use the trapezoid formula area = (base1 + base2) / 2 * height and make the constructor accept those quantities explicitly.

Recommended Answers

All 3 Replies

OK, before you go on with this, cosider for a moment: is a Quadrilateral an example or special case of a Point, or does it have points as its properties? If the latter, how many Points do you need to represent the Quadrilateral, and how many double values are you passing to the Quadrilateral constructor?

Looking at the getArea() method, what is the formula for computing the area of a Quadrilateral, and do derive the necessary line segment sizes from a pair of Points?

The quadierlateral is the last part where I put in all the info. The points is what gets and sets the x and y variables. And I am passing doubles for 4 shapes.

So much for the Socratic approach, I will be a bit more direct this time: there are issues with your code design above and beyond the error message you've posted. First off, you only posted part of the code for the second class, which makes it harder for us to judge where the problem is occurring; we can't tell which class Trapezoid inherits from. It looks as if it inherits from Point, which would be incorrect: a subclass should always be a specific case of the parent class, whereas a Trapezoid is not a single Point but a collection of Points which define a type of quadrilateral. Properly speaking, it is Quadrilateral that Trapezoid should be inheriting from, but you seem to be going in the opposite direction, making Trapezoid the parent of Quadrilateral.

The other point is that you are passing just two doubles - which define one Point - to the c'tor of Trapezoid, which isn't sufficient information to define a Trapezoid. In fact, given that a Trapezoid has two sized of matching length and two of differing length, it is safe to say that two Points would not be enough to define a Trapezoid, either; you could define a Rectangle with that much information, if you assume that one of the Points is the upper left and the other the lower right (for example), but that depends on the regularity of the Rectangle's sides. With less regular quadrilaterals, you would be better off defining an origin point, and a set of LineSegment objects (which would consist of a pair of Point objects each).

As for getArea(), the formula you are using is simply wrong. You need to get the length of the two matching opposite sides, and multiply that by the long side and the short side. Before you can do that, however, you need the positions of the defining points in space; which in turn means you would have to have the Trapezoid class defined correctly, which as I've said, you don't.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.