jueves, 23 de febrero de 2017

EJERCICIO 2
package examen;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;

public class Form_Arreglo extends JFrame implements ActionListener {

    JLabel lbltam;
    JLabel lblfrase;
    JTextField txttamanio;
    JButton btn_Motrar;
    JButton btn_ingresar;
    JButton btn_ordenar;
    JButton btn_Pares;
    JButton btn_multiplos;
    JButton btn_salir;
    JTextArea txtarea_valores;
    JScrollPane contenedor;
    ObjectOutputStream var1;
    ObjectInputStream var2;
    MainArchivo archivo = new MainArchivo();

    private File file;
    private FileWriter fileWriter;
    private PrintWriter printWriter;
    private BufferedReader bufferedReader;
    private FileReader fileReader;

    private String rutaArchivo = "Archivoexa/Examen.txt";

    Examen miexamen = new Examen(100);

    public Form_Arreglo(String titulo) {
        setTitle(titulo);
        Interfaz();
    }

    public void Interfaz() {

        lbltam = new JLabel("Tamaño del vector");
        lbltam.setBounds(10, 20, 150, 25);
        add(lbltam);

        lblfrase = new JLabel("VECTOR");
        lblfrase.setBounds(195, 55, 150, 25);
        add(lblfrase);

        txttamanio = new JTextField();
        txttamanio.setBounds(180, 20, 100, 25);
        add(txttamanio);

        btn_ingresar = new JButton("Ingresar");
        btn_ingresar.setBounds(10, 80, 100, 25);
        btn_ingresar.addActionListener(this);
        add(btn_ingresar);

        btn_Motrar = new JButton("Mostrar");
        btn_Motrar.setBounds(10, 110, 100, 25);
        btn_Motrar.addActionListener(this);
        add(btn_Motrar);

        btn_Pares = new JButton("Pares");
        btn_Pares.setBounds(10, 140, 100, 25);
        btn_Pares.addActionListener(this);
        add(btn_Pares);

        btn_multiplos = new JButton("Multiplos");
        btn_multiplos.setBounds(10, 170, 100, 25);
        btn_multiplos.addActionListener(this);
        add(btn_multiplos);

        btn_ordenar = new JButton("Ordenar");
        btn_ordenar.setBounds(10, 200, 100, 25);
        btn_ordenar.addActionListener(this);
        add(btn_ordenar);

        btn_salir = new JButton("Salir");
        btn_salir.setBounds(10, 230, 100, 25);
        btn_salir.addActionListener(this);
        add(btn_salir);

        txtarea_valores = new JTextArea();
        contenedor = new JScrollPane(txtarea_valores);
        contenedor.setBounds(150, 80, 150, 200);
        add(contenedor);

//        txtarea_valores.setBounds(150, 80, 150, 200);
//        add(txtarea_valores);
    }

    public void numPares() {
        txtarea_valores.append(" -----PARES------ \n ");
        for (int i = 0; i < miexamen.getIndice(); i++) {
            if (miexamen.getNum()[i] % 2 == 0) {
                txtarea_valores.append(" [ " + (i + 1) + " ] = " + miexamen.getNum()[i]);
                txtarea_valores.append(" \n ");
            }
        }
    }

    public void multiplosDos() {
        txtarea_valores.append(" -----MULTIPLOS DE DOS------ \n ");
        for (int i = 0; i < miexamen.getIndice(); i++) {
            if (miexamen.getNum()[i] % 2 == 0) {
                txtarea_valores.append(" [ " + (i + 1) + " ] = " + miexamen.getNum()[i]);
                txtarea_valores.append(" \n ");
            }
        }
    }

    public void burbuja() {
        int auxiliar;
        int i = 0;
        for (i = 0; i < miexamen.getIndice(); i++) {
            for (int j = 0; j < miexamen.getIndice(); j++) {
                if (miexamen.getNum()[j] < miexamen.getNum()[i]) {
                    auxiliar = miexamen.getNum()[j];
                    miexamen.getNum()[j] = miexamen.getNum()[i];
                    miexamen.getNum()[i] = auxiliar;
                }
            }
        }

    }

    public void ordenar() {
        txtarea_valores.append(" -----ORDENADO------ \n ");
        this.burbuja();
        for (int i = 0; i < miexamen.getIndice(); i++) {
            txtarea_valores.append(" [ " + (i + 1) + " ] = " + miexamen.getNum()[i]);
            txtarea_valores.append(" \n ");
            System.out.println("[" + (i + 1) + "]" + miexamen.getNum()[i]);

        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn_ingresar) {
            System.out.println(Integer.parseInt(txttamanio.getText().trim()));
            miexamen.Generar(Integer.parseInt(txttamanio.getText().trim()));
//binArio
            try {
                var1 = archivo.Escribir_Archivo_obj("Archivoexa/vector.txt");
                if (var1 != null) {
                    var1.writeObject(txtarea_valores);
                }
                var1.close();
            } catch (Exception ee) {
                System.out.println(ee.getMessage());
            }
        }
        }

        if (e.getSource() == btn_Motrar) {
            for (int i = 0; i < miexamen.getIndice(); i++) {
                txtarea_valores.append("Vector " + (i + 1) + " = " + miexamen.getNum()[i]);
                txtarea_valores.append("\n");
            }

            try {
                EscribirArchivo();
            } catch (Exception ee) {
                System.out.println("" + ee.getMessage());
            }
        }

        if (e.getSource() == btn_Pares) {
            this.numPares();

        }

        if (e.getSource() == btn_multiplos) {
            this.multiplosDos();

        }

        if (e.getSource() == btn_ordenar) {
            this.ordenar();
        }

        if (e.getSource() == btn_salir) {
            System.exit(0);
        }
    }

    public FileWriter AbrirArchivo() throws IOException {
        FileWriter temporalArchivo = null;
        try {
            temporalArchivo = new FileWriter(this.rutaArchivo);
        } catch (Exception e) {
            System.out.println("" + e.getMessage());
        }
        return temporalArchivo;
    }

    public void EscribirArchivo() throws IOException {
        FileWriter temporalArchivo = this.AbrirArchivo();
        BufferedWriter escribeArchivo = null;
        if (temporalArchivo != null) {
            escribeArchivo = new BufferedWriter(temporalArchivo);
            try {

                escribeArchivo.write(txtarea_valores.getText());
//                escribeArchivo.write("\n");
                escribeArchivo.newLine();
            } catch (IOException ee) {
                System.out.println("" + ee.getMessage());
            } catch (Exception e) {
                System.out.println("" + e.getMessage());
            } finally {
                if (escribeArchivo != null) {
                    escribeArchivo.close();
                }
            }
        }

    }

    public static void main(String[] args) {
        Form_Arreglo form_Arreglo = new Form_Arreglo("Examen");
        form_Arreglo.setLayout(null);
        form_Arreglo.setVisible(true);
        form_Arreglo.setSize(500, 350);
        form_Arreglo.setResizable(false);
        form_Arreglo.getContentPane().setBackground(new Color(255, 204, 112));

    }
}
ackage examen;

import java.util.Scanner;


public class Examen {

    int indice;
    int max;
    int num[] = new int[max];

    public Examen(int taman) {
        indice = 0;
        max = taman;
        this.num = new int[max];
    }

    public int getIndice() {
        return indice;
    }

    public void setIndice(int indice) {
        this.indice = indice;
    }

    public int[] getNum() {
        return num;
    }

    public void setNum(int[] num) {
        this.num = num;
    }

    public void Generar(int tamanio) {
        for (int i = 0; i <  tamanio; i++) {
            num[indice++] = (int) (0 + Math.random() * 10);
        }
    }

    public void mostrar() {
        for (int i = 0; i < this.getIndice(); i++) {
            System.out.println("num : [" + i + "] = " + num[i]);
        }

    }

    public void Multiplos_dos() {
        for (int i = 0; i < this.getIndice(); i++) {

            if ((num[i] % 2) == 0) {
                System.out.println("Multiplos [" + i + "] = " + num[i]);
            }
        }
    }

    public void Num_Pares() {
        for (int i = 0; i < this.getIndice(); i++) {
            if ((num[i] % 2) == 0) {
                System.out.println("Pares [" + i + "] = " + num[i]);
            }
        }
    }

    public void Ordenar_Ma() {
        int i, j, may, pos, tmp;
        for (i = 0; i < this.getIndice(); i++) {
            may = num[i];
            pos = i;
            for (j = i + 1; j < this.getIndice(); j++) {
                if (num[j] > may) {
                    may = num[j];
                    pos = j;
                }
            }
            if (pos != i) {
                tmp = num[i];
                num[i] = num[pos];
                num[pos] = tmp;
            }
            System.out.println("Vector [" + (i) + "] = " + num[i]);

        }
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Ingrese el tamaño del vector ");
        int tamanio = sc.nextInt();
        Examen a = new Examen(100);
        a.Generar(tamanio);
        a.mostrar();
        System.out.println("");
        System.out.println("Multiplos de dos ");
        a.Multiplos_dos();
        System.out.println("");
        System.out.println("Numeros pares ");
        a.Num_Pares();
        System.out.println("");
        System.out.println("OrdenAr de mayor a menor ");
        a.Ordenar_Ma();
    }
    
    
}

No hay comentarios.:

Publicar un comentario