package TheCalculator; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.math.BigInteger; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.JTextField; public class Main extends JFrame { private static final long serialVersionUID = 1L; public Main() { JTabbedPane tabbedPane = new JTabbedPane(); // Make a TabbedPane for all the content // Construct a JPanel with the following content. Given the type, determine what // to display. JComponent panel1 = makeTextPanel( "Welcome to The Very Useful Caluclator! In the tabs above, access some useful math tools to help you with quick calculations.", "Home"); // Add this JPanel to the TabbedPane tabbedPane.addTab("Home", null, panel1, "Home"); // Make it so that when I press Alt+(number), it goes to that tab. tabbedPane.setMnemonicAt(0, KeyEvent.VK_1); // Do the same for every other tab, changing the content and type to customize // each. JComponent panel2 = makeTextPanel( "Enter a positive whole number less than 2147483647 below and this will calculate whether or not it is a prime number.", "Check If Prime"); tabbedPane.addTab("Check If Prime", null, panel2, "Check if a number is prime or not."); tabbedPane.setMnemonicAt(1, KeyEvent.VK_2); JComponent panel3 = makeTextPanel( "Enter a positive whole number less than 2147483647 below and this will compute the prime factorization for it.", "Prime Factorize"); tabbedPane.addTab("Prime Factorize", null, panel3, "Generate the prime factorization of a number."); tabbedPane.setMnemonicAt(2, KeyEvent.VK_3); JComponent panel4 = makeTextPanel( "Enter a positive whole number less than 2147483647 below and this will generate all the factors for it.", "Generate Factors"); panel4.setPreferredSize(new Dimension(410, 50)); tabbedPane.addTab("Generate Factors", null, panel4, "Generates all the factors of a number."); tabbedPane.setMnemonicAt(3, KeyEvent.VK_4); JComponent panel5 = makeTextPanel( "Enter a positive whole number less than 2147483647 below and this will calculate its factorial.", "Factorial"); panel4.setPreferredSize(new Dimension(410, 50)); tabbedPane.addTab("Factorial", null, panel5, "Generates the factorial of a number."); tabbedPane.setMnemonicAt(4, KeyEvent.VK_5); // Customize the TabbedPane and display it onto the screen. tabbedPane.setVisible(true); getContentPane().add(tabbedPane, BorderLayout.NORTH); tabbedPane.setBackground(new Color(240, 248, 255)); setTitle("The Very Useful Calculator"); setBounds(0, 0, 1000, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new Main(); } protected static boolean isPrime(int num) { // Determines whether or not a number is prime. boolean isPrime = true; for (int i = 2; i < num; i++) { if (num % i == 0) { isPrime = false; } } return isPrime; } // Most of the code is in here. protected static JComponent makeTextPanel(String text, String type) { // If it's the home page, no need for a button or any of the fancier things, so // run a stripped-down version of code for the home page. if (type.equals("Home")) { JPanel panel = new JPanel(false); panel.setBackground(new Color(255, 255, 224)); // Display the content text onto the page using JLabel JLabel filler = new JLabel(text); filler.setHorizontalAlignment(JLabel.CENTER); panel.setLayout(new GridLayout(1, 1)); panel.add(filler); return panel; } JPanel panel = new JPanel(false); panel.setBackground(new Color(255, 255, 224)); // Display the content text onto the page using JLabel JLabel filler = new JLabel(text); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(filler); JLabel response = new JLabel("Response will show here."); JTextField textField = new JTextField(); // Customize the button with the name of the operation using the variable "type" JButton submitButton = new JButton(type); submitButton.setSize(100, 100); // What to do if the button is pressed. submitButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { int input = 0; boolean proceed = false; // Make sure that the input is an integer. try { input = Integer.parseInt(textField.getText()); proceed = true; } catch (NumberFormatException e) { System.out.println(e.getStackTrace()); // If it's not, display an error message. Use HTML tags to make it have color response.setText( "Please enter a positive whole number less than 2147483647."); } // If it is indeed an integer: if (proceed) { // The first operation type if (type.equals("Check If Prime")) { System.out.println("Check If Prime"); // Deal with the remaining incorrect inputs: negative, 0, or 1 if (input == 0 || input == 1) { response.setText( "No, this number is not prime."); } else if (input < 0) { response.setText( "Please enter a positive whole number less than 2147483647."); } else { response.setText("Calculating..."); // Use the isPrime method to determine our answer. if (isPrime(input)) { response.setText( "Yes, this number is prime."); } else { response.setText( "No, this number is not prime."); } } } else if (type.equals("Prime Factorize")) { System.out.println("Prime Factorize"); String primeFacto = ""; if (input > 1) { if (input <= 2147483647) { primeFacto = "The prime factorization of " + input + " is: "; // code to prime factor input. boolean finished = false; int resultant = input; int i = 2; boolean oneDone = false; // Loop to prime factorize the number. Uses the same process as someone prime // factorizing a number themselves. while (!finished) { if (isPrime(i)) { int count = 0; while (resultant % i == 0) { resultant = resultant / i; count++; } if (oneDone && count > 0) { primeFacto += " x "; } if (count == 1) { primeFacto += i; oneDone = true; } else if (count > 1) { // Add the next prime factor to the output message, using HTML to make // exponents primeFacto += (i + "" + count + ""); oneDone = true; } if (resultant == 1) { finished = true; } if (i == input && count == 1) { primeFacto += "
It is a prime number."; } } i++; } primeFacto += "
"; } else { primeFacto += "Please enter a positive whole number less than 2147483647."; } } else if (input == 1) { primeFacto += "1 has no prime factors."; } else { primeFacto += "Please enter a positive whole number less than 2147483647."; } response.setText(primeFacto); } else if (type.equals("Generate Factors")) { System.out.println("Generate Factors"); String generateFacto = ""; if (input < 0) { generateFacto = "Please enter a positive whole number less than 2147483647."; } else if (input == 0) { generateFacto = "Every number is a factor of 0."; } else { generateFacto = "The factors of " + input + " are: 1"; int count = 1; // Generate all the factors of a number. for (int i = 2; i <= input; i++) { if (input % i == 0) { generateFacto += ", " + i; count++; } } generateFacto += ". The total number of factors is " + count + "."; if (count == 2) { generateFacto += " It is a prime number."; } generateFacto += ""; } response.setText(generateFacto); } else if (type.equals("Factorial")) { System.out.println("Factorial"); // if input is > 12, use BigInteger. String factorial = ""; int ans = 1; if (input < 0) { factorial = "Please enter a positive whole number less than 2147483647."; } else if (input == 0) { factorial = "0! is 1."; } else { factorial = "" + input + "! is "; if (input <= 12) { for (int i = 2; i <= input; i++) { ans *= i; } factorial += ans; } else { // use BigInteger to find factorial BigInteger ansbi = new BigInteger("1"); for (int i = 2; i <= input; i++) { ansbi = ansbi.multiply(new BigInteger(Integer.toString(i))); } factorial += ansbi; } factorial += "."; } response.setText(factorial); } } } }); // Post these onto the screen panel.add(textField); panel.add(submitButton); panel.add(response); return panel; } }