Documentos de Académico
Documentos de Profesional
Documentos de Cultura
CURSO
DOCENTE
ALUMNOS
CICLO
: III
Vectores
package semana4;
public class ArregloUnidimensional {
private int edades[];
private int indice;
public ArregloUnidimensional() {
edades = new int[10];
indice = 0;
}
public void adicionar(int edad) {
if (indice == edades.length)
ampliarArreglo();
edades[indice] = edad;
indice++;
}
private void ampliarArreglo() {
int aux[] = edades;
edades = new int[indice + 10];
for (int i = 0; i < indice; i++)
edades[i] = aux[i];
}
public int getIndice() {
return indice;
}
public int longTotal() {
return edades.length;
}
public int obtener(int pos) {
return edades[pos];
}
public void reinicializar() {
edades = new int[10];
indice = 0;
}
public void generar() {
for (int i = 0; i < longTotal(); i++)
edades[i] = aleatorio(10, 99);
indice = longTotal();
}
public int buscar(int edad) {
for (int i = 0; i < indice; i++)
if (edad == edades[i])
return i;
return -1;
}
public int sumaEdades() {
int suma = 0;
for (int i = 0; i < indice; i++)
suma += edades[i];
return suma;
java.awt.*;
java.awt.event.*;
javax.swing.*;
semana4.ArregloUnidimensional;
getContentPane().setSize(460, 400);
getContentPane().setBackground(Color.lightGray);
lblEdad = new JLabel("Edad :");
lblEdad.setBounds(40, 20, 100, 25);
getContentPane().add(lblEdad);
txtEdad = new JTextField();
txtEdad.setBounds(120, 20, 80, 25);
getContentPane().add(txtEdad);
lblArreglo = new JLabel("Arreglo Lineal", JLabel.CENTER);
lblArreglo.setBackground(new Color(226, 223, 234));
lblArreglo.setOpaque(true);
lblArreglo.setForeground(Color.black);
lblArreglo.setFont(new Font("dialog", 3, 19));
lblArreglo.setBounds(250, 20, 199, 40);
getContentPane().add(lblArreglo);
btnIngresar = new JButton("Ingresar");
btnIngresar.setBounds(320, 80, 150, 23);
btnIngresar.addActionListener(this);
getContentPane().add(btnIngresar);
btnListar = new JButton("listar");
btnListar.setBounds(320, 105, 150, 23);
btnListar.addActionListener(this);
getContentPane().add(btnListar);
btnReportar = new JButton("reportar");
btnReportar.setBounds(320, 130, 150, 23);
btnReportar.addActionListener(this);
getContentPane().add(btnReportar);
btnReinicializar = new JButton("reinicializar arreglo");
btnReinicializar.setBounds(320, 155, 150, 23);
btnReinicializar.addActionListener(this);
getContentPane().add(btnReinicializar);
btnGenerar = new JButton("generar");
btnGenerar.setBounds(320, 180, 150, 23);
btnGenerar.addActionListener(this);
getContentPane().add(btnGenerar);
btnBuscar = new JButton("Buscar");
btnBuscar.setBounds(320, 205, 150, 23);
btnBuscar.addActionListener(this);
getContentPane().add(btnBuscar);
btnOrdenar = new JButton("Ordenar Ascendentemente");
btnOrdenar.setBounds(320, 230, 150, 23);
btnOrdenar.addActionListener(this);
getContentPane().add(btnOrdenar);
txtS = new JTextArea();
scpScroll = new JScrollPane(txtS);
scpScroll.setBounds(10, 80, 300, 298);
getContentPane().add(scpScroll);
}// Ejecucin
// de
// los
// eventos
// tipo
// ActionEvent
mensaje("Arreglo vaco...");
}
void generar() {
a.generar();
listar();
}
void buscar() {
try {
int edad = getEdad();
int pos = a.buscar(edad);
if (pos == -1)
mensaje("La edad no existe");
else
mensaje("La edad se encuentra en la posicin: "
+ pos);
} catch (Exception x) {
mensaje("Edad incorrecta");
}
}
void limpiar() {
txtEdad.setText("");
txtEdad.requestFocus();
}
void imprimir(String cadena) {
txtS.append(cadena + "\n");
}
int getEdad() {
return Integer.parseInt(txtEdad.getText());
}
MATRICES
package matrices;
import java.util.Scanner;
public class Matriz {
private double[][] ma;
private int NumeroFilas;
private int NumeroColumnas;
public Matriz() {
ma = new double[10][10];
}
public Matriz(int m, int n) {
ma = new double[m][n];
}
public Matriz(int m, int n, double v) {
ma = new double[m][n];
for (int i = 0; i < ma.length; i++) {
for (int j = 0; j < ma[0].length; j++) {
ma[i][j] = v;
}
}
}
public Matriz(double[][] a, int m, int n) {
a = new double[m][n];
ma = a;
mismo");
}
return suma;
} else {
System.out.println("el orden de las matrices no es el
}
return null;
}
public Matriz multiplica(Matriz a) {
if (ma[0].length == a.ma.length) {
a.ma[k][j];
}
return producto;
} else {
System.out.println("las columnas de A son diferentes
a las filas de B");
return null;
}
}
public int getNumeroFilas() {
return NumeroFilas;
}
public int getNumeroColumnas() {
return NumeroColumnas;
}
public Object clone() {
Matriz obj = null;
try {
obj = (Matriz) super.clone();
} catch (CloneNotSupportedException ex) {
System.out.println(" Fallo el duplicado");
}
obj.ma = (double[][]) obj.ma.clone();
return obj;
}
public String toString() {
String s = "\n";
for (int i = 0; i < ma.length; i++) {
for (int j = 0; j < ma[0].length; j++) {
s += ma[i][j] + "\t";
}
s += "\n";
}
s += "\n";
return s;
ma[i][j] = v;
}
}
return x;
}
package matrices;
import java.util.Scanner;
public class MatrizDemo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("MATRIZ A");
System.out.print("Filas de A: ");
int flsa=sc.nextInt();
System.out.print("Columnas de A: ");
int clsa=sc.nextInt();
Matriz a=new Matriz(flsa, clsa,2);
Matriz.creaMatriz(flsa, clsa, sc);
System.out.println("MATRIZ B");
System.out.print("Filas de B: ");
int flsb=sc.nextInt();
System.out.print("Columnas de B: ");
int clsb=sc.nextInt();
Matriz b=new Matriz(flsb, clsb,3);
Matriz.creaMatriz(flsb, clsb, sc);
System.out.println("\n
System.out.println("\n
System.out.println("\n
System.out.println("\n
+a.multiplica(b));
Matriz
Matriz
Matriz
Matriz
A\t" +a);
B\t " +b);
Suma\n " +a.suma(b));
Producto \n"