import java.awt.*; import java.applet.Applet; public class equation extends Applet { float[] A,B,C,D; FloatTextField[] aField,bField,cField,dField; Color bkground,fground; TextField detField, xAnswerField,yAnswerField, zAnswerField; Label specialCase; public void init() { /* SET UP THE GUI */ A=new float[3]; B=new float[3]; C=new float[3]; D=new float[3]; aField = new FloatTextField[3]; bField = new FloatTextField[3]; cField = new FloatTextField[3]; dField = new FloatTextField[3]; Color light_blue = new Color(140, 140, 255); bkground=light_blue; fground=Color.black; setBackground(bkground); setForeground(fground); setLayout(new GridLayout(9,1)); Label titleLabel1=new Label("Three equations", Label.CENTER); Label titleLabel2=new Label("three unknowns", Label.CENTER); titleLabel1.setFont(new Font("Dialog", Font.BOLD, 14)); titleLabel2.setFont(new Font("Dialog", Font.BOLD, 14)); add(titleLabel1); add(titleLabel2); /* THE INPUT FIELDS */ Panel[] inputPanel=new Panel[3]; for(int i=0;i<3;i++) { inputPanel[i]=new Panel(); inputPanel[i].setLayout(new GridLayout(1,9)); inputPanel[i].setBackground(bkground); inputPanel[i].setForeground(fground); aField[i] = new FloatTextField(0, 5); bField[i] = new FloatTextField(0, 5); cField[i] = new FloatTextField(0, 5); dField[i] = new FloatTextField(0, 5); inputPanel[i].add(new Label("")); inputPanel[i].add(aField[i]); inputPanel[i].add(new Label(" X +",Label.LEFT)); inputPanel[i].add(bField[i]); inputPanel[i].add(new Label(" Y +",Label.LEFT)); inputPanel[i].add(cField[i]); inputPanel[i].add(new Label(" Z =",Label.LEFT)); inputPanel[i].add(dField[i]); inputPanel[i].add(new Label("")); add(inputPanel[i]); } Button solveMe=new Button("Solve it!"); add(solveMe); specialCase=new Label("The solution", Label.CENTER); specialCase.setFont(new Font("Dialog", Font.BOLD, 14)); specialCase.setForeground(Color.blue); add(specialCase); /* THE ANSWER FIELDS */ Panel answerTop=new Panel(); answerTop.add(new Label("Determinant",Label.RIGHT)); detField=new TextField("",5); detField.setEditable(false); answerTop.add(detField); add(answerTop); Panel answerBottom=new Panel(); answerBottom.add(new Label(" X=",Label.RIGHT)); xAnswerField=new TextField("",5); xAnswerField.setEditable(false); answerBottom.add(xAnswerField); answerBottom.add(new Label(" Y=",Label.RIGHT)); yAnswerField=new TextField("",5); yAnswerField.setEditable(false); answerBottom.add(yAnswerField); answerBottom.add(new Label(" Z=",Label.RIGHT)); zAnswerField=new TextField("",5); zAnswerField.setEditable(false); answerBottom.add(zAnswerField); add(answerBottom); } public boolean action(Event evt, Object arg) { if(arg.equals("Solve it!")) { xAnswerField.setText(""); yAnswerField.setText(""); zAnswerField.setText(""); detField.setText(""); /* TEST IF INPUTS ARE VALID NUMBERS */ boolean validTest=true; for(int i=0;i<3;i++) { validTest = validTest && aField[i].isValid() && bField[i].isValid() && cField[i].isValid() && dField[i].isValid(); } if(validTest) solve(); else specialCase.setText("Fix inputs");; } else return false; return true; } public void solve() { /* CALCULATION ROUTINE */ for(int i=0;i<3; i++) { A[i]=aField[i].getValue(); B[i]=bField[i].getValue(); C[i]=cField[i].getValue(); D[i]=dField[i].getValue(); } /* use Cramer's rule*/ float aDeter=Determinate(D,B,C); float bDeter=Determinate(A,D,C); float cDeter=Determinate(A,B,D); float dDeter=Determinate(A,B,C); detField.setText(new Float(dDeter).toString()); if(dDeter==0) { if(aDeter==0 && bDeter==0 && cDeter==0) { /*dependent equations*/ /*an infinite number of solutions exist*/ specialCase.setText("DEPENDENT"); } else { /*no solution exists*/ specialCase.setText("NULL"); } } else { specialCase.setText("Solved!"); xAnswerField.setText(new Float(aDeter/dDeter).toString()); yAnswerField.setText(new Float(bDeter/dDeter).toString()); zAnswerField.setText(new Float(cDeter/dDeter).toString()); } repaint(); return; } public float Determinate(float[] a, float[] b,float[] c) { float det=0; det=a[0]*b[1]*c[2] +b[0]*c[1]*a[2] +c[0]*a[1]*b[2] -a[2]*b[1]*c[0] -b[2]*c[1]*a[0] -c[2]*a[1]*b[0]; return det; } public void start() { this.show(); } }