Program to Implement Mini Calculator using Java.
Source Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code=MiniCalculator.class width=350 height=180>
</applet>
*/
public class MiniCalculator extends java.applet.Applet implements ActionListener
{
Label no1,no2,result;
Button btn_add,btn_minus,btn_mul,btn_devid;
TextField n1, n2,res;
Panel p_b1,p_res,p1,p_val;
public void init()
{
p_b1=new Panel();
p_res=new Panel();
p_val=new Panel();
p1=new Panel();
p1.setBackground(Color.pink);
setBackground(Color.pink);
p_val.setLayout(new GridLayout(2, 2));
p1.setLayout(new GridLayout(4, 1));
no1=new Label("Enter 1st Number : ");
no2=new Label("Enter 2nd Number : ");
result=new Label("Result is : ");
n1 = new TextField(8);
n2 = new TextField(8);
res = new TextField(8);
btn_add=new Button("+");
btn_minus=new Button("-");
btn_mul=new Button("*");
btn_devid=new Button("/");
btn_add.addActionListener(this);
btn_minus.addActionListener(this);
btn_mul.addActionListener(this);
btn_devid.addActionListener(this);
p_val.add(no1);
p_val.add(n1);
p_val.add(no2);
p_val.add(n2);
p1.add(p_val);
p_b1.add(btn_add);
p_b1.add(btn_minus);
p_b1.add(btn_mul);
p_b1.add(btn_devid);
p1.add(p_b1);
p_res.add(result);
p_res.add(res);
p1.add(p_res);
add(p1);
}
public void actionPerformed(ActionEvent ae)
{
float no1,no2,r=0;
no1=Integer.parseInt(n1.getText());
no2=Integer.parseInt(n2.getText());
String m=ae.getActionCommand();
if(m.equals("+"))
{
r=no1+no2;
}
if(m.equals("-"))
{
r=no1-no2;
}
if(m.equals("*"))
{
r=no1*no2;
}
if(m.equals("/"))
{
r=no1/no2;
}
res.setText(r+"");
}
}
import java.applet.*;
import java.awt.event.*;
/*
<applet code=MiniCalculator.class width=350 height=180>
</applet>
*/
50% OFF Sale ::Java 8 New Features
{
Label no1,no2,result;
Button btn_add,btn_minus,btn_mul,btn_devid;
TextField n1, n2,res;
Panel p_b1,p_res,p1,p_val;
public void init()
{
p_b1=new Panel();
p_res=new Panel();
p_val=new Panel();
p1=new Panel();
p1.setBackground(Color.pink);
setBackground(Color.pink);
p_val.setLayout(new GridLayout(2, 2));
p1.setLayout(new GridLayout(4, 1));
no1=new Label("Enter 1st Number : ");
no2=new Label("Enter 2nd Number : ");
result=new Label("Result is : ");
n1 = new TextField(8);
n2 = new TextField(8);
res = new TextField(8);
btn_add=new Button("+");
btn_minus=new Button("-");
btn_mul=new Button("*");
btn_devid=new Button("/");
btn_add.addActionListener(this);
btn_minus.addActionListener(this);
btn_mul.addActionListener(this);
btn_devid.addActionListener(this);
p_val.add(no1);
p_val.add(n1);
p_val.add(no2);
p_val.add(n2);
p1.add(p_val);
p_b1.add(btn_add);
p_b1.add(btn_minus);
p_b1.add(btn_mul);
p_b1.add(btn_devid);
p1.add(p_b1);
p_res.add(result);
p_res.add(res);
p1.add(p_res);
add(p1);
}
public void actionPerformed(ActionEvent ae)
{
float no1,no2,r=0;
no1=Integer.parseInt(n1.getText());
no2=Integer.parseInt(n2.getText());
String m=ae.getActionCommand();
if(m.equals("+"))
{
r=no1+no2;
}
if(m.equals("-"))
{
r=no1-no2;
}
if(m.equals("*"))
{
r=no1*no2;
}
if(m.equals("/"))
{
r=no1/no2;
}
res.setText(r+"");
}
}
- Save the file with name MiniCalculator.java
- Compile with: javac MiniCalculator.java
- Run with:appletviewer MiniCalculator.java
No comments:
Post a Comment