Adunarea a două matrici, scăderea a două matrici, afisarea unei matrici, calculul mediei aritmetice a elementelor unei matrici + clasă de test pentru medie
Main.java
package ro.utcluj.poo.lab3matrix;Matrice.java
public class Main {
public static void main(String[] args) {
Matrice a1,a2,a3;
int[][] p=new int[][] {{1,2},{3,4}};
int[][] pp=new int[][] {{1,2},{3,4}};
a1=new Matrice(2, 2, p);
a2=new Matrice(2,2,pp);
a3=a1.add(a2);
a1.Afisare();
a2.Afisare();
a3.Afisare();
System.out.println(a1.Medie());
System.out.println(a2.Medie());
System.out.println(a3.Medie());
}
}
package ro.utcluj.poo.lab3matrix;MatriceTest.java
public class Matrice {
private int[][] a;
private int n,m;
public Matrice(int n, int m, int[][] a)
{
this.a = new int[n][m];
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
this.a[i][j] = a[i][j];
//this.a=a;
this.n=n;
this.m=m;
}
public void Afisare()
{
int i,j;
for(i=0;i<n;i++)
{for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
public Matrice add(Matrice mm)
{
Matrice x;
x=new Matrice(this.n,this.m,this.a);
int i,j;
x.n=this.n;
x.m=this.m;
for(i=0;i<this.n;i++)
for(j=0;j<this.m;j++)
x.a[i][j]=mm.a[i][j]+this.a[i][j];
return x;
}
public Matrice scadere(Matrice mm)
{
Matrice x;
x=new Matrice(this.n,this.m,this.a);
int i,j;
x.n=this.n;
x.m=this.m;
for(i=0;i<this.n;i++)
for(j=0;j<this.m;j++)
x.a[i][j]=-mm.a[i][j]+this.a[i][j];
return x;
}
public double Medie()
{
int i,j;
double s=0;
for(i=0;i<this.n;i++)
for(j=0;j<this.m;j++)
s=s+this.a[i][j];
double m;
m=s/(this.n*this.m);
return m;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
package ro.utcluj.poo.lab3matrix;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
public class MatriceTest {
@Test
public void Medie() {
Matrice a1=new Matrice(2,2,new int[][] {{1,2},{3,4}});
assertEquals(a1.Medie(),2.5,0.01);
//throw new RuntimeException("Test not implemented");
}
}