Program Statement: Write a program that creates a user interface to perform integer divisions. The user enters two numbers in the text fields,
Num1 and Num2. The division of Num1 and Num2 is displayed in the Result field when the Divide button is clicked. If Num1 and Num2 were not an integer, The program would throw a NumberFormatException. If Num2 were zero, the program would throw ArithmeticException. Display the Exception in a message dialog box.
Num1 and Num2. The division of Num1 and Num2 is displayed in the Result field when the Divide button is clicked. If Num1 and Num2 were not an integer, The program would throw a NumberFormatException. If Num2 were zero, the program would throw ArithmeticException. Display the Exception in a message dialog box.
Tested on
Software: Windows 8, Jdk 1.6, Netbeans IDE
Hardware: Intel Core i3, 4 GB RAM, 500 GB HDD
Software: Windows 8, Jdk 1.6, Netbeans IDE
Hardware: Intel Core i3, 4 GB RAM, 500 GB HDD
Source
Code
@Author: Praveen
Kanwar
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/* <applet code="DivideDemo" width=500 height=500> </applet? */
public class DivideDemo extends Applet implements ActionListener,TextListener
{
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1;
int x,y;
float res=0;
public void init()
{
l1=new Label("Num1:");
l2=new Label("Num2:");
l3=new Label("Num3:");
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
b1=new Button("Display");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(l3);
add(t3);
b1.addActionListener(this);
t1.addTextListener(this);
t2.addTextListener(this);
}
public void actionPerformed(ActionEvent ae)
{
try
{
int x=Integer.parseInt(t1.getText());
int y=Integer.parseInt(t2.getText());
if(ae.getSource()==b1)
res=x/y;
}
catch(NumberFormatException nfe)
{
javax.swing.JOptionPane.showMessageDialog(this, "Please enter Integer value");
}
catch(ArithmeticException afe)
{
javax.swing.JOptionPane.showMessageDialog(this,"Please enter non-zero value");
}
t3.setText(String.valueOf(res));
}
public void textValueChanged(TextEvent te)
{
if(!t1.getText().equals("")&&(!t2.getText().equals("")))
{
b1.setEnabled(true);
}
else
b1.setEnabled(false);
}
}
import java.awt.*;
import java.awt.event.*;
/* <applet code="DivideDemo" width=500 height=500> </applet? */
public class DivideDemo extends Applet implements ActionListener,TextListener
{
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1;
int x,y;
float res=0;
public void init()
{
l1=new Label("Num1:");
l2=new Label("Num2:");
l3=new Label("Num3:");
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
b1=new Button("Display");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(l3);
add(t3);
b1.addActionListener(this);
t1.addTextListener(this);
t2.addTextListener(this);
}
public void actionPerformed(ActionEvent ae)
{
try
{
int x=Integer.parseInt(t1.getText());
int y=Integer.parseInt(t2.getText());
if(ae.getSource()==b1)
res=x/y;
}
catch(NumberFormatException nfe)
{
javax.swing.JOptionPane.showMessageDialog(this, "Please enter Integer value");
}
catch(ArithmeticException afe)
{
javax.swing.JOptionPane.showMessageDialog(this,"Please enter non-zero value");
}
t3.setText(String.valueOf(res));
}
public void textValueChanged(TextEvent te)
{
if(!t1.getText().equals("")&&(!t2.getText().equals("")))
{
b1.setEnabled(true);
}
else
b1.setEnabled(false);
}
}
Output
Post a Comment