Este programa realiza la suma, resta, multiplicación y traspuesta de matrices
Main.java
/*
* To change this template, choose Tools Templates
* and open the template in the editor.
*/
package matematica;
/**
*
* @author program
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}
Matriz.java
/*
* To change this template, choose Tools Templates
* and open the template in the editor.
*/
/**
*
* @author program
*/
/**
* Constructor sin parametros
* @author program
*/
public class Matriz {
public int numeroFilas;
public int numeroColumnas;
public double [][] matriz;
public Matriz(){
}
/**
* Constructor con parametros
* @param nf numero de filas
* @param nc numeros de columnas
*/
public Matriz (int nf, int nc){
numeroFilas = nf;
numeroColumnas = nc;
matriz = new double[numeroFilas][numeroColumnas];
for(int i=0; i< numeroFilas; i++)
for (int j=0; j< numeroColumnas; j++)
matriz[i][j]=0;
}
/**
* Metodo de suma de matriz
* @param b primer sumando
* @return Matriz resultado de suma
*/
public Matriz suma(Matriz b){
Matriz resultado;
if((this.numeroFilas == b.numeroFilas) & (this.numeroColumnas ==
b.numeroColumnas)){
resultado = new Matriz(this.numeroFilas,this.numeroFilas);
for (int i=0; i< this.numeroFilas; i++)
for(int j=0; j< this.numeroColumnas; j++)
resultado.matriz[i][j]= this.matriz[i][j] + b.matriz[i][j];
return resultado;
}
else {
System.out.println("Error en dimensiones de las matrices");
resultado = null;
return resultado;
}
}
/**
* Metodo de resta de matrices
* @param b primer sustraendo
* @return resta de matrices
*/
public Matriz resta(Matriz b){
Matriz resultado;
if((this.numeroFilas == b.numeroFilas) & (this.numeroColumnas ==
b.numeroColumnas)){
resultado = new Matriz(this.numeroFilas,this.numeroFilas);
for (int i=0; i< this.numeroFilas; i++)
for(int j=0; j< this.numeroColumnas; j++)
resultado.matriz[i][j]= this.matriz[i][j] - b.matriz[i][j];
return resultado;
}
else {
System.out.println("Error en dimensiones de las matrices");
resultado = null;
return resultado;
}
}
/**
* Metodo para transponer matrices
* @return Transpuesta de la matriz
*/
public Matriz transpuesta(){
Matriz resultado;
resultado= new Matriz(this.numeroColumnas, this.numeroFilas);
for (int i=0; i< this.numeroFilas;i++)
for(int j=0; j< this.numeroColumnas;j++)
resultado.matriz[j][i]=this.matriz[i][j];
return resultado;
}
/**
* Metodo multiplicar matrices
* @param b
* @return
*/
public Matriz multiplicacion(Matriz b){
Matriz resultado;
resultado= new Matriz(this.numeroFilas, this.numeroColumnas);
if((this.numeroFilas == b.numeroColumnas) && (this.numeroColumnas ==
b.numeroFilas)){
for(int i=0; i< this.numeroFilas; i++){
for(int j=0; j< this.numeroColumnas; j++){
for(int k=0; k< b.numeroFilas; k++){
resultado.matriz[i][j]= resultado.matriz[i][j]+
this.matriz[i][k]*b.matriz[k][j];
}
}
}
else{
System.out.println("Las dimensiones de las matrices no son correctas");
}
}
return resultado;
}
/**
* Devuelve el objeto matriz en texto
* @return
*/
public String toString(){
String aux="[";
for (int i=0; i< numeroFilas; i++){
for (int j=0; j< numeroColumnas; j++ ){
aux+= matriz[i][j]+" ";
}
aux += "\n";
}
aux+="]";
return aux;
}
}
PruebaMatriz .java
/*
* To change this template, choose Tools Templates
* and open the template in the editor.
*/
/**
*
* @author program
*/
public class PruebaMatriz {
public static void main(String args[]){
Matriz a= new Matriz(2, 2);
a.matriz[0][0]= 1; a.matriz[0][1]=2;
a.matriz[1][0]= 3; a.matriz[1][1]=4;
System.out.println(a.toString());
Matriz b= new Matriz(2, 2);
b.matriz[0][0]= 5; b.matriz[0][1]=6;
b.matriz[1][0]= 7; b.matriz[1][1]=8;
System.out.println(b.toString());
Matriz c;
c= a.suma(b);
System.out.println(c.toString());
Matriz d;
d= a.resta(b);
System.out.println(d.toString());
c=a.transpuesta();
System.out.println("Transpuesta de a\n"+c.toString());
c=a.multiplicacion(b);
System.out.println(c.toString());
}
}
domingo, 18 de octubre de 2009
Suscribirse a:
Enviar comentarios (Atom)
tengo problemas con public static void main(String[] args) {...}
ResponderEliminarme marca como error