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?
And what does 2 here mean: Math.Pow(y-py, 2));
– user28265
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].
– Luiz Vieira
I just wanted to figure out how to calculate this, to compare with the result.
– user28265
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. :)
– Luiz Vieira