Practical Learning: Creating the Application |
|
- Start NetBeans
- To create a new application, on the main menu, click File -> New
Project...
- In the first page of the wizard, make sure Java then Java Application is
selected. Click Next
- In the Project Name, type Algebra1
- Clear the Create Main Class check box
- Click Finish
- In the Projects window, right-click Algebra1 -> New -> JFrame Form...
- Set the Class Name to
Central
- In the Package box, type Accessories
- Click Finish
- In the Projects window, right-click Algebra1 -> New -> Java Class...
- Set the Class Name to
Factors
- In the Package box, type Algebra
- Click Finish
- 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));
}
}
}
|
- In the Projects window, double-click Central
- In the Properties window, change the following characteristics:
title: Factorial, Permutation, and Combination
preferedSize: 304, 208
- In the Swing Containers section of the Palette, click Tabbed Pane
- Click the form
- In the Swing section of the Palette, click Panel
- Click the tabbed pane on the form
- Complete the design as follows:
|
Control |
Text |
Name |
JTabbedPane |
|
jtfAlgebra |
JPanel |
Factorial |
jpFactorial |
JLabel |
Number: |
|
JTextField |
0 |
jtfFactorialNumber |
JButton |
Calculate |
jbCalculateFactorial |
JLabel |
Result: |
|
JTextField |
|
jtfFactorialResult |
|
|
- In the Swing section of the Palette, click Panel
- On the form, click on the right side of the Factorial tab
- Continue the design as follows:
|
Control |
Text |
Name |
JPanel |
Permutation |
jpPermutation |
JLabel |
n: |
|
JTextField |
0 |
jtfPermutationN |
JLabel |
r: |
|
JTextField |
0 |
jtfPermutationR |
Button |
Calculate |
jbCalculatePermutation |
JLabel |
Result: |
|
JTextField |
0 |
jtfPermutationResult |
|
- In the Swing section of the Palette, click Panel
- On the form, click on the right side of the Permutation tab
- Continue the design as follows:
|
Control |
Text |
Name |
JPanel |
Permutation |
jpCombination |
JLabel |
n: |
|
JTextField |
0 |
jtfCombinationNumber |
JLabel |
r: |
|
Button |
Calculate |
jbCalculateCombination |
JLabel |
Result: |
|
JTextField |
0 |
jtfPermutationResult |
|
- Access the Factorial tab page
- Right-click its Calculate button -> Events -> Action -> actionPerformed
- Implement the event as follows:
package Accessories;
import javax.swing.JOptionPane;
import Algebra.Factors;
public class Central extends javax.swing.JFrame {
. . . No Change
private void jbFactorialCalculateActionPerformed(java.awt.event.ActionEvent evt) {
long number = 0;
long result;
try
{
number = Long.parseLong(jtfFactorialNumber.getText());
result = Algebra.Factors.Factorial(number);
jtfFactorialResult.setText(Long.toString(result));
}
catch(Exception exc)
{
JOptionPane.showMessageDialog(null, "Invalid Number");
}
}
|
- Click the Design button to return to the form
- Access the Permutation tab page and double-click its Calculate button
- Implement the event as follows:
private void jbCalculatePermutationActionPerformed(java.awt.event.ActionEvent evt) {
long n = 0, r = 0;
long result;
try {
n = Long.parseLong(jtfPermutationN.getText());
}
catch(Exception exc) {
JOptionPane.showMessageDialog(null, "Invalid Number");
}
try {
r = Long.parseLong(jtfPermutationR.getText());
result = Algebra.Factors.Permutation(n, r);
jtfPermutationResult.setText(Long.toString(result));
}
catch(Exception exc) {
JOptionPane.showMessageDialog(null, "Invalid Number");
}
}
|
- Return to the form
- Access the Combination tab page and double-click its Calculate button
- Implement the event as follows:
private void jbCalculateCombinationActionPerformed(java.awt.event.ActionEvent evt) {
long n = 0, r = 0;
long result;
try {
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");
}
}
|
- Return to the form
- Add a JButton and change its characteristics as follows:
title: Close
name: jbClose
- Double-click the button
- Implement the event as follows:
private void jbCloseActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
|
- Press F6 to execute the application
- Test the calculations with different values:
- Close the form and return to your programming environment
|
|