Вы находитесь на странице: 1из 8

DAD

PRCTICA 4
Tres implementaciones para el cculo de permetros
Fco. Javier Vaquero y Rubn Snchez 07/11/2012

Manejo de objetos, clases y mtodos del lenguaje de programacin Java.

TAREA 2
Perimetro1.java:
package lab2_2; import java.util.*; public class Perimetro1{ public static void main(String[] args){ //Ahora hacemos un tringulo Point[] TrianguloVert = new Point[3]; TrianguloVert[0]= new Point(0,0); TrianguloVert[1]= new Point(1,0); TrianguloVert[2]= new Point(1,1); //Imprimimos en pantalla tanto los vrtices como el permetro total System.out.println("La longitud del tringulo con vrtices (" + TrianguloVert[0].x + "," + TrianguloVert[0].y + ")" + " (" + TrianguloVert[1].x + "," + TrianguloVert[1].y + ")" + " (" + TrianguloVert[2].x + "," + TrianguloVert[2].y + ")"+ " es " + calcularLongitud1(TrianguloVert)); //Ahora hacemos un pentgono Point[] PentaVert = new Point[5]; PentaVert[0]= new Point(0,0); PentaVert[1]= new Point(-1,0); PentaVert[2]= new Point(0,2); PentaVert[3]= new Point(2,2); PentaVert[4]= new Point(2,0); //Imprimimos en pantalla tanto los vrtices como el permetro total System.out.println("La longitud del pentgono con vrtices (" + PentaVert[0].x + "," + PentaVert[0].y + ")" + " (" + PentaVert[1].x + "," + PentaVert[1].y + ")" + " (" + PentaVert[2].x + "," + PentaVert[2].y + ")" + " (" + PentaVert[3].x + "," + PentaVert[3].y + ")" + " (" + PentaVert[4].x + "," + PentaVert[4].y + ")"+ " es " + calcularLongitud1(PentaVert)); } //Este mtodo tiene que ser esttico para poderse llamar sin necesidad de inicializar un objeto de la clase Perimetro1 static public double calcularLongitud1(Point... vertice) { int numVertices = vertice.length; //Necesario para el bucle double longitud=0; //Longitud inicial a cero double parcialX; //Guardaremos los resultados parciales double parcialY; //Guardaremos los resultados parciales int i; //Debe inicializarse fuera del bucle para poder utilizar el ndice fuera del bucle for

for(i=0; i<(numVertices-1);i++){ //Realizamos iteradamente el modulo de los segmentos Que unen los puntos y los guardamos en longitud parcialX = vertice[i].x - vertice[i+1].x; parcialY = vertice[i].y - vertice[i+1].y; longitud += Math.sqrt((Math.pow(parcialX, 2)+Math.pow(parcialY, 2))); } //El ultimo tiene que restarse con el primero parcialX = vertice[i].x - vertice[0].x; parcialY = vertice[i].y - vertice[0].y; longitud += Math.sqrt((Math.pow(parcialX, 2)+Math.pow(parcialY, 2))); return longitud; } }

El resultado de la ejecucin del programa Perimetro1 desarrollado en este apartado es el siguiente: La longitud del tringulo con vrtices (0,0) (1,0) (1,1) es 3.414213562373095 La longitud del pentgono con vrtices (0,0) (-1,0) (0,2) (2,2) (2,0) es 9.23606797749979

TAREA 3
Longitud2.java:
package main; import java.awt.Point; public class Longitud2 { //Este mtodo tiene que ser esttico para poder ser llamado sin la necesidad de crear un objeto de la clase Longitud2 static public double calcularLongitud(Point... vertice) { int numVertices = vertice.length; double longitud=0; double parcialX; double parcialY; int i; for(i=0; i<(numVertices-1);i++){ parcialX = vertice[i].x - vertice[i+1].x; parcialY = vertice[i].y - vertice[i+1].y; longitud += Math.sqrt((Math.pow(parcialX, 2)+Math.pow(parcialY, 2))); } parcialX = vertice[i].x - vertice[0].x; parcialY = vertice[i].y - vertice[0].y; longitud += Math.sqrt((Math.pow(parcialX, 2)+Math.pow(parcialY, 2))); Return longitud; } }

Perimetro2.java:
package main; import java.awt.Point; public class Perimetro2 { public static void main(String[] args) { Point[] TrianguloVert TrianguloVert[0]= new TrianguloVert[1]= new TrianguloVert[2]= new = new Point[3]; Point(0,0); Point(1,0); Point(1,1);

System.out.println("La longitud del tringulo con vrtices (" + TrianguloVert[0].x + "," + TrianguloVert[0].y + ")" + " (" + TrianguloVert[1].x + "," + TrianguloVert[1].y + ")" + " (" + TrianguloVert[2].x + "," + TrianguloVert[2].y + ")"+ " es " + Longitud2.calcularLongitud(TrianguloVert)); //Ahora hacemos un pentgono Point[] PentaVert = new Point[5]; PentaVert[0]= new Point(0,0);

PentaVert[1]= new Point(-1,0); PentaVert[2]= new Point(0,2); PentaVert[3]= new Point(2,2); PentaVert[4]= new Point(2,0); System.out.println("La longitud del pentgono con vrtices (" + PentaVert[0].x + "," + PentaVert[0].y + ")" + " (" + PentaVert[1].x + "," + PentaVert[1].y + ")" + " (" + PentaVert[2].x + "," + PentaVert[2].y + ")" + " (" + PentaVert[3].x + "," + PentaVert[3].y + ")" + " (" + PentaVert[4].x + "," + PentaVert[4].y + ")"+ " es " + Longitud2.calcularLongitud(PentaVert)); } }

El resultado de la ejecucin del programa Perimetro2 desarrollado en este apartado es el siguiente: La longitud del tringulo con vrtices (0,0) (1,0) (1,1) es 3.414213562373095 La longitud del pentgono con vrtices (0,0) (-1,0) (0,2) (2,2) (2,0) es 9.23606797749979

TAREA 4
Longitud3.java:
package lab2; import java.util.*; public class Longitud3{ //Este mtodo no tendr que ser esttico porque se inicialar un objeto de la clase Longitud3 public double calcularLongitud(Point... vertice) { //Todo es exactamente igual al resto de los calcularLongitud utilizados anteriormente int numVertices = vertice.length; double longitud=0; double parcialX; double parcialY; int i; for(i=0; i<(numVertices-1);i++){ parcialX = vertice[i].x - vertice[i+1].x; parcialY = vertice[i].y - vertice[i+1].y; longitud += Math.sqrt((Math.pow(parcialX, 2)+Math.pow(parcialY, 2))); } parcialX = vertice[i].x - vertice[0].x; parcialY = vertice[i].y - vertice[0].y; longitud += Math.sqrt((Math.pow(parcialX, 2)+Math.pow(parcialY, 2))); return longitud; } } Perimetro3.java: package lab2; import java.util.*; import java.awt.Point; public class Perimetro3{ public static void main(String[] args){ //Inicializamos un objeto de la clase Longitud3 Longitud3 calculadora = new Longitud3(); //Ahora hacemos un tringulo Point[] TrianguloVert = new Point[3]; TrianguloVert[0]= new Point(0,0); TrianguloVert[1]= new Point(1,0); TrianguloVert[2]= new Point(1,1); //Cuando imprimimos la longitud utilizamos el objeto calculadora System.out.println("La longitud del tringulo con vrtices (" + TrianguloVert[0].x + "," + TrianguloVert[0].y +

")" + " (" + TrianguloVert[1].x + "," + TrianguloVert[1].y + ")" + " (" + TrianguloVert[2].x + "," + TrianguloVert[2].y + ")"+ " es " + calculadora.calcularLongitud(TrianguloVert)); //Ahora hacemos un pentgono Point[] PentaVert = new Point[5]; PentaVert[0]= new Point(0,0); PentaVert[1]= new Point(-1,0); PentaVert[2]= new Point(0,2); PentaVert[3]= new Point(2,2); PentaVert[4]= new Point(2,0); System.out.println("La longitud del pentgono con vrtices (" + PentaVert[0].x + "," + PentaVert[0].y + ")" + " (" + PentaVert[1].x + "," + PentaVert[1].y + ")" + " (" + PentaVert[2].x + "," + PentaVert[2].y + ")" + " (" + PentaVert[3].x + "," + PentaVert[3].y + ")" + " (" + PentaVert[4].x + "," + PentaVert[4].y + ")"+ " es " + calculadora.calcularLongitud(PentaVert)); } }

El resultado de la ejecucin del programa Perimetro3 desarrollado en este apartado es el siguiente: La longitud del tringulo con vrtices (0,0) (1,0) (1,1) es 3.414213562373095 La longitud del pentgono con vrtices (0,0) (-1,0) (0,2) (2,2) (2,0) es 9.23606797749979

Вам также может понравиться