1

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();
    }
}
1
  • 2
    You have to use System.out.println(), to print it on console. Commented Aug 14, 2015 at 17:17

3 Answers 3

2

First I think you would need some way to enter the values of each variable.

public void setX1(double x1) {
   this.x1=x1;
}

Also if you want it to be even shorter you could pass these values via the constructor.

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

You could of course break it down into small parts, but you could also have only one method that calculates the exact answer.

public double calculate() {
   return Math.sqrt(Math.pow((x2 - x1), 2) + Math.pow((y2 - y1), 2));
}

If you use the first way I mentioned you need to then do (in Main):

DistFormula newFormula = new DistFormula();
newFormula.setX1(10.1);
newFormula.setY1(18.2);
newFormula.setX2(12.9);
newFormula.setY2(17.5);

double answer=newFormula.calculate();
System.out.println("Distance between coordinates: " + answer); 

If you use the second way:

DistFormula newFormula = new DistFormula(10.1,18.2,12.9,17.5);

double answer=newFormula.calculate();
System.out.println("Distance between coordinates: " + answer);
Sign up to request clarification or add additional context in comments.

Comments

2

First lets try to fix your existing code. For any variable there are corresponding getters and setters but then why? If a variable is private you cannot access it in other class and moreover in setter you can check the allowed value. For e.g mass of a person cannot be in negative so use of setters is to set the value along with checking some allowed limits. The return type of a setter is always void but not in your code. Getters are used to retrieve the value so return type is non void. You mixed the setter and getters in your code where setters are not setting any value but returning the value which is wrong.

I have modified the code:-

public class DistFormula {

    public DistFormula() {

    }

  // Variables
  private double x1, x2, y1, y2, diff1, diff2, part1, part2, ans;

  // Get first X 
  public void setX1(double x1) {
     this.x1=x1;
  }

  // Get first Y
  public void setY1(double y1) {
   this.y1=y1;
  }

  // Get second X
  public void setX2(double x2) {
      this.x2=x2;
  }

  // Get second Y
  public void setY2(double y2) {
      this.y2=y2;
  }

  // Set first difference
  public void setFirstPart() {
    diff1 = x2 - x1;
    part1 = Math.pow(diff1, 2);

  }

  // Get first difference 
  public double getFirstPart() {
    return part1;
  }

  // Set second difference
  public void setSecondPart() {
    diff2 = y2 - y1;
    part2 = Math.pow(diff2, 2);

  }

  // Get second difference
  public double getSecondPart() {
    return part2;
  }

  // Set answer
  public void setFinalAns() {
    ans = Math.sqrt(part1 + part2);

  }

  // Get answer
  public double getFinalAns() {
    return ans;
  }

  public String toString() {
    return "Distance between coordinates: " + ans;
  }

}

public class Main {
    public static void main(String[] arguments) {

        //You are trying to achieve sqrt((x2-x1)^2 +(y2-y1)^2)
        DistFormula newFormula = new DistFormula();

        newFormula.setX1(10.1);
        newFormula.setY1(18.2);
        newFormula.setX2(12.9);
        newFormula.setY2(17.5);

        newFormula.setFirstPart();
        newFormula.setSecondPart();
        newFormula.setFinalAns();
        System.out.println(newFormula.toString());
    }
}

//Output is now coming as:-

Distance between coordinates: 2.886173937932363

In java for printing value in console you need to do it as:-

System.out.println(newFormula);

No need of calling toString method explicitly. See Why is the toString() method being called when I print an object?

A more efficient way is as under:-

public class TwoDimension {

    private double x;
    private double y;
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }

}
public class Main {
    public static void main(String[] arguments) {

        //You are trying to achieve sqrt((x2-x1)^2 +(y2-y1)^2)
        TwoDimension newFormula1 = new TwoDimension();
        newFormula1.setX(10.1);
        newFormula1.setY(18.2);

        TwoDimension newFormula2 = new TwoDimension();
        newFormula2.setX(12.9);
        newFormula2.setY(17.5);

        Double x = newFormula2.getX() -newFormula1.getX();
        Double y =newFormula2.getY() -newFormula1.getY();
        Double z = Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2)) ;
        System.out.println("Distance between coordinates: " + z);
    }
}

Comments

1

you need to call System.out.println(newFormula.toString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.