JAVA APPLICATION - APPLET


AIM:
    To write a Java program to implement applet.

ALGORITHM:
Step 1: Start
Step 2: Define the class appletcalculator which extends applet and implements ActionListner
Step 3: Initialize m as string
Step 4: Declare Add, Sub, Mul, Div as Button
Step 5: Declare opr1, opr2, ans as TextField
Step 6: Define the function init()
Step 7: Initialize Add, Sub, Mul, Div, opr1, opr2, ans
Step 8: Set the Layout
Step 9: Call the method add by passing the parameters Add, Sub, Mul, Div, opr1, opr2, ans
            seperately
Step 10: Add ActionListener to Add, Sub, Mul, Div, opr1, opr2, ans
Step 11: Define the method actionPerformed()
Step 12: Perform the operations for the buttons Add, Sub, Mul, Div
Step 13: Call the method repaint()
Step 14: Define the method paint()
Step 15: Stop

SOURCE CODE:
appletcalculator.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class appletcalculator extends Applet implements ActionListener
{
String m="";
Button Add,Sub,Mul,Div;
TextField opr1,opr2,ans;
public void init()
{
Add=new Button("+");
Sub=new Button("-");
Mul=new Button("*");
Div=new Button("/");
opr1=new TextField("Enter 1st operand",20);
opr2=new TextField("Enter 2nd operand",20);

ans=new TextField(30);
setLayout(new FlowLayout(FlowLayout.LEFT));
add(opr1);
add(opr2);
add(ans);
add(Add);
add(Sub);
add(Mul);
add(Div);
Add.addActionListener(this);
Sub.addActionListener(this);
Mul.addActionListener(this);
Div.addActionListener(this);
opr1.addActionListener(this);
opr2.addActionListener(this);
ans.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
int op1;
int op2;
float result;

String temp;
if(s.equals("+"))
{
m="You have pressed Add";
op1=Integer.parseInt(opr1.getText());
op2=Integer.parseInt(opr2.getText());
result=(float)op1+op2;
temp="The ans is "+result;
ans.setText(temp);
}
else if(s.equals("-"))
{
m="You have pressed Sub";
op1=Integer.parseInt(opr1.getText());
op2=Integer.parseInt(opr2.getText());
result=(float)op1-op2;
temp="The ans is "+result;
ans.setText(temp);
}
else if(s.equals("*"))
{
m="You have pressed Mul";
op1=Integer.parseInt(opr1.getText());
op2=Integer.parseInt(opr2.getText());
result=(float)op1*op2;
temp="The ans is "+result;
ans.setText(temp);
}
else if(s.equals("/"))
{
m="You have pressed Div";
op1=Integer.parseInt(opr1.getText());
op2=Integer.parseInt(opr2.getText());
result=(float)op1/op2;
temp="The ans is "+result;
ans.setText(temp);
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(m,100,200);
g.setColor(Color.red);
}
}

appletcalculator.html
<APPLET CODE=appletcalculator.class HEIGHT=500 WIDTH=500>
</APPLET>
 
OUTPUT:


RESULT:
          Thus the Java program for the implementation of applet was performed and the output was verified.

.
Previous
Next Post »

Still not found what you are looking for? Try again here.