Home

Example Application: Algebra

Permutation

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 LearningPractical 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:
     
    Algebra
    Control Text Name
    JTabbedPane   jtfAlgebra
    JPanel Factorial jpFactorial
    JLabel Number:  
    JTextField jtfFactorialNumber
    JButton Calculate jbCalculateFactorial
    JLabel Result:  
    JTextField   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:
     
    Algebra
    Control Text Name
    JPanel Permutation jpPermutation
    JLabel n:  
    JTextField 0 jtfPermutationN
    JLabel  r:  
    JTextField  0 jtfPermutationR
    Button Calculate jbCalculatePermutation
    JLabel Result:  
    JTextField 0 jtfPermutationResult
  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:
     
    Algebra
    Control Text Name
    JPanel Permutation jpCombination
    JLabel n:  
    JTextField 0 jtfCombinationNumber
    JLabel  r:  
    Button Calculate jbCalculateCombination
    JLabel Result:  
    JTextField 0 jtfPermutationResult
  29. Access the Factorial tab page
  30. Right-click its Calculate button -> Events -> Action -> actionPerformed
  31. 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");
            }
    }
  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(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");
            }
    }
  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(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");
            }
        }
  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:
     
    Factorial
    Permutation
    Combinatorial
  44. Close the form and return to your programming environment
 

Home Copyright © 2009 FunctionX, Inc.