Best writers. Best papers. Let professionals take care of your academic papers

Order a similar paper and get 15% discount on your first order with us
Use the following coupon "FIRST15"
ORDER NOW

Hello. I am working on a project that calculates a math equation using

Hello. I am working on a project that calculates a math equation using

Recursion and Iteration with a GUI. I have my code written, but my GUI is not showing anything. I’m not sure what’s happening. Any help would be greatly appreciated.

import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; // Extend JPanel and implement ActionListener public class Project3 extends JPanel implements ActionListener { // private JRadioButton iterationButton; private JRadioButton recursionButton; private JTextField inputText; private JTextField outputText; // Constructor public Project3(){ super(); setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); // Create UI Panel createLeftPanel(); createRightPanel(); } // Create left panel public void createLeftPanel(){ JPanel leftPanel = new JPanel(); leftPanel.setPreferredSize(new Dimension(240, 440)); leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS)); leftPanel.add(Box.createVerticalStrut(20)); leftPanel.add(new JLabel(“Enter i:”)); add(leftPanel); } // Create right panel public void createRightPanel(){ JPanel rightPanel = new JPanel(); rightPanel.setPreferredSize(new Dimension (240, 440)); rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS)); // Radio button this.iterationButton = new JRadioButton(“Iterative”, true); this.recursionButton = new JRadioButton(“Recursive”); // Button group ButtonGroup group = new ButtonGroup(); group.add(this.iterationButton); group.add(this.recursionButton); // Radio buttons rightPanel.add(this.iterationButton); rightPanel.add(Box.createVerticalStrut(20)); rightPanel.add(this.recursionButton); rightPanel.add(Box.createVerticalStrut(20)); // Input textfield this.inputText = new JTextField(); this.inputText.setMaximumSize(new Dimension(350, 20)); rightPanel.add(this.inputText); rightPanel.add(Box.createVerticalStrut(20)); JButton calculateButton = new JButton(“Calculate: “); calculateButton.addActionListener(this); calculateButton.setPreferredSize(new Dimension(170, 20)); calculateButton.setMaximumSize(new Dimension(170, 20)); rightPanel.add(calculateButton); rightPanel.add(Box.createVerticalStrut(20)); // Output text field this.outputText = new JTextField(); this.outputText.setMaximumSize(new Dimension(350, 20)); this.outputText.setEditable(false); rightPanel.add(this.outputText); rightPanel.add(Box.createVerticalStrut(20)); // Add right panel to the main panel add(rightPanel); } /** * Computes equation iteratively * @param i * @return */ private double calculateIterative(int i){ double result = 0; for (int n = 1; n<= i; n++){ result += n/((2.0*n) + 1); } return result; } private double calculateRecursive(int i){ if (i == 0) return 0; double result = 0; result = (i/((2.0* i) + 1)) + calculateRecursive(i-1); return result; } @Override public void actionPerformed(ActionEvent event) { if(!inputText.getText().trim().equals(“”)){ try { int i = Integer.parseInt(inputText.getText().trim()); if(iterationButton.isSelected()) { outputText.setText(String.valueOf(calculateIterative(i))); } else if (recursionButton.isSelected()){ outputText.setText(String.valueOf(calculateRecursive(i))); } } catch (NumberFormatException nfe) { JOptionPane.showMessageDialog(this, “Invalid input. Please try again.”); } } } public static void main(String[] args) { // Create frame JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(350, 250); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setVisible(true); } }

 
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"