import java.awt.*; import java.awt.event.*; import java.util.Vector; class MyVector { float d1; float d2; float d3; public MyVector() { d1=0; d2=0; d3=0; } public MyVector(float d1,float d2,float d3) { this.d1=d1; this.d2=d2; this.d3=d3; } public float Product(MyVector vec) { return d1*vec.d1+d2*vec.d2+d3*vec.d3; } public void Add(MyVector vec) { d1+=vec.d1; d2+=vec.d2; d3+=vec.d3; } public void Sub(MyVector vec) { d1-=vec.d1; d2-=vec.d2; d3-=vec.d3; } public void print() { System.out.println("("+d1+","+d2+","+d3+")"); } public String toString() { return "("+d1+","+d2+","+d3+")"; } } public class Preceptron implements ActionListener { Vector X=new Vector(); Vector Y=new Vector(); static Frame f; static TextArea output; static Button btn; public Preceptron() { f=new Frame("Preceptron Algorithm Demo"); output=new TextArea(); btn=new Button("Start Calculate"); btn.addActionListener(this); } public static void main(String[] args) { Preceptron pre=new Preceptron(); Panel p=new Panel(); p.setLayout(new BorderLayout()); p.add(output,BorderLayout.CENTER); p.add(btn,BorderLayout.PAGE_END); f.addWindowListener ( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); f.add(p); f.setSize(800,600); f.setLocation(100,100); f.setVisible(true); } public void actionPerformed(ActionEvent e) { InputDialog DlgInput=new InputDialog(f); DlgInput.show(); output.setText(""); output.append("The calculating procedure is:\n\n"); MyVector line=computeLine(); output.append("\n\nThe result is:\n"); output.append(line.toString()); } public MyVector computeLine() { MyVector w=new MyVector(1,1,1); output.append(w.toString()+"\n"); boolean done=true; int j=0; do { done=true; for ( int i=0;i=0 ) { w.Sub((MyVector)Y.get(i)); output.append(((MyVector)Y.get(i)).toString()+" "); output.append(w.toString()+"\n"); done=false; } j++; } while ( !done && j<10000 ); output.append("\nWhether the line has been found:"+(done==true?"true":"false")+" count="+j); return w; } class InputDialog extends Dialog implements ActionListener { TextField TFXNumber; TextField TFXPoints; TextField TFYNumber; TextField TFYPoints; Button BtnOk; Button BtnCancel; public InputDialog(Frame f) { super(f); Label LXNumber=new Label("The number of XSet points:"); Label LXPoints=new Label("XSet points:"); Label LYNumber=new Label("The number of YSet points:"); Label LYPoints=new Label("YSet points:"); TFXNumber=new TextField(25); TFXPoints=new TextField(25); TFYNumber=new TextField(25); TFYPoints=new TextField(25); BtnOk=new Button(" ok "); BtnOk.addActionListener(this); BtnCancel=new Button("cancel"); BtnCancel.addActionListener(this); Panel up=new Panel(); up.setBackground(Color.white); up.setLayout(new GridLayout(4,2)); Panel p1=new Panel(); p1.setBackground(Color.white); p1.add(LXNumber); Panel p2=new Panel(); p2.setBackground(Color.white); p2.add(TFXNumber); Panel p3=new Panel(); p3.setBackground(Color.white); p3.add(LXPoints); Panel p4=new Panel(); p4.setBackground(Color.white); p4.add(TFXPoints); Panel p5=new Panel(); p5.setBackground(Color.white); p5.add(LYNumber); Panel p6=new Panel(); p6.setBackground(Color.white); p6.add(TFYNumber); Panel p7=new Panel(); p7.setBackground(Color.white); p7.add(LYPoints); Panel p8=new Panel(); p8.setBackground(Color.white); p8.add(TFYPoints); up.add(p1); up.add(p2); up.add(p3); up.add(p4); up.add(p5); up.add(p6); up.add(p7); up.add(p8); Panel dp=new Panel(); dp.setBackground(Color.white); dp.add(BtnOk); dp.add(BtnCancel); Panel pane=new Panel(); pane.setLayout(new BorderLayout()); pane.add(up,BorderLayout.CENTER); pane.add(dp,BorderLayout.PAGE_END); add(pane); setModal(true); setSize(500,200); setLocation(150,200); setVisible(true); } public void actionPerformed(ActionEvent e) { if ( e.getSource()==BtnOk ) { X.clear(); Y.clear(); String StrXNum=TFXNumber.getText(); String StrXSet=TFXPoints.getText(); String StrYNum=TFYNumber.getText(); String StrYSet=TFYPoints.getText(); if ( StrXNum=="" || StrXSet=="" || StrYNum=="" || StrYSet=="" ) { System.out.println("actionPerformed: haven't input error"); System.exit(0); } int XNum=0; int YNum=0; int l=StrXNum.length(); int c=0; int index=0; c=(int)StrXNum.charAt(index); while ( (char)c==' ' ) { index++; c=(int)StrXNum.charAt(index); } while ( (char)c!=' ' && c!=13 && c!=10 && index