Example Application: Algebra: Windows Controls
Example Application: Algebra: Windows Controls
Description
This application demonstrates the use of the button control of Microsoft Windows. It does this
while calculating the factorial, the permutation, and the combinatorial.
Windows Controls:
Label
Button
Text Box
Tab Control
namespace Algebra2
{
public class Algebra
{
public static long Factorial(long x)
{
if (x <= 1)
return 1;
else
return x * Factorial(x - 1);
}
public static long Permutation(long n, long r)
{
if (r == 0)
return 0;
if (n == 0)
return 0;
if ((r >= 0) && (r <= n))
return Factorial(n) / Factorial(n - r);
else
return 0;
}
public static long Combinatorial(long a, long b)
{
if (a <= 1)
return 1;
}
}
Control
Text
JTabbedPane
Name
jtfAlgebra
JPanel
Factorial
JLabel
Number:
JTextField
jtfFactorialNumber
JButton
Calculate
jbCalculateFactorial
JLabel
Result:
JTextField
jpFactorial
jtfFactorialResult
Co
Tex
ntr
Name
t
ol
Per
JP
jpPer
mu
an
mutati
tati
el
on
on
JL
ab n:
el
JTe
xtF
0
iel
d
jtfPer
mutati
onN
JL
ab r:
el
JTe
xtF
0
iel
d
jtfPer
mutati
onR
jbCalc
Bu Cal
ulateP
tto cul
ermut
n ate
ation
JL Res
ab
ult:
el
JTe
xtF
0
iel
d
jtfPer
mutati
onRes
ult
Co
Tex
ntr
Name
t
ol
Per
JP
jpCom
mu
an
binatio
tati
el
n
on
JL
ab n:
el
JT
ex
0
tFi
eld
jtfCom
binatio
nNum
ber
JL
ab r:
el
jbCalc
Bu Cal
ulateC
tto cul
ombin
n ate
ation
JL
Res
ab
ult:
el
JT
ex
0
tFi
eld
jtfPer
mutati
onRes
ult
}
catch(Exception exc) {
JOptionPane.showMessageDialog(null, "Invalid
Number");
}
}
35. Return to the form
36. Access the Combination tab page and double-click its Calculate button
n = Long.parseLong(jtfCombinationN.getText());
}
catch(Exception exc) {
JOptionPane.showMessageDialog(null, "Invalid
Number");
}
try {
r = Long.parseLong(jtfCombinationR.getText());
result = Algebra.Factors.Combinatorial(n, r);
jtfCombinationResult.setText(Long.toString(result));
}
catch(Exception exc) {
JOptionPane.showMessageDialog(null, "Invalid
Number");
}
}
38. Return to the form
39. Add a JButton and change its characteristics as follows:
title: Close
name: jbClose
40. Double-click the button
41. Implement the event as follows:
private void jbCloseActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
42. Press F6 to execute the application
43. Test the calculations with different values: