# Java Programming Question.

Q. String Matrix
Write an Application while will accept a algebraic expression of variables (without limit in
variable numbers) and equivalent number of matrices of any order n of square matrix. The output
should be the corresponding operation on matrix element as specified in equation.

Java Code:
import java.io.*;
import java.util.Stack;
import java.util.Vector;

public class que3ans {

/**
* @param args
*/
static int[][] calculate(int a[][],int b[][],int mat_size,String op)
{
int[][] c=new int[mat_size][mat_size];

switch(op)
{
case “+”:

case “-“:
return(c = sub(a,b,mat_size) );
case “*”:
return(c = mul(a,b,mat_size) );
case “/”:
return(c = div(a,b,mat_size) );
default:return c;
}

}
static int[][] mul(int a[][],int b[][],int mat_size)
{
int[][] c=new int[mat_size][mat_size];
int sum=0;
for(int x=0; x<mat_size; x++)
{
for(int i=0; i<mat_size; i++)
{
for(int j=0;j<mat_size; j++)
{
sum = sum + (a[x][j]*b[j][i]);
}
c[x][i]=sum;
sum =0;
}

}

return c;
}

static int[][] add(int[][] a, int[][] b,int mat_size)
{
int[][] c=new int[mat_size][mat_size];

for(int i=0; i<mat_size; i++)
{
for(int j=0; j<mat_size; j++)
{

c[i][j]=a[i][j]+b[i][j];
}

}

return c;
}

static int[][] sub(int[][] a, int[][] b,int mat_size)
{
int[][] c=new int[mat_size][mat_size];
for(int i=0; i<mat_size; i++)
{
for(int j=0; j<mat_size; j++)
{

c[i][j]=a[i][j]-b[i][j];
}

}

return c;
}

static int[][] div(int[][] a, int[][] b,int mat_size)
{
int[][] c=new int[mat_size][mat_size];

for(int i=0; i<mat_size; i++)
{
for(int j=0; j<mat_size; j++)
{

c[i][j]=a[i][j]/b[i][j];
}

}

return c;

}

public static void main(String[] args) {
// TODO Auto-generated method stub

int mat_size=0;
String single=new String();
DataInputStream ds= new DataInputStream(System.in);
Vector v = new Vector();
int total_operators=0;
int total_operands=0;
//String temp =new String();

Stack<String> operator=new Stack<String>();
Stack<String> operand=new Stack<String>();
Stack<int[][]> Data = new Stack<int[][]>();

String stemp =new String();
//char ops[]={‘(‘,’)’,’*’,’/’,’+’,’-‘};
System.out.println(“Enter Expression”);
try
{

}catch(Exception e){}
String[] args1=new String[v.size()];
v.copyInto(args1);
stemp=args1[0];
char[] tempc = new char[stemp.length()];

tempc=stemp.toCharArray();

System.out.println(“Enter Size Of The Matrix..”);

int[][] temp3=new int[mat_size][mat_size];
int[][] a1=new int[mat_size][mat_size];
int[][] b1=new int[mat_size][mat_size];

for(int i=0; i<tempc.length; i++)
{

if(tempc[i] == ‘+’||tempc[i]==’-‘||tempc[i]==’*’||tempc[i]==’/’||tempc[i]==’)’||tempc[i]=='(‘)
{

operator.push(String.valueOf(tempc[i]));
total_operators++;
}
else
{
operand.push(String.valueOf(tempc[i]));
total_operands++;
}

}
//total_operands=tempc.length-total_operators;

for(int i=0;i<total_operands;i++)
{
System.out.println(“Enter the Values For “+operand.elementAt(i)+” Matrix “);
for(int x=0;x<mat_size;x++)
for(int j=0; j<mat_size; j++)
Data.push(temp3);
}

//int x=total_operators;
//int p=(tempc.length-1);
for(int l=0;l<tempc.length; l++)
{ if(tempc[l]==’)’||tempc[l]=='(‘)
{operator.removeElement(“)”);
operator.removeElement(“(“);
}
}

//System.out.print(operator.elementAt(0)+”  “+operator.elementAt(1));

while(!operator.empty()){

a1=Data.pop();
b1=Data.pop();
single = operator.pop();
operator.removeElement(single);

Data.push(que3ans.calculate(a1, b1, mat_size, single));

}

a1=Data.pop();
for(int i=0;i<mat_size;i++)
{
for(int j=0;j<mat_size;j++)
System.out.print(”   “+a1[i][j]);
System.out.println(“”);
}

}

}