-MatrixOperationException.java----------------------------------------------------- public class MatrixOperationException extends Exception{ public MatrixOperationException(String arg0){ super(arg0); } } -Matrix.java----------------------------------------------------------------------- public class Matrix{ private double[][] values; private int xDim, yDim; public static Matrix identity(int size){ Matrix result = new Matrix(size, size); for(int n=0;n=subject.getHeight() || x>=subject.getWidth()){ throw new MatrixOperationException("(" + y + "," + x + ")th minor is invalid."); }else{ Matrix result = new Matrix(subject.getHeight()-1, subject.getWidth()-1); for(int i=0;i=x){ result.setCell(i,j,subject.value(i,j+1)); } if(i>=y && j=y && j>=x){ result.setCell(i,j,subject.value(i+1,j+1)); } } } return result; } } public static Matrix flip(Matrix subject) throws MatrixOperationException{ Matrix result = new Matrix(subject.getHeight(),subject.getWidth()); for(int i=0;i3){ throw new MatrixOperationException("Matrix is too large for current inverse algorithm."); }else{ Matrix result = new Matrix(order, order); if(subject.getHeight()==2){ result.setCell(0,0, subject.value(1,1)); result.setCell(0,1,-subject.value(0,1)); result.setCell(1,0,-subject.value(1,0)); result.setCell(1,1, subject.value(0,0)); }else{ Matrix currentMinor; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ currentMinor=minor(subject,j,i); if((i+j)%2!=0){ currentMinor=flip(currentMinor); } result.setCell(i,j,determinant(currentMinor)); } } } result = multiply(result, 1/determinant); return result; } } } public static boolean areEqual(Matrix A, Matrix B){ boolean result = true; if(A.getHeight()!=B.getHeight() || A.getWidth()!=B.getWidth()){ result=false; }else{ for(int i=0;ixDim || other.getHeight()>yDim){ throw new MatrixOperationException("Matrix sizes incompatible"); }else{ for(int i=0;ixDim || yRef>yDim){ throw new MatrixOperationException("Out of matrix dimensions"); }else{ values[yRef][xRef]=newval; } } } -MatrixControls.java-------------------------------------------------------------------- import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.applet.*; public class MatrixControls extends Applet implements ActionListener{ private JPanel mainPanel, appPanel, unaryA, unaryB, subC, matrixA, matrixB, matrixC, centerPanel; private JComboBox opCombo; private JButton detAButton, invAButton, detBButton, invBButton, computeButton; private JTextArea displayA, displayB, displayC; private JLabel equals, opIcon, errorDisplay; public void init(){ setSize(850,200); detAButton = new JButton("Determinant"); invAButton = new JButton("Inverse"); detAButton.addActionListener(this); invAButton.addActionListener(this); detBButton = new JButton("Determinant"); invBButton = new JButton("Inverse"); detBButton.addActionListener(this); invBButton.addActionListener(this); computeButton = new JButton("Compute"); computeButton.addActionListener(this); String[] operations = {"Add", "Subtract", "Multiply", "Divide"}; opCombo = new JComboBox(operations); opCombo.addActionListener(this); Font stdFont = new Font("Monospaced", Font.PLAIN, 16); opIcon = new JLabel("+"); equals = new JLabel("="); errorDisplay = new JLabel(" "); opIcon.setFont(stdFont); equals.setFont(stdFont); displayA = new JTextArea(6, 24); displayB = new JTextArea(6, 24); displayC = new JTextArea(6, 24); displayC.setEditable(false); unaryA = new JPanel(new BorderLayout(5,5)); unaryA.add(detAButton, BorderLayout.NORTH); unaryA.add(invAButton, BorderLayout.SOUTH); unaryB = new JPanel(new BorderLayout(5,5)); unaryB.add(detBButton, BorderLayout.NORTH); unaryB.add(invBButton, BorderLayout.SOUTH); subC = new JPanel(new BorderLayout(5,5)); subC.add(opCombo, BorderLayout.NORTH); subC.add(computeButton, BorderLayout.SOUTH); matrixA = new JPanel(new BorderLayout(5,5)); matrixA.add(displayA, BorderLayout.CENTER); matrixA.add(unaryA, BorderLayout.SOUTH); matrixB = new JPanel(new BorderLayout(5,5)); matrixB.add(displayB, BorderLayout.CENTER); matrixB.add(unaryB, BorderLayout.SOUTH); matrixC = new JPanel(new BorderLayout(5,5)); matrixC.add(displayC, BorderLayout.CENTER); matrixC.add(subC, BorderLayout.SOUTH); centerPanel = new JPanel(new BorderLayout(5,5)); centerPanel.add(opIcon, BorderLayout.WEST); centerPanel.add(matrixB, BorderLayout.CENTER); centerPanel.add(equals, BorderLayout.EAST); mainPanel = new JPanel(new BorderLayout(5,5)); mainPanel.add(matrixA, BorderLayout.WEST); mainPanel.add(centerPanel, BorderLayout.CENTER); mainPanel.add(matrixC, BorderLayout.EAST); mainPanel.add(errorDisplay, BorderLayout.SOUTH); appPanel = new JPanel(new BorderLayout(5,5)); appPanel.add(mainPanel, BorderLayout.CENTER); appPanel.add(Box.createRigidArea(new Dimension(5,0)),BorderLayout.WEST); appPanel.add(Box.createRigidArea(new Dimension(5,0)),BorderLayout.EAST); appPanel.add(Box.createRigidArea(new Dimension(0,5)),BorderLayout.NORTH); appPanel.add(Box.createRigidArea(new Dimension(0,5)),BorderLayout.SOUTH); add(appPanel); } public void actionPerformed(ActionEvent event){ Object source = event.getSource(); if(source == opCombo){ if(opCombo.getSelectedIndex() == 0){ opIcon.setText("+"); } if(opCombo.getSelectedIndex() == 1){ opIcon.setText("-"); } if(opCombo.getSelectedIndex() == 2){ opIcon.setText("x"); } if(opCombo.getSelectedIndex() == 3){ opIcon.setText("/"); } } if(source == invAButton){ try{ displayC.setText(Matrix.format( Matrix.inverse( Matrix.parse( displayA.getText())))); errorDisplay.setText(" "); }catch(MatrixOperationException e){ errorDisplay.setText(e.getMessage()); } } if(source == invBButton){ try{ displayC.setText(Matrix.format( Matrix.inverse( Matrix.parse( displayB.getText())))); errorDisplay.setText(" "); }catch(MatrixOperationException e){ errorDisplay.setText(e.getMessage()); } } if(source == detAButton){ try{ displayC.setText(Double.toString( Matrix.determinant( Matrix.parse( displayA.getText())))); errorDisplay.setText(" "); }catch(MatrixOperationException e){ errorDisplay.setText(e.getMessage()); } } if(source == detBButton){ try{ displayC.setText(Double.toString( Matrix.determinant( Matrix.parse( displayB.getText())))); errorDisplay.setText(" "); }catch(MatrixOperationException e){ errorDisplay.setText(e.getMessage()); } } if(source == computeButton){ try{ Matrix A = Matrix.parse(displayA.getText()); Matrix B = Matrix.parse(displayB.getText()); boolean Adouble = (A.getHeight()==1 && A.getWidth()==1); boolean Bdouble = (B.getHeight()==1 && B.getWidth()==1); if(opCombo.getSelectedIndex() == 0){ if(Adouble && Bdouble){ displayC.setText(String.valueOf( Double.parseDouble(displayA.getText()) +Double.parseDouble(displayB.getText()))); } if(Adouble && !Bdouble){ displayC.setText(Matrix.format( Matrix.add(B, Double.parseDouble(displayA.getText())))); } if(!Adouble && Bdouble){ displayC.setText(Matrix.format( Matrix.add(A, Double.parseDouble(displayB.getText())))); } if(!Adouble && !Bdouble){ displayC.setText(Matrix.format( Matrix.add(A, B))); } } if(opCombo.getSelectedIndex() == 1){ if(Adouble && Bdouble){ displayC.setText(String.valueOf( Double.parseDouble(displayA.getText()) -Double.parseDouble(displayB.getText()))); } if(Adouble && !Bdouble){ displayC.setText(Matrix.format( Matrix.add(Matrix.multiply(B, -1), Double.parseDouble(displayA.getText())))); } if(!Adouble && Bdouble){ displayC.setText(Matrix.format( Matrix.add(A, Double.parseDouble(displayB.getText())* -1))); } if(!Adouble && !Bdouble){ displayC.setText(Matrix.format( Matrix.add(A, Matrix.multiply(B, -1)))); } } if(opCombo.getSelectedIndex() == 2){ if(Adouble && Bdouble){ displayC.setText(String.valueOf( Double.parseDouble(displayA.getText()) *Double.parseDouble(displayB.getText()))); } if(Adouble && !Bdouble){ displayC.setText(Matrix.format( Matrix.multiply(B, Double.parseDouble(displayA.getText())))); } if(!Adouble && Bdouble){ displayC.setText(Matrix.format( Matrix.multiply(A, Double.parseDouble(displayB.getText())))); } if(!Adouble && !Bdouble){ displayC.setText(Matrix.format( Matrix.multiply(A, B))); } } if(opCombo.getSelectedIndex() == 3){ if(Adouble && Bdouble){ displayC.setText(String.valueOf( Double.parseDouble(displayA.getText()) /Double.parseDouble(displayB.getText()))); } if(Adouble && !Bdouble){ displayC.setText(Matrix.format( Matrix.multiply(Matrix.inverse(B), Double.parseDouble(displayA.getText())))); } if(!Adouble && Bdouble){ displayC.setText(Matrix.format( Matrix.multiply(A, 1/Double.parseDouble(displayB.getText())))); } if(!Adouble && !Bdouble){ displayC.setText(Matrix.format( Matrix.multiply(A,Matrix.inverse(B)))); } } errorDisplay.setText(" "); }catch(Exception e){ errorDisplay.setText(e.getMessage()); } } } }