Realizar un ejercicio que permita calcular la distancia entre dos puntos conocidos aplicando la formula matematica respectiva.
package packDistancia;
import java.io.*;
public class Principal {
public static InputStreamReader Leer = new InputStreamReader(System.in);
public static BufferedReader Teclado = new BufferedReader(Leer);
public static void main(String[] args)throws IOException {
System.out.println("ingrese x1");
int a1 = Integer.parseInt(Teclado.readLine());
System.out.println("ingrese x2");
int a2 = Integer.parseInt(Teclado.readLine());
System.out.println("ingrese y1");
int b1 = Integer.parseInt(Teclado.readLine());
System.out.println("ingrese y2");
int b2 = Integer.parseInt(Teclado.readLine());
Puntos objpunto = new Puntos(a1,a2,b1,b2);
double res=objpunto.Generar(a1,a2,b1,b2);
System.out.println(res); }
}
package packDistancia;
public class Puntos {
private int x1;
private int x2;
private int y1;
private int y2;
public Puntos(int x1, int x2, int y1, int y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public double Generar(int x1, int x2, int y1, int y2){
double r;
r=Math.sqrt(Math.pow((this.x2-this.x1),2)+Math.pow((this.y2-this.y1),2));
return r; }
}
CLASE PRINCIPAL
package packDistancia;
import java.io.*;
public class Principal {
public static InputStreamReader Leer = new InputStreamReader(System.in);
public static BufferedReader Teclado = new BufferedReader(Leer);
public static void main(String[] args)throws IOException {
System.out.println("ingrese x1");
int a1 = Integer.parseInt(Teclado.readLine());
System.out.println("ingrese x2");
int a2 = Integer.parseInt(Teclado.readLine());
System.out.println("ingrese y1");
int b1 = Integer.parseInt(Teclado.readLine());
System.out.println("ingrese y2");
int b2 = Integer.parseInt(Teclado.readLine());
Puntos objpunto = new Puntos(a1,a2,b1,b2);
double res=objpunto.Generar(a1,a2,b1,b2);
System.out.println(res); }
}
CLASE PUNTOS
package packDistancia;
public class Puntos {
private int x1;
private int x2;
private int y1;
private int y2;
public Puntos(int x1, int x2, int y1, int y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public double Generar(int x1, int x2, int y1, int y2){
double r;
r=Math.sqrt(Math.pow((this.x2-this.x1),2)+Math.pow((this.y2-this.y1),2));
return r; }
}
Publicar un comentario