Skip to content
Advertisement

How correct to make (move) the class internal in Java?

Structure of my project is:

/src/
----/action/
--------/impl/
------------ActionCalculateQuadrangle.interface
--------CalculateQuadrangle.class
--------Line.class
----/other packages/

Code ActionCalculateQuadrangle.interface:

public interface ActionCalculateQuadrangle {
    double getArea(Quadrangle quadrangle);
    double getPerimeter(Quadrangle quadrangle);
    double getLengthLine(Line line);
    double getSlope(Line line);

    List<Point[]> getCombinationsElementsOver(Point[] points, int pointsOnOneLine);
    Point getCrossPoint(Line line1, Line line2);

    boolean isQuadrangle(Quadrangle quadrangle);
    boolean isPointsOnStraightLine(Point[] points);
    boolean isConvex(Quadrangle quadrangle);
    boolean isSquare(Quadrangle quadrangle);
    boolean isRhombus(Quadrangle quadrangle);
    boolean isTrapezoid(Quadrangle quadrangle);
    boolean isDeltoid(Quadrangle quadrangle);
    boolean isStraightPerpendicular(Line line1, Line line2);
    boolean isEqualLengthsDiagonals(Line line1, Line line2);
    boolean isCrossLines(Line line1, Line line2);
    boolean areSidesParallel(Line side1, Line side2);
}

Code Line.class:

public class Line {
    private final Point p1;
    private final Point p2;

    public Line(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public Point getP1() {
        return this.p1;
    }

    public Point getP2() {
        return this.p2;
    }
}

Part of code unity-class ‘CalculateQuadrangle.class` (I cite part of the code so as not to clutter up the place):

// ... some code

@Override
    public double getPerimeter(final Quadrangle quadrangle) {
        Line side1 = new Line(quadrangle.getP1(), quadrangle.getP2());
        Line side2 = new Line(quadrangle.getP2(), quadrangle.getP3());
        Line side3 = new Line(quadrangle.getP3(), quadrangle.getP4());
        Line side4 = new Line(quadrangle.getP1(), quadrangle.getP1());

        return getLengthLine(side1) + getLengthLine(side2) + getLengthLine(side3) + getLengthLine(side4);
    }

    @Override
    public double getLengthLine(final Line line) {
        Point p1 = line.getP1();
        Point p2 = line.getP2();

        return Math.sqrt(Math.pow(p2.getX() - p1.getX(), 2) + Math.pow(p2.getY() - p1.getY(), 2));
    }

    @Override
    public double getSlope(final Line line) {
        Point p1 = line.getP1();
        Point p2 = line.getP2();

        if ((p1.getY() == p2.getY()) || (p1.getX() == p2.getX())) {
            return 0;
        } else {
            return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
        }
    }

// ... some code

That’s ok. But A want to make class ‘Line’ internal.

How I do it? Like this:

// ... some code

// PUT IT HERE!
public class Line {
    private final Point p1;
    private final Point p2;

    public Line(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    public Point getP1() {
        return this.p1;
    }

    public Point getP2() {
        return this.p2;
    }
}

@Override
    public double getPerimeter(final Quadrangle quadrangle) {
        Line side1 = new Line(quadrangle.getP1(), quadrangle.getP2());
        Line side2 = new Line(quadrangle.getP2(), quadrangle.getP3());
        Line side3 = new Line(quadrangle.getP3(), quadrangle.getP4());
        Line side4 = new Line(quadrangle.getP1(), quadrangle.getP1());

        return getLengthLine(side1) + getLengthLine(side2) + getLengthLine(side3) + getLengthLine(side4);
    }

    @Override
    public double getLengthLine(final Line line) {
        Point p1 = line.getP1();
        Point p2 = line.getP2();

        return Math.sqrt(Math.pow(p2.getX() - p1.getX(), 2) + Math.pow(p2.getY() - p1.getY(), 2));
    }

    @Override
    public double getSlope(final Line line) {
        Point p1 = line.getP1();
        Point p2 = line.getP2();

        if ((p1.getY() == p2.getY()) || (p1.getX() == p2.getX())) {
            return 0;
        } else {
            return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
        }
    }

// ... some code

But compiler java show me error like this – Class 'CalculateQuadrangle' must either be declared abstract or implement abstract method 'getLengthLine(Line)' in 'ActionCalculateQuadrangle'.

  1. Declarate abstract – can’t for me.
  2. implement abstract method ‘getLengthLine(Line)’ in ‘ActionCalculateQuadrangle’. That mean, what i must implement ALL abstract method, which have in the argument object class Line.

enter image description here

How correct to make (move) the class internal?

After update still have error: enter image description here enter image description here

Advertisement

Answer

The error you are seeing is because you now have two Line classes, and the method definition and interface definition are not referring to now the same class of CalculateQuadrangle.Line

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement