Am I making a mistake, because in declaring the variables?

Viewed 126

My code works. It is giving me the correct result, but I feel I did a mistake because I declare x1, y1, x2, y2 too often (globally and locally). Am I? If I, however, delete one of the declarations, it does not work anymore. Error message:

error: cannot find symbol

Maybe someone can explain to me, how I should have solved the problem without declaring x1, y1, x2, y2 that often.

public class Distanz {
    public static void main(String[] args) {
        double d = 0;
        double x1 = 10;
        double y1 = 8;
        double x2 = 2;
        double y2 = 12;
        berechneDistanzAlsProzedur(x1, x2, y1, y2);
        System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: " + berechneDistanzAlsFunktion(d));
    }
    public static void berechneDistanzAlsProzedur(double x1, double x2, double y1, double y2) {
        x1 = 10;
        y1 = 8;
        x2 = 2;
        y2 = 12;
        double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
        System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: " + Math.sqrt(sqd_d));
    }
    public static double berechneDistanzAlsFunktion(double d) {
        double x1 = 10;
        double y1 = 8;
        double x2 = 2;
        double y2 = 12;
        double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
        return (Math.sqrt(sqd_d));
    }
}
3 Answers

You can simply use the parameters x1, y1, x2, and y2 instead. Otherwise, your methods will always return the same value regardless of the arguments it was called with.

public static void berechneDistanzAlsProzedur(double x1, double x2, double y1, double y2) {
  double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
  System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: "+Math.sqrt(sqd_d));
}
public static double berechneDistanzAlsFunktion(double x1, double x2, double y1, double y2) {
  double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
  return (Math.sqrt(sqd_d));
}  

You could go simple and declare global variables like this:

public class Distanz {
    public static void main(String[] args) {
        double d = 0;
        berechneDistanzAlsProzedur();
        System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: "+berechneDistanzAlsFunktion(d));
    }

    static double x1 = 10;
    static double y1 = 8;
    static double x2 = 2;
    static double y2 = 12;

    public static void berechneDistanzAlsProzedur() {
        double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
        System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: "+Math.sqrt(sqd_d));
    }

    public static double berechneDistanzAlsFunktion(double d) {
        double sqd_d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
        return (Math.sqrt(sqd_d));
    }     
}

Of course, I might not know what you want at all and my answer could be completely useless but I think I answered it pretty well. :D

Below solution can eliminate static declaration and code will look very neat.

public class Distanz {
        public static void main(String[] args) {
        Point point = new Point(10, 8, 2, 12);
        berechneDistanzAlsProzedur(point);
        System.out.print("Distanz von p1 und p2 berechnet mit einer Funktion: " + berechneDistanzAlsFunktion(point));
}

public static void berechneDistanzAlsProzedur(Point point) {
    double sqd_d = (point.getX1() - point.getX2()) * (point.getX1() - point.getX2()) + (point.getY1() - point.getY2()) * (point.getY1() - point.getY2());
    //This formula is same which you have used in berechneDistanzAlsFunktion method, you can eliminate berechneDistanzAlsProzedur function and directly call berechneDistanzAlsFunktion in main function
    //You can pass different points to validate different result
    System.out.println("Distanz von p1 und p2 berechnet mit einer Prozedur: " + Math.sqrt(sqd_d));
}

public static double berechneDistanzAlsFunktion(Point point) {
    return (Math.sqrt((point.getX1() - point.getX2()) * (point.getX1() - point.getX2()) + (point.getY1() - point.getY2()) * (point.getY1() - point.getY2())));
   }
}

Your Point class should be look like this:

public class Point {
    private final double x1;
    private final double y1;
    private final double x2;
    private final double y2;

    public Point(double x1, double y1, double x2, double y2){
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    public double getX1() {
        return x1;
    }

    public double getY1() {
        return y1;
    }

    public double getX2() {
        return x2;
    }

    public double getY2() {
        return y2;
    }
}

Here Point.java called as Wrapper class, which is holding Points information in this code.

Happy Coding.

Related