I'm making a Distance Formula calculator for practice, but I'm not able to get the program to run on the console when I instantiate the variables. What am I doing wrong here? Any feedback in terms of shortening the code or making it more efficient is also welcome. I've attached it here:
DistFormula.java
public class DistFormula {
public DistFormula() {
}
// Variables
private double x1, x2, y1, y2, diff1, diff2, part1, part2, ans;
// Get first X
public double X1(double x1) {
return x1;
}
// Get first Y
public double Y1(double y1) {
return y1;
}
// Get second X
public double X2(double x2) {
return x2;
}
// Get second Y
public double Y2(double y2) {
return y2;
}
// Set first difference
public double setFirstPart() {
diff1 = x2 - x1;
part1 = Math.pow(diff1, 2);
return part1;
}
// Get first difference
public double getFirstPart() {
return part1;
}
// Set second difference
public double setSecondPart() {
diff2 = y2 - y1;
part2 = Math.pow(diff2, 2);
return part2;
}
// Get second difference
public double getSecondPart() {
return part2;
}
// Set answer
public double setFinalAns() {
ans = Math.sqrt(part1 + part2);
return ans;
}
// Get answer
public double getFinalAns() {
return ans;
}
public String toString() {
return "Distance between coordinates: " + ans;
}
}
Main.java
public class Main {
public static void main(String[] arguments) {
DistFormula newFormula = new DistFormula();
newFormula.X1(10.1);
newFormula.Y1(18.2);
newFormula.X2(12.9);
newFormula.Y2(17.5);
newFormula.setFirstPart();
newFormula.setSecondPart();
newFormula.setFinalAns();
newFormula.toString();
}
}