Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to setup an MVC application. I don't know how to split this up so that the model view controller will all talk to

I need to setup an MVC application. I don't know how to split this up so that the model view controller will all talk to each other and function properly. Could you please help? please separate and label like example in this link https://www.newthinktank.com/2013/02/mvc-java-tutorial/

--------------

import javax.swing.*; import java.awt.*; import java.awt.event.*;

public class Matrix implements ActionListener { private static int col, row; //dimentions private static double myMatrix [][]; private static double tempMatrix [][]; private static JTextField inputField [][]; private static int result; private static JButton minusB, plusB, inverseB, multiplyB, nMultiplyB, nDivisionB, getValueB, showMatrix, transposing, newMatrix; private static JPanel choosePanel [] = new JPanel[8]; private static int lastCol , lastRow ;

Matrix () { col = row = 0; myMatrix = new double [0][0]; ChooseOperation(); } //prompting for matrix's dimensions private static void getDimension() { JTextField lField = new JTextField(5); //lenght field JTextField wField = new JTextField(5); //col field //design input line JPanel choosePanel [] = new JPanel [2]; choosePanel [0] = new JPanel(); choosePanel [1] = new JPanel(); choosePanel[0].add(new JLabel("Enter Dimensitions") ); choosePanel[1].add(new JLabel("Rows:")); choosePanel[1].add(lField); choosePanel[1].add(Box.createHorizontalStrut(15)); // a spacer choosePanel[1].add(new JLabel("Cols:")); choosePanel[1].add(wField); result = JOptionPane.showConfirmDialog(null, choosePanel, null,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); //save last dimensions lastCol = col; lastRow = row; //ok option if(result == 0) { if(wField.getText().equals("")) col = 0; else { if(isInt(wField.getText())) { col = Integer.parseInt(wField.getText()); } else { JOptionPane.showMessageDialog(null, "Wrong Dimensions"); col = lastCol; row = lastRow; return; } if(isInt(lField.getText())) { row = Integer.parseInt(lField.getText()); } else { JOptionPane.showMessageDialog(null, "Wrong Dimensions"); col = lastCol; row = lastRow; return; } } if(col < 1 || row < 1) { JOptionPane.showConfirmDialog(null, "You entered wrong dimensions", "Error",JOptionPane.PLAIN_MESSAGE); col = lastCol; row = lastRow; } else { tempMatrix = myMatrix; myMatrix = new double [row][col]; if(!setElements(myMatrix, "Fill your new matrix")) //filling the new matrix { //backup myMatrix = tempMatrix; } } } else if(result == 1) { col = lastCol; row = lastRow; } }//end get Dimension //setting a matrix's elementis private static boolean setElements(double matrix [][], String title ) { int temp, temp1; //temprature variable String tempString; JPanel choosePanel [] = new JPanel [row+2]; choosePanel[0] = new JPanel(); choosePanel[0].add(new Label(title )); choosePanel[choosePanel.length-1] = new JPanel(); choosePanel[choosePanel.length-1].add(new Label("consider space field as zeros")); inputField = new JTextField [matrix.length][matrix[0].length]; //lenght loop for(temp = 1; temp <= matrix.length; temp++) { choosePanel[temp] = new JPanel(); for(temp1 = 0; temp1 < matrix[0].length; temp1++) { inputField [temp-1][temp1] = new JTextField(3); choosePanel[temp].add(inputField [temp-1][temp1]); if(temp1 < matrix[0].length -1) { choosePanel[temp].add(Box.createHorizontalStrut(15)); // a spacer } }//end col loop }//end row loop result = JOptionPane.showConfirmDialog(null, choosePanel, null, JOptionPane.OK_OPTION, JOptionPane.PLAIN_MESSAGE); if(result == 0) { checkTextField(inputField); for(temp = 0; temp < matrix.length; temp++) { for(temp1 = 0; temp1 < matrix[0].length; temp1++) { tempString = inputField[temp][temp1].getText(); if(isDouble(tempString)) { matrix [temp][temp1] = Double.parseDouble(inputField[temp][temp1].getText()); } else { JOptionPane.showMessageDialog(null, "You entered wrong elements"); //backup col = lastCol; row = lastRow; return false; } } } return true; } else return false; }//end get Inputs //for setting spaced fields as zeros private static void checkTextField (JTextField field [][] ) { for(int temp = 0; temp < field.length; temp++) { for(int temp1 = 0; temp1 < field[0].length; temp1++) { if(field[temp][temp1].getText().equals("")) field[temp][temp1].setText("0"); } } }//end reset private void ChooseOperation () { int temp; for(temp = 0; temp < choosePanel.length; temp++) { choosePanel [temp] = new JPanel (); } ImageIcon chooseImage = new ImageIcon(getClass().getResource ("choose-button.png")) ; JLabel chooseLabel = new JLabel (chooseImage); choosePanel[0].add(chooseLabel); choosePanel[1].add(Box.createHorizontalStrut(15)); // a spacer choosePanel[6].add(Box.createHorizontalStrut(15)); // a spacer ImageIcon logoImage = new ImageIcon(getClass().getResource("logo.png")) ; JLabel logoLabel = new JLabel (logoImage); choosePanel[7].add(logoLabel); showMatrix = new JButton ("Show Matrix"); showMatrix.setPreferredSize(new Dimension(175,35)); showMatrix.addActionListener(this); choosePanel[2].add(showMatrix); plusB = new JButton ("Plusing with Matrix"); plusB.setPreferredSize(new Dimension(175,35)); plusB.addActionListener(this); choosePanel[2].add(plusB); minusB = new JButton ("Subtracting with Matrix"); minusB.setPreferredSize(new Dimension(175,35)); minusB.addActionListener(this); choosePanel[2].add(minusB); multiplyB = new JButton ("Multiplying by matrix"); multiplyB.setPreferredSize(new Dimension(175,35)); multiplyB.addActionListener(this); choosePanel[3].add(multiplyB); nMultiplyB = new JButton ("Multiplying by scaler"); nMultiplyB.setPreferredSize(new Dimension(175,35)); nMultiplyB.addActionListener(this); choosePanel[3].add(nMultiplyB); nDivisionB = new JButton ("Dividing by scaler"); nDivisionB.setPreferredSize(new Dimension(175,35)); nDivisionB.addActionListener(this); choosePanel[3].add(nDivisionB); transposing = new JButton ("Transposing"); transposing.setPreferredSize(new Dimension(175,35)); transposing.addActionListener(this); choosePanel[4].add(transposing); if(col == row ) { getValueB = new JButton ("GET Value"); getValueB.setPreferredSize(new Dimension(175,35)); getValueB.addActionListener(this); choosePanel[4].add(getValueB); inverseB = new JButton ("Inversing"); inverseB.setPreferredSize(new Dimension(175,35)); inverseB.addActionListener(this); choosePanel[4].add(inverseB); } newMatrix = new JButton("New Matrix"); newMatrix.setPreferredSize(new Dimension(275,35)); newMatrix.addActionListener(this); choosePanel[5].add(newMatrix); JOptionPane.showConfirmDialog(null, choosePanel, null, JOptionPane.CLOSED_OPTION , JOptionPane.PLAIN_MESSAGE); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == showMatrix) { showMatrix( myMatrix, "Your Matrix"); } if(e.getSource() == plusB) { matrixPlusMatrix(); } else if(e.getSource() == minusB) { matrixMinusMatrix(); } else if(e.getSource() == multiplyB) { multiplyByMatrix(); } else if(e.getSource() == inverseB) { inverse(); } else if(e.getSource() == nMultiplyB) { guiMultliplyByScaler(); } else if(e.getSource() == nDivisionB) { divideByScaler (); } else if(e.getSource() == transposing ) { guiTransposing(myMatrix); } else if(e.getSource() == getValueB) { guiGetValue(); } else if(e.getSource() == newMatrix) { newMatrix(); } }//end action performed

private static void showMatrix(double [][] matrix, String title ) { int temp, temp1; //temprature variable JPanel choosePanel [] = new JPanel [matrix.length+1]; choosePanel[0] = new JPanel (); choosePanel[0].add( new JLabel (title) ); for(temp = 1; temp <= matrix.length; temp++) { choosePanel[temp] = new JPanel(); for(temp1 = 0; temp1 < matrix[0].length; temp1++) { if(matrix[temp-1][temp1] == -0) { matrix[temp-1][temp1] = 0; } choosePanel[temp].add(new JLabel(String.format("%.2f", matrix[temp-1][temp1]))); if(temp1 < matrix[0].length -1) { choosePanel[temp].add(Box.createHorizontalStrut(15)); // a spacer } }//end col loop }//end row loop if(col == 0 || row == 0) { JOptionPane.showMessageDialog(null, "You haven't entered any matrix"); } else { JOptionPane.showMessageDialog(null, choosePanel, null, JOptionPane.PLAIN_MESSAGE, null); } }//end show Matrix

private static void matrixPlusMatrix () { if(myMatrix.length < 1) { JOptionPane.showMessageDialog(null, "You haven't entered any matrix"); } else { double m2[][]=new double [row][col]; double sum[][] = new double [row][col];

if(setElements(m2, "Fill Aditional Matrix")) {

for(int i=0;i

if(myMatrix.length < 1) { JOptionPane.showMessageDialog(null, "You haven't entered any matrix"); } else { double m2[][]=new double [row][col]; double sub[][] = new double [row][col]; double temp [][] = new double [row][col];

if(setElements(m2, "Fill Subtracting Matrix")) {

for(int i=0;i

private static void multiplyByMatrix () { JTextField wField = new JTextField(5); //col field int col2 = 0; double m2 [][] , results[][]; int sum; if(myMatrix.length < 1) { JOptionPane.showMessageDialog(null, "You haven't entered any matrix"); } else { //design input line JPanel choosePanel [] = new JPanel [2]; choosePanel [0] = new JPanel(); choosePanel [1] = new JPanel(); choosePanel[0].add(new JLabel("Enter Dimensitions") ); choosePanel[1].add(new JLabel("Rows:")); choosePanel[1].add(new JLabel(""+col)); choosePanel[1].add(Box.createHorizontalStrut(15)); // a spacer choosePanel[1].add(new JLabel("Cols:")); choosePanel[1].add(wField); result = JOptionPane.showConfirmDialog(null, choosePanel, null,JOptionPane.PLAIN_MESSAGE, JOptionPane.PLAIN_MESSAGE); if(result == 0) { if(wField.getText().equals("")) { col2 = 0; } else { if(isInt(wField.getText()) ) { col2 = Integer.parseInt(wField.getText()); } } m2 = new double [col][col2]; results = new double [row][col2]; if(setElements(m2, "Fill multiplying matrix")) { for ( int i = 0 ; i < row ; i++ ) { for ( int j = 0 ; j < col2 ; j++ ) { sum = 0; for ( int k = 0 ; k < col ; k++ ) { sum += myMatrix[i][k]*m2[k][j]; } results[i][j] = sum; } } showMatrix(results, "Mulitiplication Result"); }//elements checking }//dimensions checking else return; }//end else of checking }//end multiply by matrix method

private static void guiMultliplyByScaler () { double[][]results=new double [row][col]; double x; String tempString; if(myMatrix.length < 1) { JOptionPane.showMessageDialog(null, "You haven't entered any matrix"); return; } tempString = JOptionPane.showInputDialog(null, "Enter the scaler number for multiplying");

if (tempString == null) //cancel option { return; } else if(!tempString.equals("")) x= Double.parseDouble(tempString); else { JOptionPane.showMessageDialog(null, "You haven't entered a scaler"); return; } results = multliplyByScaler(myMatrix, x); showMatrix(results, "Multiplication Result"); }//end multiply by number

private static double [][] multliplyByScaler (double [][] matrix , double x) { double[][]results=new double [row][col]; int i,j; for (i=0;i

private static void divideByScaler () { double[][]results=new double [row][col]; int i,j; double x; String tempString; if(myMatrix.length < 1) { JOptionPane.showMessageDialog(null, "You haven't entered any matrix"); return; } //prompting for the scaler tempString = JOptionPane.showInputDialog("Enter the scaler number for dividing"); if (tempString == null) //cancel option { return; } else if(!tempString.equals("")) x= Double.parseDouble(tempString); else { JOptionPane.showMessageDialog(null, "You haven't entered a scaler"); return; } if(x == 0) { JOptionPane.showMessageDialog(null, "Excuse me we can't divid by 0"); return; }

for (i=0;i temp) { swap(res[temp], res[temp+1]); sign *= -1; temp--; } } } for(temp = 0; temp < res.length; temp++) { result *= res[temp][temp]; } return result * sign; }//end getValue private static void guiTransposing (double [][] matrix) { if(myMatrix.length < 1) { JOptionPane.showMessageDialog(null, "You haven't entered any matrix"); return ; } double [][] transMatrix = new double[matrix[0].length][matrix.length]; transMatrix = transposing(matrix); showMatrix(transMatrix, "Trasnposing Matrix"); } private static double [][] transposing (double [][] matrix) { double [][] transportMatrix = new double[matrix[0].length][matrix.length]; int temp1, temp; //termprature variable for(temp = 0 ; temp < matrix[0].length; temp++) { for(temp1 = 0; temp1 < matrix.length; temp1++) { transportMatrix[temp][temp1] = matrix[temp1][temp]; //swap (temp, temp1) } } return transportMatrix; } private static double [][] getMinor (int i, int j) { // i for order in row //j for order in col double [][] results = new double [row-1 ][col-1]; int row_count = 0, col_count = 0; int temp, temp1; for(temp = 0; temp < row; temp++) { for(temp1 = 0; temp1 < col; temp1++) { if(temp != i && temp1 != j) { results[row_count][col_count] = myMatrix[temp][temp1]; col_count++; } }//end col loop col_count = 0; if(i != temp) row_count++; }//end row loop return results; } private static void inverse () { if(myMatrix.length < 1) { JOptionPane.showMessageDialog(null, "You haven't entered any matrix"); return; } else if (col != row) { JOptionPane.showMessageDialog(null, "You must enter square matrix"); return; } else if(getValue(myMatrix) == 0) { JOptionPane.showMessageDialog(null, "Your Matrix " + "hasn't an inverse one " + "Because its value = 0"); return; } double [][] inverseMatrix = new double[row][col]; double [][] minor = new double [row -1 ][col -1]; double [][] cofactor = new double [row ][col]; double delta; //myMatrix value int temp , temp1; //get cofactor for(temp = 0; temp < row; temp++) { for(temp1 = 0; temp1 < col; temp1++) { minor = getMinor (temp, temp1); double minorValue = getValue(minor); cofactor[temp][temp1] = Math.pow(-1.0, temp+temp1) * getValue(minor); } }//end cofactor looping //transport cofactor to get ADJ cofactor = transposing(cofactor); delta = getValue(myMatrix); //count Matrix's for(temp = 0; temp < row; temp++) { for(temp1 = 0; temp1 < col; temp1++) { inverseMatrix[temp][temp1] = cofactor[temp][temp1] / delta; } } showMatrix(inverseMatrix, "Inversing Matrix"); }//end inverse private static boolean isInt (String str) { int temp; if (str.length() == '0') return false; for(temp = 0; temp < str.length();temp++) { if(str.charAt(temp) != '+' && str.charAt(temp) != '-' && !Character.isDigit(str.charAt(temp))) { return false; } } return true; } private static boolean isDouble (String str) { int temp; if (str.length() == '0') return false; for(temp = 0; temp < str.length();temp++) { if(str.charAt(temp) != '+' && str.charAt(temp) != '-' && str.charAt(temp) != '.' && !Character.isDigit(str.charAt(temp)) ) { return false; } } return true; } private static void newMatrix () { getDimension(); } public static void main (String [] args) { Matrix m1 = new Matrix (); } }

//end class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

To organize your code into a basic ModelViewController MVC structure well separate the code into three main components Model View and Controller Model ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Calculus With Applications

Authors: Margaret L. Lial

12th Edition

978-0135871348, 0135871344

More Books

Students also viewed these Programming questions