lunes, 16 de septiembre de 2013

Sesión 1 Java II



SUMA Y RESTA DE VECTORES
























EJECUCIÓN

































SUMA Y RESTA DE MATRICES


EJECUCIÓN


MENÚ SWITCH

package metodoscodigos;

import javax.swing.JOptionPane;

public class Metodoscodigos {

    public static void main(String[] args) {


        menu();

    }

    public static void menu() {

        int menu;
        try {
            do {


                menu = Integer.parseInt(JOptionPane.showInputDialog("INGRESE LA OPCION PARA EJECUTAR EL PROGRAMA DESEADO"
                        + "\n 1. Factorial de un numero"
                        + "\n 2. Acumulado y Promedio de un numero"
                        + "\n 3. Potencia de un numero"
                        + "\n 4. Suma y resta de vectores"
                        + "\n 5. Suma y resta de matrices"
                        + "\n 6. Tablas de multiplicar"
                        + "\n 7. Fin del Programa" + JOptionPane.QUESTION_MESSAGE));

                switch (menu) {
                    case 1:
                        factorial();
                        break;
                    case 2:
                        promedio();
                        break;
                    case 3:
                        potencia();
                        break;
                    case 4:
                        vectores();
                        break;
                    case 5:
                        matrices();
                        break;
                    case 6:
                        tablasmul();
                        break;
                    case 7:
                        menu = 7;
                        break;
                }
            } while (menu != 7);


        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "ERROR" + e.getMessage(), "mensaje", JOptionPane.ERROR_MESSAGE);
        } finally {
        }
    }

//factorial.
    public static void factorial() {
        int n, mul, fac;
        String s;

        s = JOptionPane.showInputDialog(null, "INGRESE EL NUMERO AL CUAL DESEA VER SU FACTORIAL  ");
        n = Integer.parseInt(s);



        for (int m = 0; m <= n; m++) {

            mul = n * m;
            fac = mul * n;
            JOptionPane.showMessageDialog(null, +fac);

        }
    }

    // acomulado y promedio
    public static void promedio() {


        int i = 0, suma = 0, num, cant, sum;
        String s;

        s = JOptionPane.showInputDialog(null, "INGRESE LA CANTIDAD DE NUMEROS QUE DESEA SUMAR Y ACUMULAR  ");
        cant = Integer.parseInt(s);



        while (i < cant) {
            s = JOptionPane.showInputDialog(null, "INGRESE  NUMERO");
            num = Integer.parseInt(s);
            suma = suma + num;
            i++;

        }
        JOptionPane.showMessageDialog(null, +suma);
    }

    //potencia
    public static void potencia() {


        int n, ex, pot;
        String s;

        s = JOptionPane.showInputDialog(null, "INGRESE UN NUMERO  ");
        n = Integer.parseInt(s);

        s = JOptionPane.showInputDialog(null, "INGRESE LA POTENCIA CON LA CUAL DESEA ELEVAR EL NUMERO ");
        ex = Integer.parseInt(s);

        JOptionPane.showMessageDialog(null, +Math.pow(n, ex));

    }

    // suma y resta de vectores.
    public static void vectores() {

        int i, j, k, f;

        String s;

        s = JOptionPane.showInputDialog(null, "Introduce el tamaño de los vectores");
        f = Integer.parseInt(s);

        int v[] = new int[f];
        int w[] = new int[f];
        int R[] = new int[f];


        JOptionPane.showMessageDialog(null, "Introduce los valores del vector V: ");
        for (i = 0; i < f; i++) {
            s = JOptionPane.showInputDialog(null, "V [" + i + "]=");
            v[i] = Integer.parseInt(s);
        }

        JOptionPane.showMessageDialog(null, "Introduce los valores del vector W: ");
        for (j = 0; j < f; j++) {

            s = JOptionPane.showInputDialog(null, "W [" + j + "]=");
            w[j] = Integer.parseInt(s);

        }


        for (k = 0; k < f; k++) {
            R[k] = v[k] + w[k];

        }

        JOptionPane.showMessageDialog(null, "El vector resultante de la suma  es: ");
        for (k = 0; k < f; k++) {
            JOptionPane.showMessageDialog(null, R[k] + "\t");

        }
        for (k = 0; k < f; k++) {
            R[k] = v[k] - w[k];

        }

        JOptionPane.showMessageDialog(null, "El vector resultante de la resta es: ");
        for (k = 0; k < f; k++) {
            JOptionPane.showMessageDialog(null, R[k] + "\t");

        }
    }

    // SUMA Y RESTA DE MATRICES
    public static void matrices() {
        int i, j, k, f, m, a;

        String s;

        s = JOptionPane.showInputDialog(null, "Introduce el tamaño de la fila");
        f = Integer.parseInt(s);
        s = JOptionPane.showInputDialog(null, "Introduce el tamaño de la columna");
        k = Integer.parseInt(s);

        int v[][] = new int[f][k];
        int w[][] = new int[f][k];
        int sum[][] = new int[f][k];
        int res[][] = new int[f][k];

        for (i = 0; i < f; i++) {
            for (j = 0; j < k; j++) {
                v[i][j] = i + 1;
            }
        }
        for (i = 0; i < f; i++) {
            for (j = 0; j < k; j++) {
                System.out.print(v[i][j] + "\t|");
            }
            System.out.println();

        }

        for (i = 0; i < f; i++) {
            for (j = 0; j < k; j++) {
                w[i][j] = i + 1;
            }
        }
        for (i = 0; i < f; i++) {
            for (j = 0; j < k; j++) {
                System.out.print(w[i][j] + "\t|");
            }
            System.out.println();

        }
        for (i = 0; i < f; i++) {
            for (j = 0; j < k; j++) {
                sum[i][j] = v[i][j] + w[i][j];
            }
        }
        for (i = 0; i < f; i++) {
            for (j = 0; j < k; j++) {
                System.out.print(sum[i][j] + "\t|");
            }
            System.out.println();



        }
        for (i = 0; i < f; i++) {
            for (j = 0; j < k; j++) {
                res[i][j] = v[i][j] - w[i][j];
            }
        }
        for (i = 0; i < f; i++) {
            for (j = 0; j < k; j++) {
                System.out.print(res[i][j] + "\t|");
            }
            System.out.println();
        }
    }

    // TABLAS DE MULTIPLICAR

    public static void tablasmul() {

        int resultado, i, j;

        for (i = 1; i <= 10; i++) {
            for (j = 1; j <= 10; j++) {
                JOptionPane.showMessageDialog(null, i + "*" + j + "=" + i * j);

            }
        }
    }
}

EJECUCIÓN



Elaborado por:

Javier Castaño
July Sánchez
Fredy Olaya


No hay comentarios.:

Publicar un comentario