Witam mam za zadanie napisać program który mnoży mi dwie macierzy oczywiście one same są generowane przez program (nie ma tylko warunku sprawdzającego k1 i w2) chciałbym aby metoda mnozenie w klasie operacje macierz była statyczna (żebym mógł ją wywołać jak metodę z klasy Math). Kiedy tworzę public static Macierz mnozenie to eclipse chcre by każda zmienna była statyczna. Dlaczego tak się dzieje i czy da się to obejść.
IOimport java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main (String args[]){
String s = null;
String sP[] = new String [2];
String s2[] = new String[2];
String s3[] = new String [2];
System.out.println("podaj wymiary tablicy w formacie x1,y1;x2,y2");
BufferedReader we = new BufferedReader(new InputStreamReader(System.in));
try {
s = we.readLine();
if ((s != null) && (!s.equals(""))) {
sP = s.split(";");
s2=sP[0].split(",");
s3=sP[1].split(",");
}
} catch (IOException e) {
e.printStackTrace();
}
Macierz tab1=new Macierz(new Integer(s2[0].trim()), new Integer(s2[1].trim()),true);
Macierz tab2=new Macierz(new Integer(s3[0].trim()), new Integer(s3[1].trim()),true);
Macierz wynik=new Macierz(new Integer(s2[0].trim()), new Integer(s3[1].trim()),false);
Operacje_macierz oper=new Operacje_macierz (tab1,tab2,wynik);
tab1.Wypisz();
System.out.println();
tab2.Wypisz();
System.out.println();
wynik=oper.mnozenie();
wynik.Wypisz();
//System.out.println(wynik.x+ " "+ wynik.y);
}
}
import java.util.Random;
public class Macierz {
int x,y;
int tab[][];
public Macierz(int x,int y,boolean fill){
this.x=x;
this.y=y;
tab=new int [x][y];
if(fill){
Wypelnij();
}
}
private void Wypelnij(){
int n=10;
Random rand = new Random();
for(int i=0; i<x; i++){
for(int j=0; j<y; j++){
tab[i][j]=rand.nextInt(n+1);
}
}
}
public void Wypisz(){
for(int i=0; i<x; i++)
{
for(int j=0; j<y; j++)
System.out.print(tab[i][j]+" ");
System.out.println();
}
}
}
public class Operacje_macierz {
Macierz m1,m2,w;
int w1,k1,w2,k2;
public Operacje_macierz(Macierz m1, Macierz m2,Macierz w){
this.m1=m1;
this.m2=m2;
this.w=w;
w1=m1.x;
k1=m1.y;
w2=m2.x;
k2=m2.y;
}
public Macierz mnozenie(){
int s=0;
for (int i=0;i<w1;i++){
for (int j=0;j<k2;j++){
for (int x=0;x<k1;x++){
s+=m1.tab[i][x]*m2.tab[x][j];
}
w.tab[i][j]=s;
s=0;
}
}
return w;
}
}