User Interface that allows user to perform
Integer Division

Aim:
To write a program that creates a user interface to perform integer divisions. The user enters two numbers in the textfields,Num1 and Num2.The division of Num1 and Num2 is displayed in the Result field when the Divide button is clicked. If Num1 or Num2 were not an integer, the program would throw a Number Format Exception. If Num2 were Zero, the program would throw an Arithmetic Exception Display the exception in a message dialog box.

Description:
Create an user interface using swing components. First create a class by extending JFrame class of javax.swing package and implement ActionListener interface to perform an action when the button is clicked on the frame. Create three text fields, a button and a label and attach those components onto the getContentPane() of JFrame.
Attach action Listener to the button and provide action Performed() method to perform the
operation division. When an exception is raised then display that information in a JOptionPane.

Program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ARDemo extends JFrame implements ActionListener
{ JTextField tf1, tf2, tf3;
JButton b;
JLabel l;
ARDemo()
{ Container c = getContentPane();
c.setLayout(new FlowLayout());
l=new JLabel("Enter Numbers and press divide button");
tf1=new JTextField("",5);
tf2=new JTextField("",5);
tf3=new JTextField("",5);
b=new JButton("Divide");
c.add(l);
c.add(tf1);
c.add(tf2);
c.add(b);
c.add(tf3);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand()=="Divide")
{ try
{
int n1=Integer.parseInt(tf1.getText());
int n2=Integer.parseInt(tf2.getText());
int n=n1/n2;
tf3.setText(""+n);
}
catch(ArithmeticException e1)
{
JOptionPane.showMessageDialog (null,"Arthimetic Exception");
}
catch(NumberFormatException e2)
{
JOptionPane.showMessageDialog(null,"NumberFormatException");
}
}
}
public static void main(String args[])
{ ARDemo ob = new ARDemo();
ob.setSize(800,600);
ob.setVisible(true);
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}