⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 second.java

📁 JavaApplet实例编程源代码, 里面有好几章
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;

public class Second extends Applet
{
  private Label prompt1,prompt2,prompt3,prompt4,prompt5,prompt6,prompt7;
  private Label     LFunction1,LFunction2,LFunction3,LFunction4,LFunction5,LFunction6;
  private TextField TFunction1,TFunction2,TFunction3,TFunction4,TFunction5,TFunction6;
  
  private Label     LX1,LY1,LX2,LY2;
  private TextField TX1,TY1,TX2,TY2;
  
  private Button BResult;
  
  private TextField TvalX,TvalY;
  
  private Label LCounter;
  
  public void init()
  {
    this.setLayout(null);
    prompt1 = new Label("利用数值方法中的割线法(Second Method)求方程式的解:");
    prompt1.setBounds(10,0,400,20);
    add(prompt1);
    prompt2 = new Label("请输入以下方程式的系数:");
    prompt2.setBounds(10,30,150,20);
    add(prompt2);
    
    LFunction1 = new Label("Y = ");
    LFunction1.setBounds(10,60,20,20);
    add(LFunction1);
    TFunction1 = new TextField();
    TFunction1.setBounds(40,60,30,20);
    TFunction1.setText("0");
    add(TFunction1);
    
    LFunction2 = new Label("X^5 +");
    LFunction2.setBounds(80,60,40,20);
    add(LFunction2);
    TFunction2 = new TextField();
    TFunction2.setBounds(130,60,30,20);
    TFunction2.setText("0");
    add(TFunction2);
    
    LFunction3 = new Label("X^4 +");
    LFunction3.setBounds(170,60,40,20);
    add(LFunction3);
    TFunction3 = new TextField();
    TFunction3.setBounds(220,60,30,20);
    TFunction3.setText("0");
    add(TFunction3);
    
    LFunction4 = new Label("X^3 +");
    LFunction4.setBounds(260,60,40,20);
    add(LFunction4);
    TFunction4 = new TextField();
    TFunction4.setBounds(310,60,30,20);
    TFunction4.setText("0");
    add(TFunction4);
    
    LFunction5 = new Label("X^2 +");
    LFunction5.setBounds(350,60,40,20);
    add(LFunction5);
    TFunction5 = new TextField();
    TFunction5.setBounds(400,60,30,20);
    TFunction5.setText("0");
    add(TFunction5);
    
    LFunction6 = new Label("X +");
    LFunction6.setBounds(440,60,20,20);
    add(LFunction6);
    TFunction6 = new TextField();
    TFunction6.setBounds(470,60,30,20);
    TFunction6.setText("0");
    add(TFunction6);
    
    prompt3 = new Label("请输入以下两点坐标,代表您要利用这两点求一个解:");
    prompt3.setBounds(10,90,400,20);
    add(prompt3);
    
    LX1 = new Label("X1:");
    LX1.setBounds(10,120,20,20);
    add(LX1);
    TX1 = new TextField("0");
    TX1.setBounds(40,120,100,20);
    add(TX1);
    
    LY1 = new Label("Y1:");
    LY1.setBounds(140,120,20,20);
    add(LY1);
    TY1 = new TextField("0");
    TY1.setBounds(170,120,100,20);
    TY1.setEditable(false);
    add(TY1);
    
    LX2 = new Label("X2:");
    LX2.setBounds(280,120,20,20);
    add(LX2);
    TX2 = new TextField("0");
    TX2.setBounds(310,120,100,20);
    add(TX2);
    
    LY2 = new Label("Y2:");
    LY2.setBounds(410,120,20,20);
    add(LY2);
    TY2 = new TextField("0");
    TY2.setBounds(440,120,100,20);
    TY2.setEditable(false);
    add(TY2);
    
    BResult = new Button("求解");
    BResult.setBounds(550,120,40,20);
    add(BResult);
    
    prompt4 = new Label("在两个 Y 值之间,乘积不需小于零,便可求出一解:");
    prompt4.setBounds(10,150,300,20);
    add(prompt4);
    
    prompt5 = new Label("解为: X = ");
    prompt5.setBounds(10,180,60,20);
    add(prompt5);
    TvalX = new TextField("0");
    TvalX.setBounds(80,180,150,20);
    TvalX.setEditable(false);
    add(TvalX);
    
    prompt6 = new Label("Y = ");
    prompt6.setBounds(240,180,30,20);
    add(prompt6);
    TvalY = new TextField("0");
    TvalY.setBounds(270,180,150,20);
    TvalY.setEditable(false);
    add(TvalY);
    
    prompt7 = new Label("PS:此方法精确到小数点以下六位");
    prompt7.setBounds(420,180,200,20);
    add(prompt7);
    
    LCounter = new Label("此方法共迭代了 0 次");
    LCounter.setBounds(10,210,400,20);
    add(LCounter);
    
  }
  
  public boolean action(Event e, Object o)
  {
    if(e.target instanceof TextField)
    {
      if(e.target == TX1)
      {
        System.out.println(TX1.getText());
        TY1.setText(String.valueOf(runFunction(Double.valueOf(TX1.getText()).doubleValue())));
      }
      
      if(e.target == TX2)
      {
        System.out.println(TX2.getText());
        TY2.setText(String.valueOf(runFunction(Double.valueOf(TX2.getText()).doubleValue())));
      }
    }
    
    if(e.target == BResult)
    {
      double x1=0,x2=0,x3=0;
      double Fx1=0,Fx2=0;
      
      int counter=0;
      x1 = Double.valueOf(TX1.getText()).doubleValue();
      x2 = Double.valueOf(TX2.getText()).doubleValue();
      
      Fx1 = runFunction(x1);
      Fx2 = runFunction(x2);
      x3 = ((x1*Fx2 - x2*Fx1)/(Fx2 - Fx1));
      
      while((Math.abs(x1-x2) >= 0.000001)&&(counter<=60))
      {
        
        x3 = ((x1*Fx2 - x2*Fx1)/(Fx2 - Fx1));
        x1 = x2;
        x2 = x3;
        Fx1 = runFunction(x1);
        Fx2 = runFunction(x2);
        
        counter ++;
        LCounter.setText("此方法共迭代了"+counter+"次");
      }
      if(counter <=60)
      {
        TvalX.setText(String.valueOf(x1));
        TvalY.setText(String.valueOf(runFunction(x1)));
      }
      else
      {
        TvalX.setText("可能无解");
        TvalY.setText("可能无解");
        LCounter.setText("此方法共迭代了"+counter+"次,但求不出解,请重新输入点坐标");
      }   
    }
    return true;
  }
  
  public double runFunction(double x)
  {
    double f;
    f = Math.pow(x,5)*Double.valueOf(TFunction1.getText()).doubleValue()
      + Math.pow(x,4)*Double.valueOf(TFunction2.getText()).doubleValue()
      + Math.pow(x,3)*Double.valueOf(TFunction3.getText()).doubleValue()
      + Math.pow(x,2)*Double.valueOf(TFunction4.getText()).doubleValue() 
      + Math.pow(x,1)*Double.valueOf(TFunction5.getText()).doubleValue() 
      + Double.valueOf(TFunction6.getText()).doubleValue()  ;
    
    return f;    
  }
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -