Листинг 7.polinom.java - вычисление значений дробно-рациональной функции комплексного переменного
----------------------------------------------------------------
// импортируем необходимые пакеты классов
import java.awt.*; //граф. интерфейса
import java.math.*; //математич. библиотека
import java.applet.*; //поддержка апплета
import java.lang.*; //стандартная библиотека классов
import java.awt.event.*; //обработчик событий
// Создание класса, эмулирующего комплексные числа и операции с ними...
class Complex{
private double re,im;//Действительная и мнимая часть
// Четыре конструктора
public Complex() {}
public Complex(double re){this.re = re;}
public Complex(double re, double im)
{this.re = re;this.im = im;}
public Complex(Complex c)
{this.re = c.re;this.im = c.im;}
// Методы доступа
public double getRe(){return re;}
public double getIm(){return im;}
public static double Abs(Complex c){return Math.pow(c.re,c.im);}
public static Complex Add(Complex c1, Complex c2)
{return new Complex(c1.re+c2.re, c1.im+c2.im);}
public static Complex Mult(Complex c1, Complex c2){
return new Complex(c1.re*c2.re-c1.im*c2.im, c1.re * c2.im + c1.im * c2.re);}
public static Complex Div(Complex c1, Complex c2){
double d= c2.re*c2.re+c2.im*c2.im;
return new Complex((c1.re*c2.re+c1.im*c2.im)/d,
(c2.re * c1.im-c2.im * c1.re) / d); }
}
public class polinom extends Applet
implements ActionListener{
int m,n;
double P[]={3,2,1,0.5};
double Q[]={5,4,3,2,1,0.2};
Button getitb,edit;
Label Pl[],Ql[];
TextField nt,mt,zret,zimt,Pt[],Qt[];
public void vectors(int n,int m)
{int i,j=0;
if (n>13) n=13; if (n<0) n=0;
if (m>13) m=13; if (m<0) m=0;
Pt=new TextField[n]; Qt=new TextField[m];
Pl=new Label[n]; Ql=new Label[m];
for(i=0;i<=n-1;i++)
{ j=j+25;
Pt[i]=new TextField(""+(n-i-1));
Pt[i].setBounds(33,153+j,30,20);
add(Pt[i]);
Pl[i]=new Label("a"+(n-i-1));
Pl[i].setBounds(13,153+j,20,20);
add(Pl[i]);
}
j=0;
for(i=0;i<=m-1;i++)
{ j=j+25;
Qt[i]=new TextField(""+(m-i-1));
Qt[i].setBounds(133,153+j,30,20);
add(Qt[i]);
Ql[i]=new Label("a"+(m-i-1));
Ql[i].setBounds(113,153+j,20,20);
add(Ql[i]);
}
Pt[n-1].setText("0.5");
Qt[m-1].setText("0.2");
}
public void init(){
int i,j;
j=0;
setBackground(new Color(125,168,203));
setLayout(null);
Label nl=new Label("n:");
nl.setBounds(7,15,10,10);
add(nl);
nt=new TextField("4");
nt.setBounds(20,10,30,20);
add(nt);
Label ml=new Label("m:");
ml.setBounds(50,15,12,10);
add(ml);
mt=new TextField("6");
mt.setBounds(65,10,30,20);
add(mt);
Label zre=new Label("z.Re:");
zre.setBounds(110,12,30,20);
add(zre);
zret=new TextField("3");
zret.setBounds(145,10,40,20);
add(zret);
Label zim=new Label("z.Im:");
zim.setBounds(110,40,30,20);
add(zim);
zimt=new TextField("2");
zimt.setBounds(145,40,40,20);
add(zimt);
Label itogi=new Label("Results:");
itogi.setBounds(345,80,100,20);
add(itogi);
getitb=new Button("Get This!");
getitb.setBounds(323,333,100,33);
add(getitb);
edit=new Button("Change values of vectors...");
edit.setBounds(270,373,203,33);
add(edit);
getitb.addActionListener(this);
edit.addActionListener(this);
n=Integer.parseInt(nt.getText());
m=Integer.parseInt(mt.getText());
Label pnz=new Label("P_n(z)");
pnz.setBounds(33,153,50,20);
add(pnz);
Label qmz=new Label("Q_m(z)");
qmz.setBounds(123,153,50,20);
add(qmz);
vectors(n,m);
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.drawRect(5,5,203,500);
g.setColor(Color.white);
g.drawRect(230,5,353,500);
g.fillRect(250,103,323,209);
g.drawString("(c) 2006. Khramkov Ivan",100,523);
}
public void actionPerformed(ActionEvent ae){
int r,i,j;
String st;
Complex x,y,z,ss;
double t1,s1,s,md,yi;
j=0;
t1=0;
Graphics img=getGraphics();
String str = ae.getActionCommand();
if(str.equals("Get This!"))
{
z=new Complex(
Double.parseDouble(zret.getText()),
Double.parseDouble(zimt.getText()));
img.drawString("z="+String.format("%3.2f", z.getRe())+"+"+
String.format("%3.2f", z.getIm())+"i",255,123);
x=new Complex();
y=new Complex();
s1=Double.parseDouble(zret.getText());
for(r=0;r<=n-1;++r)
{
t1=Double.parseDouble(Pt[r].getText());
x=Complex.Add(
Complex.Mult(x,z),new Complex(t1));
}
for(r=0;r<=m-1;++r)
{
t1=Double.parseDouble(Qt[r].getText());
y=Complex.Add(
Complex.Mult(y,z),new Complex(t1));
}
ss=new Complex(Complex.Div(x,y));
img.drawString("F="+String.format("%3.2f", ss.getRe())+"+"+
String.format("%3.2f", ss.getIm())+"i",255,153);
md=Math.sqrt(ss.getRe()*ss.getRe()+
ss.getIm()*ss.getIm());
img.drawString("Module="+String.format("%3.2f", md),255,183);
s1=Math.toDegrees(Math.atan(ss.getIm() / ss.getRe()));
img.drawString("Phase="+String.format("%3.2f", s1)+" grad",255,213);
}
else
{
for(i=0;i<=n-1;i++)
{
remove(Pt[i]);
remove(Pl[i]);
}
for(i=0;i<=m-1;i++)
{
remove(Qt[i]);
remove(Ql[i]);
}
n=Integer.parseInt(nt.getText());
m=Integer.parseInt(mt.getText());
vectors(n,m);
}
}
}
Листинг polinom.html
--------------------------------------------------------------
<HTML> <BODY><center>
<applet code=polinom.class width=633 height=533></applet>
</center> </BODY></HTML>

Результат контрольного примера: