Java Point 2d code, does not display the distance correctly

Asked

Viewed 331 times

8

package OrientacaoObjetos;

public class Ponto2D {
    //campos publicos: nao existem restricoes p/ valores de x e y
    public double x, y;

    //constr. default: ponto definido na origem(0, 0)
    public Ponto2D() {
        x = 0; y = 0;
    }
    //constr. paramentizada: ponto definido na instanciacao
    public Ponto2D(double px, double py) {
        x = px; y = py;
    }
    //determina distancia entre instancia e cordenada dada
    public double distancia(double px, double py) {
        return Math.sqrt(Math.pow(x- px, 2) + Math.pow(y- py, 2));
    }
    //determina distancia entre instancia e cordenada dada
   public double distancia(Ponto2D p) {
       return Math.sqrt(Math.pow(x- p.x, 2) + Math.pow(y- p.y, 2));
   }
   //fornece representacao textual dos objetos
   public String toString() {
       return "Ponto2D[x=" + x + ",y=" + y + "]";
   }
}

Here I define values for px and py in creating a new Ponto2d object. put a distance and it displays only px and py values data in the creation of Ponto2d constructor.

package OrientacaoObjetos;

public class UsaPonto2D {
    public static void main(String[] args) {
        Ponto2D p1 = new Ponto2D(3.3, 4.6);
        p1.distancia(55.5, 32.1);
        System.out.println(p1.toString());
    }

}

What’s wrong with it?

1 answer

6


Some things are wrong:

1 - You calculate the distance but do not store it anywhere.

When making the call p1.distancia(55.5, 32.1);, you execute the method distancia but simply ignores its return. Ideally you store what this function returns (return) to use later. For example:

double dist = p1.distancia(55.5, 32.1);

2 - You only print the point p1.

When making the call System.out.println(p1.toString()); you are only printing the content of your object dot. See the method toString to figure out what he prints. You can add in your main class a call to print the previously stored distance (in my example, in the variable dist):

System.out.println(p1.toString());
System.out.println("Distancia: " + dist); // Nova linha adicionada

Or, alternatively, you can print the distance directly without having a temporary and intermediate storage (i.e., ignoring "error" 1). For example:

System.out.println(p1.toString());
System.out.println(p1.distancia(55.5, 32.1)); // Nova linha adicionada
  • And what does 2 here mean: Math.Pow(y-py, 2));

  • 1

    My dear fellow, this site is not a forum, but a question and answer site. Your new question is cool, but I suggest you open another question with her. : ) Oh, and if you haven’t already, please do the [tour] and read to [help].

  • I just wanted to figure out how to calculate this, to compare with the result.

  • 1

    No problem. Just open another question with the new doubt that will have a whole community willing to help you (besides me). Another reason for you to open a new question is that a decent explanation will get too big for comments space. And seriously: do at least the [tour] that you will better understand how this site works and you will feel more comfortable with your interactions. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.