Question 1- Write a Java program to create an applet to find the sum of three numbers. Use proper GUI components and layout in your program.
Solution -
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class three_sum_applet extends Applet implements ActionListener
{
Label l1 = new Label("First Number");
Label l2 = new Label("Second Number");
Label l3 = new Label("Third Number");
TextField t1 = new TextField(5);
TextField t2 = new TextField(5);
TextField t3 = new TextField(5);
Button b1 = new Button("Add");
Font f1 = new Font("Arial",Font.BOLD, 25);
String str="";
public void init()
{
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
b1.addActionListener(this);
}
public void paint(Graphics g)
{
g.setFont(f1);
g.drawString(str,130,100);
}
public void actionPerformed(ActionEvent e)
{
int num1 = Integer.parseInt(t1.getText());
int num2 = Integer.parseInt(t2.getText());
int num3 = Integer.parseInt(t3.getText());
int sum = num1+num2+num3;
str = "Sum of three number ="+sum;
repaint();
}
}
/* <applet code="three_sum_applet.class" width="520" height="350"></applet> */
filename: three_sum_applet.java
For compiling the file type this command in cmd: javac three_sum_applet.javaFor execution of the file use this command: appletviewer three_sum_applet.java
Output:
if you have any doubt related to this post, you can ask in comment section and if you want solution of any previous year questions, please post that question in section we will try to solve that

0 comments:
Post a Comment