OOP – ASSIGNMENT XIV

Lab Manual Download: Create an applet with three text Fields and four buttons add, subtract, multiply and divide. User will enter two values in the Text Fields. When any button is pressed, the corresponding operation is performed and the result is displayed in the third Text Fields.

ASSIGNMENT NO 14

 Title: Demonstration of Applet and basic Swing components

Objectives: To learn and demonstrated use of applet and swing components

Problem Statement:  Create an applet with three text Fields and four buttons add, subtract, multiply and divide. User will enter two values in the Text Fields. When any button is pressed, the corresponding operation is performed and the result is displayed in the third Text Fields.

Outcomes: 1) Students will be able to demonstrate applet

2) Students will be able to demonstrate basic swing components

Hardware requirements: Any CPU with Pentium Processor or similar, 256 MB RAM or more, 1 GB Hard Disk or more.

Software requirements:  64 bit Linux/Windows Operating System, JDK 1.4 or later

Theory:

An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal. There are some important differences between an applet and a standalone Java application, including the following −

  • An applet is a Java class that extends the java.applet.Applet class.
  • A main() method is not invoked on an applet, and an applet class will not define main().
  • Applets are designed to be embedded within an HTML page.
  • When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user’s machine.
  • A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.
  • The JVM on the user’s machine creates an instance of the applet class and invokes various methods during the applet’s lifetime.
  • Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed.
  • Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.

Life Cycle of an Applet: Four methods in the Applet class gives you the framework on which you build any serious applet −

  • init− This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.
  • start− This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.
  • stop− This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.
  • destroy− This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.
  • paint− Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.

Advantage of Applet

There are many advantages of applet. They are as follows:

  • It works at client side so less response time.
  • Secured
  • It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc.

Drawback of Applet

  • Plugin is required at client browser to execute applet.

Invoking an Applet: An applet may be invoked by embedding directives in an HTML file and viewing the file through an applet viewer or Java-enabled browser.

The <applet> tag is the basis for embedding an applet in an HTML file.

JApplet class in Applet: As we prefer Swing to AWT. Now we can use JApplet that can have all the controls of swing. The JApplet class extends the Applet class.

Event Handling: Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism has a code which is known as an event handler, that is executed when an event occurs. Java uses the Delegation Event Model to handle the events. This model defines the standard mechanism to generate and handle the events.

The Delegation Event Model has the following key participants.

  • Source− The source is an object on which the event occurs. Source is responsible for providing information of the occurred event to it’s handler. Java provide us with classes for the source object.
  • Listener− It is also known as event handler. The listener is responsible for generating a response to an event. From the point of view of Java implementation, the listener is also an object. The listener waits till it receives an event. Once the event is received, the listener processes the event and then returns.

The benefit of this approach is that the user interface logic is completely separated from the logic that generates the event. The user interface element is able to delegate the processing of an event to a separate piece of code.

In this model, the listener needs to be registered with the source object so that the listener can receive the event notification. This is an efficient way of handling the event because the event notifications are sent only to those listeners who want to receive them.

Steps Involved in Event Handling

Step 1 − The user clicks the button and the event is generated.

Step 2 − The object of concerned event class is created automatically and information about the source and the event get populated within the same object.

Step 3 − Event object is forwarded to the method of the registered listener class.

Step 4 − The method is gets executed and returns.

Conclusion : In this assignment, we have studied and demonstrated applet, event handling and swing basic components.

Roll No. Name of Student Date of Performance Date of Assessment Grade Sign of Student Sign of Faculty

PROGRAM

/*********************CalculatorApplet.java*********************/
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 public class CalculatorApplet extends JApplet implements ActionListener{
 JTextField num1,num2,num3;
 JLabel lnum1,lnum2,lnum3;
 JButton add1,sub,mul,div,mod,clear;

public void init() {
 getContentPane().setLayout(new FlowLayout());
 lnum1=new JLabel("First Number");
 lnum2=new JLabel("Second Number");
 lnum3=new JLabel("Answer ");
 num1=new JTextField("0.0",10);
 num2=new JTextField("0.0",10);
 num3=new JTextField("0.0",22);
 num3.setEnabled(false);
 add1=new JButton("+");
 sub=new JButton("-");
 mul=new JButton("*");
 div=new JButton("/");
 mod=new JButton("%");
 clear=new JButton("Reset");

add1.addActionListener(this);
 sub.addActionListener(this);
 mul.addActionListener(this);
 div.addActionListener(this);
 mod.addActionListener(this);
 clear.addActionListener(this);

getContentPane().add(lnum1);
 getContentPane().add(num1);
 getContentPane().add(lnum2);
 getContentPane().add(num2);
 getContentPane().add(lnum3);
 getContentPane().add(num3);
 getContentPane().add(add1);
 getContentPane().add(sub);
 getContentPane().add(mul);
 getContentPane().add(div);
 getContentPane().add(mod);
 getContentPane().add(clear);
 }
 public void actionPerformed(ActionEvent e)
 {
 Object o=e.getSource();
 double n1=0,n2=0,ans=0;
 n1=Double.parseDouble(num1.getText());
 n2=Double.parseDouble(num2.getText());
 if(o==add1)
 ans=n1+n2;
 else if(o==sub)
 ans=n1-n2;
 else if(o==mul)
 ans=n1*n2;
 else if(o==div)
 ans=n1/n2;
 else if(o==mod)
 ans=n1%n2;
 num3.setText(String.valueOf(ans));
 if(o==clear)
 {
 num1.setText("0.0");
 num2.setText("0.0");
 num3.setText("0.0");
 }
}
}
/**************************calciapplet.html***************************/
<html>
    <head>
        <title>Calculator Applet</title>
        </head>
    <body>
        <applet code="CalculatorApplet.class" width=750 height=400>            
        </applet>
    </body>
</html>

OUTPUT

Leave a Reply

Your email address will not be published. Required fields are marked *