Taller de repaso de metodos y arreglos

5

Click here to load reader

Transcript of Taller de repaso de metodos y arreglos

Page 1: Taller de repaso de metodos y arreglos

TALLER DE REPASO DE METODOS Y ARREGLOS.

1. REPASO DE METODOS Implemente en Java una clase para trabajar con triángulos isósceles. Dicha clase deberá proporcionar un método constructor, métodos de consulta de las variables de estado que requiera y dos métodos para calcular el perímetro y el área de un triángulo respectivamente. Los métodos deben ser invocados desde la clase Main. ///////****************************///////////////////******************////////////////// package javaapplication13; public class Triangulo { private float base, altura; public Triangulo (float b, float a){ base=b; altura=a; } public float get_base () { return base; } public float get_altura () { return altura;} public double perimetro () { double perimetro1=0; perimetro1= base+2.0*Math.sqrt(altura*altura+(base*base/4.0)); return perimetro1; } public double area () { double areas=0; areas= base*altura/2.0; return areas; } }

2. REPASO DE ARREGLOS MANEJO DE SWITCH CASE, E INVOCACION DE METODOS

CREADOS EN LA CLASE MAIN.

/*Se deben crear dos vectores.

En uno se colocarán los carnet de los estudiantes que aprobaron Cálculo

1, en otro los que aprobaron Etica. Crear un tercer arreglo que contenga

los carnet de los estudiantes que aprobaron ambas materias.. */

package javaapplication13;

import javax.swing.*;

/** * * @author DIANA PLATA */

public class Main {

/**

* @param args the command line arguments

*/

static int[] vCalculo, vEtica, vComun;

Page 2: Taller de repaso de metodos y arreglos

public static void main(String[] args) {

int opcion = 0;

do {

opcion = Integer.parseInt(JOptionPane.showInputDialog("Ingrese la Opción que desea \n 1.

TRIANGULOS \n 2. COMPARACION ARREGLOS \n 3. SALIR"));

switch (opcion) {

case 1: {

Triangulo triangulos = new Triangulo(5, 4);

double perimetros = 0, areas = 0;

float bases = 0, alturas = 0;

perimetros = triangulos.perimetro();

bases = triangulos.get_base();

alturas = triangulos.get_altura();

areas = triangulos.area();

String salida = "BASE \t ALTURA \t AREA \t PERIMETRO \n";

salida += bases + "\t" + alturas + "\t" + areas + "\t" + perimetros + "\n";

JTextArea cuadrosalida = new JTextArea();

cuadrosalida.setText(salida);

JOptionPane.showMessageDialog(null, cuadrosalida, "VALORES DEL TRIANGULO",

JOptionPane.INFORMATION_MESSAGE);

}

break;

case 2: {

int nCal, nEt, comunes;

nCal = Integer.parseInt(JOptionPane.showInputDialog("Cuantos alumnos aprobaron

calculo 1? "));

nEt = Integer.parseInt(JOptionPane.showInputDialog("Cuantos alumnos aprobaron

Etica 1? "));

Page 3: Taller de repaso de metodos y arreglos

vCalculo = new int[nCal];

vEtica = new int[nEt];

String Titulos = "CARNETS DE ALUMNOS DE CALCULO 1";

llenarVector(vCalculo, nCal, Titulos);

Titulos = "CARNETS DE ALUMNOS DE ETICA";

llenarVector(vEtica, nEt, Titulos);

comunes = (cuantosComunes(nCal, nEt));

vComun = new int[comunes];

llenarNuevo(nCal, nEt);

mostrarVector(vComun, comunes);

}

break;

default:

JOptionPane.showMessageDialog(null, "INSERTO UN VALOR INCORRECTO");

}

} while (opcion != 3);

}

/* METODO QUE ASIGNA VALORES A UN VECTOR DE DIMENSION D */

static void llenarVector(int v[], int d, String Titulo) {

for (int i = 0; i < d; i++) {

v[i] = Integer.parseInt(JOptionPane.showInputDialog(Titulo,

JOptionPane.INFORMATION_MESSAGE + "Introduzca el numero de la posicion " + i));

}

}

Page 4: Taller de repaso de metodos y arreglos

static void llenarNuevo(int d, int d2) {

int pos = 0;

for (int i = 0; i < d; i++) {

if (buscaNum(vCalculo[i], d2) == true) {

vComun[pos] = vCalculo[i];

//meterElem(pos, vCalculo[i]);

pos++;

}

}

}

/* ME DICE CUANTOS CARNETS ESTAN EN AMBOS VECTORES. TIENE 2 PARAMETROS:

d: ES LA DIMENSION

DE vCalculo, d2: DIMENSION DE vEtica.

BUSCA CADA ELEMENTO DE vCalculo en vEtica */

static int cuantosComunes(int d, int d2) {

int acum = 0;

for (int i = 0; i < d; i++) {

if (buscaNum(vCalculo[i], d2) == true) {

acum++;

}

}

return acum;

}

/* ESTE METODO ME INDICA SI UN NUMERO n ESTA EN EL VECTOR vEtica. ADEMAS DE

n, TIENE UN

PARAMETRO d, QUE ES LA DIMENSION DE vEtica, NECESARIA PARA CONTROLAR EL

FOR */

Page 5: Taller de repaso de metodos y arreglos

static boolean buscaNum(int n, int d) {

for (int i = 0; i < d; i++) {

if (vEtica[i] == n) {

return true;

}

}

return false;

}

/* ESTE METODO MUESTRA LOS ELEMENTOS DE UN VECTOR CUALQUIERA CON

DIMENSION CUALQUIERA,

EL VECTOR ES EL PARAMETRO V Y LA DIMENSION DEL VECTOR X , SE PENSÓ ASI

PARA PODER

UTILIZARLO EN EL VECTOR RESULTADO

*/

static void mostrarVector(int v[], int x) {

String Salida = "ELEMENTOS COMUNES EN LOS DOS VECTORES \n";

for (int i = 0; i < x; i++) {

Salida += (v[i] + " ");

}

JTextArea cuadrosalida = new JTextArea();

cuadrosalida.setText(Salida);

JOptionPane.showMessageDialog(null, cuadrosalida, "ELEMENTOS COMUNES",

JOptionPane.INFORMATION_MESSAGE);

}

}