Új hozzászólás Aktív témák

  • #74220800

    törölt tag

    Sziasztok!

    Ismét segítségeteket kérném.

    Van egy pont osztályom:

    public class Point{
    private int x;
    private int y;

    public Point(int x, int y){
    this.x = x;
    this.y= y;
    }

    public int getX(){
    return this.x;
    }

    public int getY(){
    return this.y;
    }

    public boolean equals(Object other){
    Point otherPoint = (Point) other;
    return getX() == otherPoint.getX() && getY() == otherPoint.getY();
    }

    public double getDistance(Point other){
    return Math.sqrt( Math.pow(getX() - other.getX(), 2) + Math.pow(getY() - other.getY(), 2));
    }

    public String toString(){
    return "(" + getX() +", " + getY() + ")";
    }


    }

    Van egy pontokból álló vonallánc osztályom:

    import java.util.ArrayList;
    public class PolyLine{

    private ArrayList<Point> points = new ArrayList<Point>();


    public PolyLine(ArrayList<Point> points){
    this.points = points;

    for (int i = 0; i < points.size()-1; i++){
    if (!add(points.get(i))){
    points.remove(i);
    i--;
    }
    }

    }

    public ArrayList<Point> getPoints(){
    return points;
    }

    public boolean equals(Object other){
    return points.equals(other);
    }

    public boolean add(Point p){

    int pos = points.indexOf(p);

    return pos != -1 && !points.get(pos+1).equals(p);

    }

    public String toString(){

    String a = "[";

    String b = "" ;

    for (int i = 0; i < points.size()-1; i++){
    b = b + points.get(i).toString()+ ", ";
    }

    String c = points.get(points.size()-1).toString() + "]";

    return a + b + c;

    }



    }

    A baj az, hogy a két vonallánc összehasonlításánál az equals false-t dob, amikor azok megegyeznek. Az ismétlődések nem számítanak, azt a vonallánc constructorja kiszedi. A fejlécek fixek, azokat nem lehet változtatni. A sejtesem az, hogy point osztály equals metódusával számol és nem a beépített arraylistessel.

    Itt hozza a teszt:

    import java.util.ArrayList;
    public class Bsp10{

    public static void main(String[] args){


    ArrayList<Point> points = new ArrayList<>();
    points.add(new Point(292, -457));
    points.add(new Point(292, -457));
    points.add(new Point(292, -457));
    points.add(new Point(17, -277));
    points.add(new Point(-942, -103));
    points.add(new Point(-557, -18));

    PolyLine polyLine = new PolyLine(points);

    ArrayList<Point> points2 = new ArrayList<>();
    points2.add(new Point(292, -457));
    points2.add(new Point(17, -277));
    points2.add(new Point(-942, -103));
    points2.add(new Point(-557, -18));

    PolyLine polyLine2 = new PolyLine(points2);

    System.out.println(polyLine);
    System.out.println(polyLine2);
    System.out.println(polyLine.equals(polyLine2));

    }
    }

    Köszi előre is!

Új hozzászólás Aktív témák