Numbers 1.0



 

 

Complete Source Code

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Arrays;


public class Numbers {


   private static JFrame myFrame;
   private static JPanel myPanel;
   private static MenuListener menuListener;
   private static FieldListener fieldListener;
   private static JMenuBar menuBar;
   private static JMenu file, methods, help;
   private static JMenuItem exit, aboutNumbers, aboutCollatz;
   private static JMenuItem[] methodItem;
   private static JLabel titleLabel, questionLabel, answerLabel, copyright;
   private static JTextField inputField;
   private static JButton enter;
   private static JTextArea outputArea;
   private static JScrollPane scrollPane;
   private static int method;
   private static String[] title;
   private static String message1, message2;

   public static class MenuListener implements ActionListener {

      public void actionPerformed(ActionEvent e) {

         Object source = e.getSource();

         if (source == exit) System.exit(0);
         if (source == aboutNumbers) JOptionPane.showMessageDialog(myPanel, message1);
         if (source == aboutCollatz) JOptionPane.showMessageDialog(myPanel, message2);
         for (int i = 0; i < 7; i++) if (source == methodItem[i]) method = i;
         titleLabel.setText(title[method]);
         inputField.setText(null);
         outputArea.setText(null);

      }

   }

   public static class FieldListener implements ActionListener {

      public void actionPerformed(ActionEvent e) {

         int N;

         outputArea.setText(null);

         try {

            N = Integer.parseInt(inputField.getText());

            if (N < 1) {

               inputField.setText("Invalid Entry!");
               N = 0;

            }

         }

         catch (NumberFormatException exp) {

            inputField.setText("Invalid Entry!");
            N = 0;

         }

         if (method == 0 && N > 0) {

            if (isPrime(N)) outputArea.setText(N + " is a prime number.");
            if (!isPrime(N)) outputArea.setText(N + " is NOT a prime number.");

         }

         if (method == 1 && N > 0) {

            String str = Arrays.toString(divisors(N));
            outputArea.setText(str.substring(1,str.length()-1));

         }

         if (method == 2 && N > 0) outputArea.setText(summation(N) + "");
         if (method == 3 && N > 0) outputArea.setText(sumDigits(N) + "");
         if (method == 4 && N > 0) outputArea.setText(reverseDigits(N) + "");
         if (method == 5 && N > 0) outputArea.setText(NthPrime(N) + "");

         if (method == 6 && N > 0) {

            int[] list = collatz(N);
            String str = Arrays.toString(list);
            outputArea.setText(str.substring(1,str.length()-1));
            outputArea.append("\n\nThere are " + list.length + " terms in this sequence.");

         }

      }

   }

   public static boolean isPrime(int N) {

      int n, m, r;

      if (N < 2) return false;
      if (N == 2) return true;
      if (N > 2 && N % 2 == 0) return false;

      if (N > 2 && N % 2 != 0) {

         n = 2;
         m = 3;
         r = 1;

         while (m <= Math.sqrt(N) && r > 0) {

            r = N % m;
            m = 2*n + 1;
            n++;

         }

         if (r > 0) return true;
         else return false;

      }

      return false;

   }

   public static int[] divisors(int N) {

      int[] list = new int[(int)N/2];
      int divisorCount = 0;

      for (int i = 1; i <= N/2; i++) {

         if (N % i == 0) {

            list[divisorCount] = i;
            divisorCount++;

         }

      }

      int[] copy = new int[divisorCount+1];
      for (int i = 0; i < divisorCount; i++) copy[i] = list[i];
      copy[divisorCount] = N;
      return copy;

   }

   public static int summation(int N) {

      int sum = 0;
      for (int i = 1; i <= N; i++) sum += i;
      return sum;

   }

   public static int sumDigits(int N) {

      int sum = 0;

      while (N != 0) {

         sum += N % 10;
         N /= 10;

      }

      return sum;

   }

   public static int reverseDigits(int N) {

      int digit, revN;

      revN = 0;

      if (N < 10) revN = N;

      else {

         do {

            digit = N % 10;
            revN = 10*(revN + digit);
            N /= 10;

         } while (N >= 10);

         revN += N;

      }

      return revN;

   }

   public static int NthPrime(int N) {

      int n, count;

      n = 1;
      count = 0;

      do {

         n++;
         if (isPrime(n)) count++;

      } while (count < N);

      return n;

   }

   public static int[] collatz(int N) {

      int[] list = new int[1000];
      int terms = 1;

      list[0] = N;

      while (N != 1) {

         if (N % 2 == 0) N /= 2;
         else N = 3*N + 1;

         list[terms] = N;
         terms++;

      }

      int[] copy = new int[terms];
      for (int i = 0; i < terms; i++) copy[i] = list[i];
      return copy;

   }

   public static void main(String[] args) {

      myFrame = new JFrame("Numbers 1.0");
      myPanel = new JPanel();
      menuListener = new MenuListener();
      fieldListener = new FieldListener();
      menuBar = new JMenuBar();
      file = new JMenu("File");
      methods = new JMenu("Methods");
      help = new JMenu("Help");
      exit = new JMenuItem("Exit");
      aboutNumbers = new JMenuItem("About Numbers");
      aboutCollatz = new JMenuItem("About 3N + 1");
      methodItem = new JMenuItem[7];
      titleLabel = new JLabel("Prime Number Test");
      questionLabel = new JLabel("Enter a positive integer : ");
      answerLabel = new JLabel("Answer : ");
      copyright = new JLabel("\u00A9 2011 by Dilip Muthukrishnan", JLabel.CENTER);
      inputField = new JTextField();
      enter = new JButton("Enter");
      outputArea = new JTextArea();
      scrollPane = new JScrollPane(outputArea);
      method = 0;
      title = new String[7];
      message1 = "This program was written using the Java programming language. It integrates a\n" +
                 "list of computational methods that I wrote back in February into a Graphical\n" +
                 "User Interface (GUI). The only new technique that I have used in this program\n" +
                 "is the try...catch statement for exception handling.";
      message2 = "The 3N + 1 sequence of a given natural number N is obtained as follows. Starting\n" +
                 "with N, if N is even, divide by 2. If N is odd, compute 3N + 1. Set one of\n" +
                 "these results as the new value of N and repeat this process until N = 1. The\n" +
                 "unproven Collatz Conjecture in Mathematics states that the 3N + 1 sequence\n" +
                 "of any natural number ALWAYS terminates at 1 after only a finite number of steps.";

      title[0] = "Prime Number Test";
      title[1] = "Divisors";
      title[2] = "Summation (1 to N)";
      title[3] = "Sum of Digits";
      title[4] = "Reverse Digits";
      title[5] = "Nth Prime";
      title[6] = "3N + 1 Sequence";

      myPanel.setLayout(null);
      myPanel.setBackground(new Color(255,225,0));
      myPanel.add(titleLabel);
      myPanel.add(questionLabel);
      myPanel.add(answerLabel);
      myPanel.add(copyright);
      myPanel.add(inputField);
      myPanel.add(enter);
      myPanel.add(scrollPane);

      titleLabel.setBounds(10,10,300,25);
      titleLabel.setFont(new Font("TimesRoman",Font.BOLD,18));
      questionLabel.setBounds(10,60,150,20);
      inputField.setBounds(165,60,100,25);
      inputField.setMargin(new Insets(0,5,0,0));
      inputField.addActionListener(fieldListener);
      enter.addActionListener(fieldListener);
      enter.setBounds(275,60,73,25);
      enter.setMargin(new Insets(0,0,0,0));
      answerLabel.setBounds(10,100,60,20);
      scrollPane.setBounds(75,100,275,125);
      outputArea.setMargin(new Insets(5,5,5,5));
      outputArea.setLineWrap(true);

      for (int i = 0; i < 7; i++) {

         methodItem[i] = new JMenuItem(title[i]);
         methodItem[i].addActionListener(menuListener);
         methods.add(methodItem[i]);

      }

      exit.addActionListener(menuListener);
      aboutNumbers.addActionListener(menuListener);
      aboutCollatz.addActionListener(menuListener);
      file.add(exit);
      help.add(aboutNumbers);
      help.add(aboutCollatz);
      menuBar.add(file);
      menuBar.add(methods);
      menuBar.add(help);

      copyright.setForeground(new Color(0,0,150));
      copyright.setBounds(80,260,220,30);

      myFrame.setJMenuBar(menuBar);
      myFrame.setContentPane(myPanel);
      myFrame.setLocation(100,100);
      myFrame.setSize(400,350);
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      myFrame.setResizable(false);
      myFrame.setVisible(true);

   }


}