-
Notifications
You must be signed in to change notification settings - Fork 0
/
FactApplet.java
40 lines (36 loc) · 993 Bytes
/
FactApplet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="FactApplet" width=400 height=300>
</applet>
*/
public class FactApplet extends Applet implements ActionListener {
TextField number, factorial;
Button compute;
public void init(){
Label numberp = new Label("Input Number: ");
Label factorialp = new Label("Factorial: ");
number= new TextField(30);
factorial = new TextField(30);
compute = new Button("Compute");
add(numberp);
add(number);
add(factorialp);
add(factorial);
add(compute);
compute.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String snumber;
snumber = number.getText();
int inumber = Integer.parseInt(snumber);
factorial.setText(((Double) getFactorial(inumber)).toString());
}
double getFactorial(int k) {
double fact = 1;
for (int i = 1; i <= k; i++)
fact = fact * i;
return fact;
}
}