I have written code for the Matrix Multiplication GUI and I have set up two classes that build panels Matrix A and Matrix B. I used a toString() method in both classes, but how do I bring both methods into a multiplication class to do the math and return it to the GUI to display?
public class MatrixA extends JPanel { private JPanel panel; private JTextArea matrixATextArea; public static String inputA; // constructor public MatrixA() { setBorder( BorderFactory.createTitledBorder("Matrix A")); buildPanel(); add( panel ); setVisible( true ); } private void buildPanel() { matrixATextArea = new JTextArea( 10, 30 ); setPreferredSize( new Dimension( 400, 225 ) ); panel = new JPanel(); panel.setBorder( BorderFactory.createLineBorder( Color.black ) ); matrixATextArea.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); panel.add( matrixATextArea ); } public String getTextA() { return inputA = matrixATextArea.getText(); } public String toString() { return inputA; } }
*Note: Matrix B is using the same exact code except inputB as the variable.
MatrixGUI:
public class MatrixGUI extends JFrame { private MatrixInstructions matrixMain; // holding instructions. private MatrixA matrixA; // matrix on left side. private MatrixB matrixB; // matrix on right side. private JPanel buttonPanel; // to hold the calculate and exit button. private JButton calcButton; // to calculate the multiplication. // constructor public MatrixGUI() { // display title setTitle( "Matrix Multiplier" ); // specify action for close button setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // create a layout. setLayout( new BorderLayout() ); // create custom panels. matrixMain = new MatrixInstructions(); matrixA = new MatrixA(); matrixB = new MatrixB(); // create button panel buildButtonPanel(); // add everything to content pane. add( matrixMain, BorderLayout.NORTH ); add( matrixA, BorderLayout.WEST ); add( matrixB, BorderLayout.EAST ); add( buttonPanel, BorderLayout.SOUTH ); pack(); setVisible( true ); } // build the button panel in the south region. private void buildButtonPanel() { // create a panel for buttons buttonPanel = new JPanel(); // create the buttons calcButton = new JButton( "Calculate" ); // register the action buttons calcButton.addActionListener( new MyButtonListener() ); buttonPanel.add( calcButton ); } private class MyButtonListener implements ActionListener { public void actionPerformed( ActionEvent e ) { JOptionPane.showMessageDialog(null, MatrixMult.resultant); } } public static void main( String[] args ) { new MatrixGUI(); } }
and finally the multiplier, which was supplied by the instructor. This is the class that I am having the most issues with, my program compiles but will not do the calculations. I don't think that I am initializing anything in this class, but I need to pull the string from input A and input B into here and do the calculations, then create a string array and send it to the GUI to display.
public class MatrixMult { public String inputA; public String inputB; public static String resultant; public static int[][] convertStringToArray( String str ) { String[] rows = str.split("\\n"); // The \n character separates rows. int numRows = rows.length; // count the non white-space tokens, then count columns. int numCols = str.split("\\s+").length / numRows; Scanner strScanner = new Scanner( str ); //System.out.println( numRows + " " + numCols ); int[][] a = new int[ numRows ][ numCols ]; for( int i = 0; i < numRows; i++ ) { for( int j = 0; j < numCols; j++ ) { a[ i ][ j ] = strScanner.nextInt(); } } return a; } public static String toString( int arr[][] ) { int aRows = arr.length; int aColumns = arr[0].length; String str = ""; for(int i = 0; i < aRows; i++) { // aRow for(int j = 0; j < aColumns; j++) { // aColumn str += arr[ i ][ j ] + " "; } str += "\n"; } return str; } public static void printArray( int a[][] ) { int aRows = a.length; int aColumns = a[0].length; for(int i = 0; i < aRows; i++) { // aRow for(int j = 0; j < aColumns; j++) { // aColumn System.out.print( a[ i ][ j ] + " " ); } System.out.println(); } } public static int[][] multiply( int a[][], int b[][] ) { int aRows = a.length; int aColumns = a[0].length; // all rows have the same length. int bRows = b.length; int bColumns = b[0].length; // all rows have the same length. if ( aColumns != bRows ) { throw new IllegalArgumentException( "A:Rows: " + aColumns + " did not match B:Columns " + bRows + "."); } int[][] resultant = new int[aRows][bColumns]; for(int i = 0; i < aRows; i++) { for(int j = 0; j < bColumns; j++) { for(int k = 0; k < aColumns; k++) { resultant[i][j] += a[i][k] * b[k][j]; } } } return resultant; } public String toString() { return resultant; } }