jueves, 8 de abril de 2010

Programa #8

Diseñe un programa que permita resolver la función f(x)= sen² x. Ingresando desde teclado los limites inferior y superior, asi como el numero de segmentos.




import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Programa8 extends Applet implements ActionListener {
   
    private TextField limInf, limSup, numSeg;
    private int LimInf, LimSup, NumSeg;
    private Image fondo;
 
    public void init() {
        
        fondo = getImage(getCodeBase(),"fondoPrograma8.png");

 numSeg = new TextField(2);
 add(numSeg);
 numSeg.addActionListener(this);
  
 limInf = new TextField(2);
 add(limInf);
 limInf.addActionListener(this);
  
 limSup = new TextField(2);
 add(limSup);
 limSup.addActionListener(this);
    }
 
    public void actionPerformed(ActionEvent event) {
        LimInf = Integer.parseInt(limInf.getText());  
 LimSup = Integer.parseInt(limSup.getText());  
 NumSeg = Integer.parseInt(numSeg.getText());  
 repaint();
    }
 
    public void paint(Graphics g) { 
 double h,LimInfRAD,Fx0,XnRAD,Fxn,X,XRAD,Xn,Zum=0,I;
 
 boolean b = g.drawImage(fondo, 0, 0, 300, 200, this);
 
 h = (LimSup - LimInf) / NumSeg;
 LimInfRAD = (LimInf * Math.PI) / 180; 
 Fx0 = 0.5 - (0.5 *(Math.cos(2*LimInfRAD)));
 XnRAD = ((NumSeg * h) * Math.PI)/180;
 Fxn = 0.5 - (0.5 *(Math.cos(2*XnRAD)));
 
 for(int i=1 ; i<=NumSeg ; i++) {
     X = i*h;
     XRAD = (X * Math.PI)/180;
     Xn = 0.5 -(0.5*Math.cos(2*XRAD));
     Zum = Zum+Xn;
 }
 g.setColor(Color.white);
 g.drawString(""+LimSup,60,85);
 g.drawString(""+LimInf,50,140);
 I = (LimSup-LimInf) *( (Fx0 + 2*(Fx0+Zum)+Fxn)/(2*NumSeg));
 g.drawString(""+I,163,112);
    }
}

Comentarios