Example Application: Algebra
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
Practical Learning: Creating the Application
1. Start NetBeans
2. To create a new application, on the main menu, click File -> New Project...
3. In the first page of the wizard, make sure Java then Java Application is selected. Click Next
4. In the Project Name, type Algebra1
5. Clear the Create Main Class check box
6. Click Finish
7. In the Projects window, right-click Algebra1 -> New -> JFrame Form...
8. Set the Class Name to Central
9. In the Package box, type Accessories
10. Click Finish
11. In the Projects window, right-click Algebra1 -> New -> Java Class...
12. Set the Class Name to Factors
13. In the Package box, type Algebra
14. Click Finish
15. Change the file as follows:
using System;
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;
}
}
return Factorial(a) / (Factorial(b) * Factorial(a - b));
16. In the Projects window, double-click Central
17. In the Properties window, change the following characteristics:
title: Factorial, Permutation, and Combination
preferedSize: 304, 208
18. In the Swing Containers section of the Palette, click Tabbed Pane
19. Click the form
20. In the Swing section of the Palette, click Panel
21. Click the tabbed pane on the form
22. Complete the design as follows:
Control
Text
JTabbedPane
Name
jtfAlgebra
JPanel
Factorial
JLabel
Number:
JTextField
jtfFactorialNumber
JButton
Calculate
jbCalculateFactorial
JLabel
Result:
JTextField
jpFactorial
jtfFactorialResult
23. In the Swing section of the Palette, click Panel
24. On the form, click on the right side of the Factorial tab
25. Continue the design as follows:
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
26. In the Swing section of the Palette, click Panel
27. On the form, click on the right side of the Permutation tab
28. Continue the design as follows:
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
29. Access the Factorial tab page
30. Right-click its Calculate button -> Events -> Action -> actionPerformed
31. Implement the event as follows:
package Accessories;
import [Link];
import [Link];
public class Central extends [Link] {
. . . No Change
private void
jbFactorialCalculateActionPerformed([Link] evt) {
long number = 0;
long result;
try
{
number =
[Link]([Link]());
result = [Link](number);
[Link]([Link](result));
}
catch(Exception exc)
{
[Link](null, "Invalid
Number");
}
}
32. Click the Design button to return to the form
33. Access the Permutation tab page and double-click its Calculate button
34. Implement the event as follows:
private void
jbCalculatePermutationActionPerformed([Link] evt)
{
long n = 0, r = 0;
long result;
try {
n = [Link]([Link]());
}
catch(Exception exc) {
[Link](null, "Invalid
Number");
}
try {
r = [Link]([Link]());
result = [Link](n, r);
[Link]([Link](result));
}
catch(Exception exc) {
[Link](null, "Invalid
Number");
}
}
35. Return to the form
36. Access the Combination tab page and double-click its Calculate button
37. Implement the event as follows:
private void
jbCalculateCombinationActionPerformed([Link] evt)
{
long n = 0, r = 0;
long result;
try {
n = [Link]([Link]());
}
catch(Exception exc) {
[Link](null, "Invalid
Number");
}
try {
r = [Link]([Link]());
result = [Link](n, r);
[Link]([Link](result));
}
catch(Exception exc) {
[Link](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([Link] evt) {
dispose();
}
42. Press F6 to execute the application
43. Test the calculations with different values:
44. Close the form and return to your programming environment