
/* Matrix.java by Jason Plumb
   Concepts of programming languages
   Spring, 1999
   This class implements a simple matrix class
*/
import myInputs;
import java.io.*;

class Matrix{

	// Member variable declarations
	private int iWidth;					//The size of the matrix
	private int iHeight;				//The size of the matrix
	private int members[];				//The integer members

	/* Constructor method */
	public Matrix(int w, int h){			//Constructor method
		if(w<0)
			w = 0;							//Default to 0 width if less than 0
		if(h<0)
			h = 0;							//Default to 0 height if less than 0 
		iWidth = w;
		iHeight = h;
		members = new int[w*h];				//Allocate space appropriately
	}//constructor


	/* This method will transpose the matrix by changing rows into columns 
	   and vice versa */
	public void transpose(){
		int temp[] = new int[iWidth*iHeight];		//Temporary array to hold elements
		int iRow=0;
		int iCol=0;

		if( (iWidth==0) || (iHeight==0))
			return;									//Bail out if empty matrix

		while( (iWidth*iRow+iCol)<(iWidth*iHeight)){		//Loop for all elements
			temp[ (iHeight)*iCol+iRow] = members[(iWidth)*iRow+iCol];
			iCol++;
			if(iCol>=iWidth){						//Check for row increment
				iRow++;
				iCol = 0;
			}//if
		}//while

		System.arraycopy(temp, 0, members, 0, iWidth*iHeight-1);	//Copy temp to members
		iRow = iWidth;
		iWidth = iHeight;
		iHeight = iRow;							//Swap rows and cols
	
	}//transpose method

	/* This method will get the member items from the user */
	public void getUserInput(){
		int iRow=0;
		int iCol=0;
		myInputs inp = new myInputs();

		while( (iWidth*iRow+iCol)<(iWidth*iHeight)){
			System.out.print("Enter matrix entry (Row=" + (int)(iRow+1) + " Col=" + (int)(iCol+1) + "): ");
			try{
				members[iWidth*iRow + iCol] = inp.inputIntegerQuiet();					
			}//try
			catch(Exception e){
				System.out.println("\r\nError obtaining input...aborting!");
				iRow = iWidth*iHeight+1;			//Break out condition
			}//catch

			iCol++;
			if(iCol>=iWidth){
				iCol = 0;
				iRow++;
			}//if

		}//while
		System.out.println("");
	}//getUserInput


	/* This method will multiply the matrix with another one.  It takes
	   a matrix as a parameter and returns a Matrix as the result */
	public Matrix multiply(Matrix m){
		Matrix mat = new Matrix(m.getWidth(), iHeight);
		int iRow;
		int iCol;
		int iTmp;

		//We must check to ensure that multiplication is possible...
		if(iWidth!=m.getHeight())
			return null;

		//And check for empty matrix multiplication...
		if( (iWidth==0) || (iHeight==0)){
			mat = new Matrix(0,0);
			return mat;
		}
			
		for(iRow=0;iRow<iHeight;iRow++){			//Loop for all left rows

			for(iCol=0;iCol<m.iWidth;iCol++){		//Loop for all right columns

				for(int j=0;j<iWidth;j++){			//Loop to get sum
					int s1, s2;
					s1 = members[iWidth*iRow+j];
					s2 = m.members[m.iWidth*j+iCol];
					mat.members[ m.iWidth*iRow+iCol] += s1*s2;
				}//for

			}//for

		}//for

		return mat;
	}//multiply

	/* This method will square the matrix and return the resulting matrix */
	public Matrix squareMatrix(){
		Matrix m = new Matrix(0,0);
		return m;
	}//squareMatrix method

	public int getWidth(){
		return iWidth;
	}//getWidth method

	public int getHeight(){
		return iHeight;
	}

	/* This will copy the members of one matrix into another */
	public void copyMatrix(Matrix source){
		System.arraycopy(source.members, 0, members, 0, iWidth*iHeight);
	}//copyMatrix method

	/* This method will display the matrix to the screen */
	public void printMatrix(){
		int iRow=0;
		int iCol=0;
		if( (iHeight==0) || (iWidth==0)){
			System.out.println("\r\n ** EMPTY MATRIX **\r\n");
			return;
		}
		System.out.print("\r\n [   ");
		while( (iWidth*iRow+iCol)<=(iWidth*iHeight-1)){		//Loop for all members
			String s = String.valueOf(members[iWidth*iRow+iCol]);
			System.out.print(s);
			for(int i=5;i>s.length();i--)
				System.out.print(" ");
			iCol++;
			if(iCol>=iWidth){
				iRow++;
				iCol = 0;
				if(iRow<iHeight)
					System.out.print("\r\n     ");				//Put in a new line
			}//if
		}//while	
		System.out.print("]\r\n\r\n");
	}//printMatrix method

	//This method will print the matrix to a file.  It assumes that the 
	//file object already exists and the file pointer has been moved to the 
	//desired location
	public void printToFile(RandomAccessFile f) throws IOException{
		try{
			
			int iRow=0;
			int iCol=0;
			if( (iHeight==0) || (iWidth==0)){
				f.writeBytes("\r\n ** EMPTY MATRIX **\r\n");
				return;
			}
			f.writeBytes("\r\n [   ");
			while( (iWidth*iRow+iCol)<=(iWidth*iHeight-1)){		//Loop for all members
				String s = String.valueOf(members[iWidth*iRow+iCol]);
				f.writeBytes(s);
				for(int i=5;i>s.length();i--)
					f.writeBytes(" ");
				iCol++;
				if(iCol>=iWidth){
					iRow++;
					iCol = 0;
					if(iRow<iHeight)
						f.writeBytes("\r\n     ");				//Put in a new line
				}//if
			}//while	
			f.writeBytes("]\r\n\r\n");

		}//try
		catch(IOException e){
			throw e;					//Throw it on up
		}//catch
	}//printToFile method
	
}//Matrix class
