Verilen 2 sayı arasındaki asal sayıları bulan basit bir applet uygulaması. Bu dokümanın Applet uygulaması ve algoritma geliştirme açısından faydalı olacağını düşünüyorum...
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.TextField;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
public class asal_sayi_bul extends Applet
{
private Label label1 = new Label();
private Label label2 = new Label();
private TextField textField1 = new TextField();
private TextField textField2 = new TextField();
private Label label3 = new Label();
private TextField textField3 = new TextField();
private Button button1 = new Button();
private int ilk;
private int son;
int i;
public **** paint(Graphics g)
{
g.drawString("",800,600);
}
public asal_sayi_bul()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private **** jbInit() throws Exception
{
this.setLayout(null);
this.setSize(new Dimension(491, 300));
label1.setText("7 üzeri sayi giriniz");
label1.setBounds(new Rectangle(15, 25, 100, 20));
label2.setText("max sayi veriniz");
label2.setBounds(new Rectangle(15, 50, 90, 20));
textField1.setBounds(new Rectangle(120, 25, 65, 20));
textField2.setBounds(new Rectangle(120, 50, 65, 20));
label3.setText("asal sayilar");
label3.setBounds(new Rectangle(20, 80, 70, 20));
textField3.setBounds(new Rectangle(95, 80, 390, 20));
button1.setLabel("sonuc");
button1.setBounds(new Rectangle(15, 120, 80, 25));
button1.addActionListener(new ActionListener()
{
public **** actionPerformed(ActionEvent e)
{
button1_actionPerformed(e);
}
});
this.add(button1, null);
this.add(textField3, null);
this.add(label3, null);
this.add(textField2, null);
this.add(textField1, null);
this.add(label2, null);
this.add(label1, null);
}
private **** button1_actionPerformed(ActionEvent e)
{
int x=1;
if (x==1)
{
Integer sayi1=new Integer(textField1.getText());
ilk=sayi1.intValue();
Integer sayi2=new Integer(textField2.getText());
son=sayi2.intValue();
int flg = 1;
String text = "";
if (ilk<son)
{
for (i=ilk; i<son; i++)
{
if(i%2!=0 && i%3!=0 && i%5!=0 && i%7!=0)
{
if (flg==1)
{
text = String.valueOf(i);
textField3.setText(text);
flg=0;
}
else
{
text = text + "," + String.valueOf(i);
textField3.setText(text);
}
}
}
}
}
}
}
---------------------------------------------------------------
Aşşağıdaki kodları notepad e yazıp programınızın class ının bulundugu kısma .htm olarak kaydedin
--------------------------------------------------------------
<html>
<applet code="asal_sayi_bul.class" width=400 height=300>
</applet>
</html>
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.TextField;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
public class asal_sayi_bul extends Applet
{
private Label label1 = new Label();
private Label label2 = new Label();
private TextField textField1 = new TextField();
private TextField textField2 = new TextField();
private Label label3 = new Label();
private TextField textField3 = new TextField();
private Button button1 = new Button();
private int ilk;
private int son;
int i;
public **** paint(Graphics g)
{
g.drawString("",800,600);
}
public asal_sayi_bul()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private **** jbInit() throws Exception
{
this.setLayout(null);
this.setSize(new Dimension(491, 300));
label1.setText("7 üzeri sayi giriniz");
label1.setBounds(new Rectangle(15, 25, 100, 20));
label2.setText("max sayi veriniz");
label2.setBounds(new Rectangle(15, 50, 90, 20));
textField1.setBounds(new Rectangle(120, 25, 65, 20));
textField2.setBounds(new Rectangle(120, 50, 65, 20));
label3.setText("asal sayilar");
label3.setBounds(new Rectangle(20, 80, 70, 20));
textField3.setBounds(new Rectangle(95, 80, 390, 20));
button1.setLabel("sonuc");
button1.setBounds(new Rectangle(15, 120, 80, 25));
button1.addActionListener(new ActionListener()
{
public **** actionPerformed(ActionEvent e)
{
button1_actionPerformed(e);
}
});
this.add(button1, null);
this.add(textField3, null);
this.add(label3, null);
this.add(textField2, null);
this.add(textField1, null);
this.add(label2, null);
this.add(label1, null);
}
private **** button1_actionPerformed(ActionEvent e)
{
int x=1;
if (x==1)
{
Integer sayi1=new Integer(textField1.getText());
ilk=sayi1.intValue();
Integer sayi2=new Integer(textField2.getText());
son=sayi2.intValue();
int flg = 1;
String text = "";
if (ilk<son)
{
for (i=ilk; i<son; i++)
{
if(i%2!=0 && i%3!=0 && i%5!=0 && i%7!=0)
{
if (flg==1)
{
text = String.valueOf(i);
textField3.setText(text);
flg=0;
}
else
{
text = text + "," + String.valueOf(i);
textField3.setText(text);
}
}
}
}
}
}
}
---------------------------------------------------------------
Aşşağıdaki kodları notepad e yazıp programınızın class ının bulundugu kısma .htm olarak kaydedin
--------------------------------------------------------------
<html>
<applet code="asal_sayi_bul.class" width=400 height=300>
</applet>
</html>