Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Take the code below and modify it so that it handles exceptions. The method pars

ID: 3560446 • Letter: T

Question

Take the code below and modify it so that it handles exceptions. The method parseInt throws an exception and your code will handle the exception by outputing an error message (you could use JOptionPane for this) telling the user what they did wrong. Try before adding the new code and type non-numeric information in the text field. This is one way to find out what kind of exception is thrown. You could also look up the parseInt method in the Oracle documentation.

// Interface

interface FactInterface

{

public int getFactorial(int number);

}

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class JFrameFact extends JFrame implements FactInterface

{

private JFrame mainFrame;

private JPanel controlPanel;

private JLabel result,lb1;

private JTextField tf;

  

public static void main(String[] args)

{

JFrameFact ff = new JFrameFact();

ff.prepareGUI();

ff.mathCalculations();

}

public void prepareGUI()

{

mainFrame = new JFrame("Calculating Factorial Using Java Swings");

mainFrame.setSize(500,300);

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainFrame.setLayout(new BorderLayout());

controlPanel = new JPanel(new GridLayout(2,2));

mainFrame.add(controlPanel);

mainFrame.setVisible(true);

}

// Override the interface Method

@Override

public int getFactorial(int number)

{

int fact = 1;

if(number == 0)

return 1;

else

{

for(int i = number; i >= 1; i--)

{

fact = fact * i;

}

}

return fact;

}


public void mathCalculations()

{

result = new JLabel("",JLabel.CENTER);

lb1 = new JLabel("Enter Number : ",JLabel.CENTER);

tf = new JTextField(5);

JButton fact = new JButton("Calculate Factorial");

fact.addActionListener(new MyListener());

controlPanel.add(lb1);

controlPanel.add(tf);

controlPanel.add(fact);

controlPanel.add(result);

mainFrame.add(controlPanel);

mainFrame.setVisible(true);


}

class MyListener implements ActionListener

{

@Override

public void actionPerformed(ActionEvent e)

{

int number = Integer.parseInt(tf.getText());

if(number < 0)

{

JOptionPane.showMessageDialog(controlPanel, "Number Cannot be Negetive");

}


else

{

int factorial = new JFrameFact().getFactorial(number);

result.setText("Factorial of "+number+" is : "+factorial);

}

}

  }

}

Explanation / Answer


import java.awt.*;
import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

interface FactInterface

{

public int getFactorial(int number);

}

public class JFrameFact extends JFrame implements FactInterface

{

private JFrame mainFrame;

private JPanel controlPanel;

private JLabel result,lb1;

private JTextField tf;

public static void main(String[] args)

{

JFrameFact ff = new JFrameFact();

ff.prepareGUI();

ff.mathCalculations();

}

public void prepareGUI()

{

mainFrame = new JFrame("Calculating Factorial Using Java Swings");

mainFrame.setSize(500,300);

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainFrame.setLayout(new FlowLayout());

controlPanel = new JPanel(new GridLayout(2,2));

mainFrame.add(controlPanel);

mainFrame.setVisible(true);

}

// Override the interface Method

@Override

public int getFactorial(int number)

{

int fact = 1;

if(number == 0)

return 1;

else

{

for(int i = number; i >= 1; i--)

{

fact = fact * i;

}

}

return fact;

}


public void mathCalculations()

{

result = new JLabel("",JLabel.CENTER);

lb1 = new JLabel("Enter Number : ",JLabel.CENTER);

tf = new JTextField(5);

JButton fact = new JButton("Calculate Factorial");

fact.addActionListener(new MyListener());

controlPanel.add(lb1);

controlPanel.add(tf);

controlPanel.add(fact);

controlPanel.add(result);

mainFrame.add(controlPanel);

mainFrame.setVisible(true);


}

class MyListener implements ActionListener

{

@Override

public void actionPerformed(ActionEvent e)

{

int number = Integer.parseInt(tf.getText());

if(number < 0)

{

JOptionPane.showMessageDialog(controlPanel, "Number Cannot be Negetive");

}

else

{

int factorial = new JFrameFact().getFactorial(number);

result.setText("Factorial of "+number+" is : "+factorial);

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote