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();
    }
    
    
}

EJERCICIO 1
package Semana4examen;

import Semana4examen.ArregloSueldos;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.*;

/**
 *
 * @author luz
 */
public  class Form_Sueldo extends JFrame implements ActionListener {

    JLabel lbl_taman;
    JLabel lbl_frase;
    JTextField txt_tamanio;
    JTextField txt_resul;
    JButton btn_agregar;
    JButton btn_mostrar;
    JButton btn_mosmayores;
    JButton btn_resultado;
    JButton btn_salir;
    JScrollPane contenedor;
    JTextArea jta_arregloSu;
    String rutaArchivo = "h:/Sueldo.txt";
    ArregloSueldos miarreglo = new ArregloSueldos(100);

    public Form_Sueldo() {
        interfaz();
    }

    public void interfaz() {
        lbl_taman = new JLabel("Ingrese tamaño ");
        lbl_taman.setBounds(20, 20, 100, 20);
        add(lbl_taman);

        txt_tamanio = new JTextField();
        txt_tamanio.setBounds(120, 20, 50, 20);
        add(txt_tamanio);

        btn_agregar = new JButton("AGREGAR");
        btn_agregar.setBounds(250, 70, 100, 20);
        btn_agregar.addActionListener(this);
        add(btn_agregar);

        btn_mostrar = new JButton("MOSTRAR");
        btn_mostrar.setBounds(250, 100, 100, 20);
        btn_mostrar.addActionListener(this);
        add(btn_mostrar);

        btn_mosmayores = new JButton("MAYORES");
        btn_mosmayores.setBounds(250, 130, 100, 20);
        btn_mosmayores.addActionListener(this);
        add(btn_mosmayores);

        lbl_frase = new JLabel(" ARREGLO DEL SUELDO ");
        lbl_frase.setBounds(60, 50, 140, 20);
        add(lbl_frase);

        jta_arregloSu = new JTextArea();
        contenedor = new JScrollPane(jta_arregloSu);
        contenedor.setBounds(40, 70, 200, 220);
        add(contenedor);

        txt_resul = new JTextField();
        txt_resul.setBounds(290, 195, 130, 20);
        add(txt_resul);

        btn_resultado = new JButton("PROMEDIO");
        btn_resultado.setBounds(250, 160, 100, 20);
        btn_resultado.addActionListener(this);
        add(btn_resultado);

        btn_salir = new JButton("SALIR");
        btn_salir.setBounds(290, 240, 100, 20);
        btn_salir.addActionListener(this);
        add(btn_salir);

    }

    public void mostrar() {
        for (int i = 0; i < miarreglo.getIndice(); i++) {
            System.out.println(" sueldo [" + (i + 1) + "] : " + miarreglo.getSueldos()[i]);
            jta_arregloSu.append(" sueldo [" + (i + 1) + "] : " + miarreglo.getSueldos()[i]);
            jta_arregloSu.append("\n");
        }
    }

    public void mayores() {
        int cont = 0;
        System.out.println("\n *** Sueldos mayores a 2000 ***");

        for (int i = 0; i < miarreglo.getIndice(); i++) {
            if (miarreglo.getSueldos()[i] > 2000) {
                System.out.println(" mayores [" + ( i + 1) + " ] : "+ miarreglo.getSueldos()[i] );
                jta_arregloSu.append("mayores [" + ( i + 1 )+ "] : "+ miarreglo.getSueldos()[i] );
                jta_arregloSu.append("\n");
                cont++;
            }
        }
        
        jta_arregloSu.append(" Mayores  " + cont);
        jta_arregloSu.append("\n");

    }

    public void promedio() {
        double promedio = 0;
        double suma = 0;
        int cont = 0;
        for (int i = 0; i < miarreglo.getIndice(); i++) {
            if (miarreglo.getSueldos()[i] > 2000) {
                suma = suma + miarreglo.getSueldos()[i];
                cont++;
            }
        }
        promedio = suma / cont;
        txt_resul.setText(String.valueOf(promedio));

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btn_agregar) {

            System.out.println(Integer.parseInt(txt_tamanio.getText().trim()));
            miarreglo.Llenar_Sueldo(Integer.parseInt(txt_tamanio.getText().trim()));

        }

        if (e.getSource() == btn_mostrar) {

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

        if (e.getSource() == btn_mosmayores) {
            this.mayores();
            try {
                EscribirArchivo();
                System.out.println("\n");
            } catch (Exception ee) {
                System.out.println(""+ee.getMessage());
            }
        }

        if (e.getSource() == btn_resultado) {
            this.promedio();
            try {
                EscribirArchivo();
            } catch (Exception ee) {
                System.out.println(""+ee.getMessage());
            }
        }

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

    public static void main(String[] args) {
        Form_Sueldo Ir = new Form_Sueldo();
    
        Ir.setLayout(null);
        Ir.setSize(450, 400);
        Ir.setVisible(true);
    }

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


package Semana4examen;

import java.util.Scanner;
import sun.security.util.Length;

/**
 *
 * @author luz
 */
public class ArregloSueldos {

    private int max;
    private int indice;
    private double sueldos[];

    public ArregloSueldos(int tam) {
      
        this.indice = 0;
        this.max=tam;
        this.sueldos= new double[this.max];
    }
  

   
    public int getIndice() {
        return indice;
    }

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

    public double[] getSueldos() {
        return sueldos;
    }

    public void setSueldos(double[] sueldos) {
        this.sueldos = sueldos;
    }

    public void Llenar_Sueldo(int tamanio) {
        for (int i = 0; i < tamanio; i++) {
            this.sueldos[indice++]=(double) (500+(Math.random()*3000));
        }
    }
    
    public void Mostrar_Sueldo(){
        System.out.println("*** SUELDOS ***");
        for (int i = 0; i < getIndice() ; i++) {
            System.out.println("["+sueldos[i]+"]");
        }
    }
    
    public double Mayores_Sueldos(){
        int cont=0;
        System.out.println("\n *** Sueldos mayores a 2000 ***");
        for (int i = 0; i < getIndice(); i++) {
            
            if (sueldos[i] > 2000) {
                System.out.println("["+sueldos[i]+"]");
                cont++;
            }
        }
        return cont;
    }
    
    public void Promedio_Sueldos(){
        double suma=0;
        double promedio;
        for (int i = 0; i  = "+ promedio);
       
    }
    
    
}