Program
To find the sum of two numbers using applet.
Algorithm
Step 1: Start
Step 2: Write html code for implementing the applet
Step 3: Initialize TextFields t1,t2
Step 4: Start applet by calling init(),paint(),action(Event event,Object obj)
Step 5: Stop
Funtion public void init()
Step 1: Start
Step 2: Add TextFields t1,t2 to applet
Step 3: Exit
Funtion public void paint()
Step 1: Start
Step 2: Initialize x,y,z as 0
Step 3: s1=text from t1
Step 4: x=integer representation of s1
Step 5: s2=text from t2
Step 6: y=integer representation of s2
Step 7: z=x+y
Step 8: s=string representation of z
Step 9: Drawstring s at position (70,50)
Step 10: Exit
Funtion public void action(Event event, Object obj)
Step 1: Start
Step 2: Call repaint()
Step 3: return true
Step 4: Exit
Program
/*<applet code=MyAppAdd.class height=300 width=200> </applet>*/ import java.awt.*; import java.awt.event.*; import java.applet.*; public class MyAppAdd extends Applet implements ActionListener { TextField t1,t2; Button add; public void init() { setSize(300,200); t1=new TextField(5); t2=new TextField(5); add=new Button("SUM"); add(t1); add(t2); add(add); add.addActionListener(this); } public void paint(Graphics g) { int a,b,sum; String s1,s2,s; g.drawString("Input a number in each box",65,50); try { s1=t1.getText(); a=Integer.parseInt(s1); s2=t2.getText(); b=Integer.parseInt(s2); sum=a+b; s=String.valueOf(sum); g.drawString("Sum : " + s,70,80); } catch(Exception e) { System.out.println("Error : "+ e); } } public void actionPerformed(ActionEvent arg0) { repaint(); } } |