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));
}
}