Determinant

<< MatrixApplet | OtherProjectsTrailIndex | Curve-O-Matic >>

A working version is here

Determinant.java


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**
 * Class Determinant - write a description of the class here
 * 
 * @author Chris Thiel, OFMCap
 * @version 19 Jan 2011
 */
public class Determinant extends Applet implements ItemListener, ActionListener
{
    private Label detLabel;
    private Panel matrixPanel;
    private Button button;
    private Choice orderChoice;
    private TextField[][] cells;
    Panel centerPanel;
    public void init()
    {
       setLayout(new BorderLayout());
       button= new Button("Compute Determinant");
       button.addActionListener(this);
       Panel buttonPanel= new Panel(new FlowLayout(FlowLayout.CENTER));
       buttonPanel.add(button);
       add(buttonPanel, BorderLayout.SOUTH);
       orderChoice=new Choice();
       orderChoice.add("1x1");
       orderChoice.add("2x2");
       orderChoice.add("3x3");
       //orderChoice.add("4x4");
       //orderChoice.add("5x5");
       Panel choicePanel =  new Panel(new FlowLayout(FlowLayout.CENTER));
       choicePanel.add(orderChoice);
       add(choicePanel , BorderLayout.NORTH);
       detLabel=new Label(" = ?");
       centerPanel=new Panel();
       add(centerPanel, BorderLayout.CENTER);
       initMatrix(2);
       orderChoice.select(2-1);
       orderChoice.addItemListener(this);
    }
    public void initMatrix(int n)
    {

        matrixPanel = new Panel();
        matrixPanel.setLayout(new GridLayout(n,n));
        cells = new TextField[n][n];
        for (int i=0; i<n; i++)
           for (int j=0; j<n; j++){
               if (i==j){
                   cells[i][j]=new TextField("1");
                }else {
                    cells[i][j]=new TextField("0");
                }
               cells[i][j].setColumns(8);
               matrixPanel.add(cells[i][j]);
            }
       this.remove(centerPanel);     
       centerPanel=new Panel();
       centerPanel.setLayout(new FlowLayout(FlowLayout.CENTER) );
       centerPanel.add(matrixPanel);
       detLabel.setText(" = ? Press Button below to compute");
       //detLabel.setSize(80,30);
       centerPanel.add(detLabel);
       add(centerPanel, BorderLayout.CENTER);
       validate();

    }
    public int getMatrixSize(){
        return orderChoice.getSelectedIndex()+1;
    }
    /**
     * Paint method for applet.
     * 
     * @param  g   the Graphics object for this applet
     */
    public void paint(Graphics g)
    {

       detLabel.update(g);
       matrixPanel.update(g);
       // button.update(g);
       //centerPanel.update(g);
       //this.update(g);
    }
    /**
     *  ItemListener Implementation 
     */
    public void itemStateChanged(ItemEvent e)
    {
        initMatrix(orderChoice.getSelectedIndex()+1);
        //detLabel.setText("= " + getMatrixSize());
    }
    /**
     * ActionEventLister implementation
     */
    public void actionPerformed(ActionEvent e)
    {
        // Need to compute the determinant using Sarrus' method
        int n=getMatrixSize();
        // n=1 is a special case.
        if (n==1){
            detLabel.setText(" = "+cells[0][0].getText());
        } else if (n==2) {
            Rational ad=new Rational(cells[0][0].getText());
            ad=ad.multiply(new Rational(cells[1][1].getText()));
            Rational bc=new Rational(cells[0][1].getText());
            bc=bc.multiply(new Rational(cells[1][0].getText()));
            bc=bc.multiply(new Rational(-1));
            ad=ad.add(bc);
            detLabel.setText(" = "+ad.toString());
        } else { //n>=3
            Rational sum=new Rational(0);
            for (int i=0; i<n; i++)
            {
              Rational x=new Rational(1);
              for (int j=0; j<n; j++)
                { 
                     x=x.multiply(new Rational(cells[(i+j)%n][j].getText()) );
                }
              sum=sum.add(x);

            }
            Rational diff=new Rational(0);
            for (int i=0; i<n; i++)
            {
              Rational x=new Rational(1);
              for (int j=0; j<n; j++)
                { 
                     x=x.multiply(new Rational(cells[(i+j)%n][n-j-1].getText()) );
                }
              diff=diff.add(x);
            }
            diff=diff.multiply(new Rational(-1));
            sum=sum.add(diff);
            //detLabel.setText(" = "+sum.toString());
            //Rational t=new Rational(cells[0][0].getText());
            detLabel.setText(" = "+sum.toString());
        }
    }
}