import javax.swing.*;
import java.text.*;
import java.util.Locale;

public class matris1
{
public static String toString(double left, int w, int d)
// converts a double to a string with given width and decimals.
{
	NumberFormat df=NumberFormat.getInstance(Locale.US);
	df.setMaximumFractionDigits(d);
    df.setMinimumFractionDigits(d);
    df.setGroupingUsed(false);
    String s = df.format(left);
    while (s.length() < w)
      s = " " + s;
    if (s.length() > w)
    {
        s = "";
        for (int i=0; i<w; i++)
          s = s + "-";
    }
    return s;
}

public static String toString(double[][] left)
{
//return a string representation of a matrix
return toString(left,25,15);
}

public static String toString(double[][] left,int w,int d)
{
//return a string representation of a matrix
int n,m;
String b;
b="";
n=left.length;
m=left[0].length;
for(int i=0;i<n;i++)
  {
  for(int j=0;j<m;j++)
    {
    b=b+toString(left[i][j],w,d);
    }
    b=b+"\n";
  }
return b;
}

public static double[][] carp(double[][] left,double[][] right)
{
//multiplication of two matrices
int ii,jj,i,j,k;
int m1=left[0].length;
int n1=left.length;
int m2=right[0].length;
int n2=right.length;
double[][] b;
b=new double[m1][n2];
 if(n1 != m2)
 {
 System.out.println("inner matrix dimensions must agree");
 for(ii=0;ii<n1;ii++)
   {
   for(jj=0;jj<m2;jj++)
     b[ii][jj]=0;
   }
   return b;
 }
 for(i=0;i<m1;i++)
   {
   for(j=0;j<n2;j++)
     {
     for(k=0;k<n1;k++)
	    b[i][j]+=left[i][k]*right[k][j];
     }
   }
return b;
//iki matrisin çarpımı
}
public static double[][] topla(double[][] left,double[][] right)
{
//addition of two matrices
int n1=left.length;
int m1=left[0].length;
int n2=right.length;
int m2=right[0].length;
int nMax,mMax;
int i,j;
if(m1>=m2) mMax=m1;
else       mMax=m2;
if(n1>=n2) nMax=n1;
else       nMax=n2;
double b[][];
b=new double[nMax][mMax];
for(i=0;i<n1;i++)
  {
  for(j=0;j<m1;j++)
    {
    b[i][j]=b[i][j]+left[i][j];
    }
  }
for(i=0;i<n2;i++)
  {
  for(j=0;j<m2;j++)
    {
    b[i][j]=b[i][j]+right[i][j];
    }
  }
return b;
//end of matrix addition method
}
  public static void main(String args[])
  {
  double a[][]={{3,-0.1,-0.2},{0.1,7,-0.3},{0.3,-0.2,10}};
  double b[][]={{3,0,2},{1,2,-7},{6,8,26}};
  double c[][]=carp(a,b);
  double d[][]=topla(a,b);
  String s="a=\n"+toString(a);
  s+="b=\n"+toString(b);
  s+="a*b=\n"+toString(b);
  s+="a+b=\n"+toString(b);
  String s1="matris çarpma ve toplama işlemleri";
  JOptionPane.showMessageDialog(null,s,s1,JOptionPane.PLAIN_MESSAGE);
  }
}