Write a program that asks the user for 2 integer numbers and displays those numb
ID: 3887305 • Letter: W
Question
Write a program that asks the user for 2 integer numbers and displays those numbers added, subtracted, multiplied, divided and modulo. Use a WidgetView object. In your GUI, display a label containing instructions to the user such as "Enter two ints and I'll perform math operations on them." Create two text fields for the user to enter the numbers. Use a button for the user to indicate that he has entered the numbers. When the user clicks the button, display labels showing the results of each operation with an indication of the operation performed, such as "adding 7 and 8 is 15."
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Addition extends JFrame implements ActionListener
{
JLabel lable1, lable3;
JButton b1;
JTextField t1,t2,t3;
Addition ()
{
lable1 = new JLabel("Enter two ints and I'll perform math operations on them.");
lable3 = new JLabel("OUTPUT");
b1=new JButton("Add");
t1=new JTextField(10);
t2=new JTextField(10);
add(lable1);
add(t1);
add(t2);
add(lable3);
add(b1);
b1.addActionListener(this);
setSize(200,200);
setLayout(new FlowLayout());
setTitle("Addition of two numbers");
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
int n1 = Integer.parseInt((t1.getText()));
int n2 = Integer.parseInt((t2.getText()));
int sum = n1 + n2;
String s = String.valueOf(sum);
t3.setText(s);
}
}
public static void main(String args[])
{
Addition add = new Addition ();
add.setVisible(true);
add.setLocation(200,200);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.