Arithmatic Operation in Java Using Applet

by Rakesh on May 23, 2014 · 0 comments

P22

 

In this post a java program using applet.

In above Image is the output of the program. In this program there are 4 Labels, 3 TextBoxes, 1 Button & 1 Combobox. First Two textbox used for enter the number & theird one is to display the result. Combo Box is used for selecting arithmetic operation to perform. After that Press the OK Button to display the Result.

Following Step used for creating this program.

Step 1 : Open Notepad

Step 2 : Write Code (Coding of this program is bellow).

Step 3: Save File with .java Extension.

Step 4: Open Command Prompt.

Step 5: Type Command like: Javac programname.java (Java has been Install First)

Step 6 : Type Another Command : Appletviewer programname.java.

Code of this Project

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet  code="p22" height=200 width=200>
 </applet>*/
public class p22 extends Applet implements ActionListener
{
 TextField t1,t2,t3;
 Label l1,l2,l3,l4;
 Choice c;
 Button b;
 float s=0;
 public void init()
 {
 l1=new Label("Frist No. :-");
 add(l1);
 t1=new TextField(10);
 add(t1);
 
 l2=new Label("Second No. :-");
 add(l2);
 t2=new TextField(10);
 add(t2);
 
 l3=new Label("Result :-");
 add(l3);
 t3=new TextField(10);
 add(t3);
 
 b=new Button("OK");
 add(b);
 
 l4=new Label("Select Operation to Perform :");
 c=new Choice();
 c.add("+");
 c.add("-");
 c.add("*");
 c.add("/");
 add(l4);
 add(c);
 b.addActionListener(this);
 }
 public void actionPerformed(ActionEvent ae)
 {
 if(ae.getSource()==b)
 {
 if(c.getSelectedItem()=="+")
 {
 s=Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText());
 t3.setText(String.valueOf(s));
 }
 if(c.getSelectedItem()=="-")
 {
 s=Integer.parseInt(t1.getText())-Integer.parseInt(t2.getText());
 t3.setText(String.valueOf(s));
 }
 if(c.getSelectedItem()=="*")
 {
 s=Integer.parseInt(t1.getText())*Integer.parseInt(t2.getText());
 t3.setText(String.valueOf(s));
 }
 if(c.getSelectedItem()=="/")
 {
 s=Integer.parseInt(t1.getText())/Integer.parseInt(t2.getText());
 t3.setText(String.valueOf(s));
 }
 }
 }
}

 

Leave a Comment

Previous post:

Next post:

Güncel Haberler