JAVA & J2EE Unit 2 Engineered for Tomorrow Prepared by Santhiya.M & Ganga V C Department OF Computer Science and Engineeering.

Slides:



Advertisements
Similar presentations
Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets Running Java applets. Security.
Advertisements

Dept. of Computer Science - SSBN Java Applets Vishnuvardhan.M.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Applets, Graphical User Interfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads.
Java Applets. Lecture Objectives Learn about Java applets. Know the differences between Java applets and applications. Designing and using Java applets.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Java Applets. Applets The term Applet refers to a little application. In JAVA the applet is a java program that is embedded within a HTML document and.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
Program that runs in appletviewer (test utility for applets) Web browser (IE, Communicator) Executes when HTML (Hypertext Markup Language) document containing.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 Applets. 2 Design of Applets 3 Sun wrote Java to be executable within a hosting application browser The applications are applets. An applet is downloaded.
Creating a Java Application and Applet
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 17 : Applets King Fahd University of Petroleum & Minerals College of Computer Science.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Java Applets.
Java Applets.
Java Applets.
Chapter 13: Advanced GUIs and Graphics
30 Java Applets.
Distributed Computing, M. L. Liu
Applets.
UNIT-5.
Java Applets.
Introduction to Computing Using Java
Java Applets.
Java Applets.
Java applets 1/3/2019.
Java – Inheritance.
Applet in Java.
Java Programming COMP-417 Applet
Chap 1. Getting Started Objectives
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Advanced GUIs and Graphics
Presentation transcript:

JAVA & J2EE Unit 2 Engineered for Tomorrow Prepared by Santhiya.M & Ganga V C Department OF Computer Science and Engineeering

Engineered for Tomorrow OVERVIEW CLASS FUNDAMENTALS INHERITANCE EXCEPTION HANDLING JAVA APPLETS

Engineered for Tomorrow CLASS FUNDAMENTALS CLASS: Class creates a new data type Class is used to create object Class is template of the object Object is an instance for the class General form: Class classname{ Type inst-var1; Type inst-var2; //.. Type inst-varn; Type methodname1(parameter-list){ //body of method } Type methodname2(parameter-list){ //body of method } //.. Type methodnamen(parameter-list){ //body of method } Method and instance variable are member of the class Sample class Class box { Int width; Int height; member of Int depth; class Long volum; }

Engineered for Tomorrow Declaring the objects 1.Declare a variable for the class type 2.Using new operator dynamically allocate memory for the object created. StatementEffect Box b1;null b1 b1= new Box();width b1height depth Box object

Engineered for Tomorrow METHODS Methods are functions in Java. It is declared and defined inside the class. Methods declared inside the class operates on the instance variable of the class The general form of the method: Type name(parameter-list){ // body of method } Type is the type of the value the return statement returns. Parameter list is the formal parameter. return statement The method have return statement which will return a value of nothing

Engineered for Tomorrow CONSTRUCTORS Constructor is the method which has same name as class name. Constructor initializes an object immediately upon creation It is automatically called when an object is created. It has no return value type, not even void. The implicit return type of a class constructor is class type itself. Constructor can be overloaded and parameterized. Class Box { Int width, height, depth; Box(int w, int h, int d) { Width=w; Height= h; Depth= d;} Int volume() { Return width*height*depth; } Class boxdemo { Public static void main(String args[]) { Box b1=new box(10, 20, 30); System.out.println(b1.volume()); } Class Box { Int width, height, depth; Box(int w, int h, int d) { Width=w; Height= h; Depth= d;} Int volume() { Return width*height*depth; } Class boxdemo { Public static void main(String args[]) { Box b1=new box(10, 20, 30); System.out.println(b1.volume()); }

Engineered for Tomorrow The this keyword Instance variables are hidden by the parameters and local variables in a method. this keyword are used inside the methods. this keyword are used to refer the object on which the method is invoked. This keyword refers current instance of the object. Box( int width, int depth, int height) { this.width=width; this.height-height; this.depth=depth; }

Engineered for Tomorrow The this keyword…. This keyword are used to refer overloaded constructor class Loan{ private double interest; private String type; public Loan(){ this(“personal loan”); } public Loan(String type){ this.type = type; this.interest = 0.0; }

Engineered for Tomorrow The this keyword…. Cannot assign value to this variables. It will result in compilation error this = new Loan(); // lead to error this can be used to return object public Loan getLoan(){ return this; // return object } this is a valid return value

Engineered for Tomorrow Garbage collection Java run time automatically, randomly recover the memory from the objects to which no reference exist When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation. There is no explicit deletion and no memory is given back to the operating system. Every object tree must have one or more root objects. As long as the application can reach those roots, the whole tree is reachable Unreachable objects roots

Engineered for Tomorrow finalize Method The action that is to be performed by after destroying the object. Finalize method is called prior to garbage collection It is not automatically called by compiler like constructor Finalize method is called only once by garbage collection thread. Any exception thrown by finalize method is ignored by GC thread and it will not be propagated further, in fact I doubt if you find any trace of it. Proteced void finalize() { // finalize code }

Engineered for Tomorrow Inheritance Important OOPs concept Creation of hierarchal classification Class that is inherited is called super class Class that does inheritance is called sub class extend keyword is used for inheritance. Class superclass{ Int I,j; Void show() { //----- }} Class subclass extend superclass{ Int k; Void display() {\\--- }} Variable I and j can be used by subclass Method show can be used by subclass Private member in super class cannot be used by sub class

Engineered for Tomorrow Using Super Two general form 1.Calling super class constructor 2.Accessing super class member that has been hidden by a member of sub class Calling super class constructor Super(parameter_list); Class box{ Int width; Int depth; Box( int w, int d) { Width=w; Depth=d; } show() { System.out.println(width, depth);} Class boxe extend box {int height; Boxe(int w, int d, int h) { Super(w,d);//super class constructor Height=h; } Show() { Super.show();// super class show method System.out.println(height);}

Engineered for Tomorrow Exception handling An Error is any unexpected result obtained from a program during execution. Unhandled errors may led to abnormal program termination. Errors should be handled by the programmer, to prevent them from reaching the user. Some typical causes of errors: Memory errors File system errors Calculation errors (i.e. divide by 0) Array errors (i.e. accessing element –1) Conversion errors

Engineered for Tomorrow Exception handling… Code to be monitored for exception is kept in try block Action to be taken for exception is kept in catch block After catch the things to be executed are kept in finally block Throw and throws are used to manually throw an error. Catch block catches all exceptions of its type and subclasses of its type If there are multiple catch blocks that match a particular exception type, only the first matching catch block executes Makes sense to use a catch block of a superclass when all catch blocks for that class’s subclasses will perform same functionality

Engineered for Tomorrow Preceding step try block throw statement unmatched catch matching catch unmatched catch next step Control flow for Throw Example – try { … normal program code } catch(Exception e) { … exception handling code }

Engineered for Tomorrow throwable ExceptionError RunTimeExceptionIO ExceptionAWT Error Out of memory error Thread death ArrayIndexOutofBound Exception Inputmismatch exception classcastnullpointer Arithmetic Exception hierarchy

Engineered for Tomorrow An applet is a Java program that runs in a Web browser. An applet is a Java class that extends the java.applet.Applet class. No main() in java applet program. Applets are designed to be embedded within an HTML page. Code of the applet is downloaded when user view the applet. 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. Like applet viewer 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. Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file. JAVA APPLET

Engineered for Tomorrow Applets in the Class Hierarchy

Engineered for Tomorrow Designing an Applet An applet class can be designed as a derived class of JApplet in much the same way that regular Swing GUIs are defined as derived classes of JFrame However, an applet normally defines no constructors – The method init performs the initializations that would be performed in a constructor for a regular Swing GUI

Engineered for Tomorrow Designing an Applet Components can be added to an applet in the same way that a component is added to a JFrame – The method add is used to add components to an applet in the same way that components are added to a JFrame

Engineered for Tomorrow How Applets Differ from Swing GUIs Some of the items included in a Swing GUI are not included in an applet Applets do not contain a main or setVisible method – Applets are displayed automatically by a Web page or an applet viewer Applets do not have titles – Therefore, they do not use the setTitle method – They are normally embedded in an HTML document, and the HTML document can add any desired title

Engineered for Tomorrow Four methods in the Applet: init: This method is intended for whatever initialization is needed for the applet. 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. destroy: This method is only called when the browser shuts down normally. paint: Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. LIFE CYCLE OF APPLET

Engineered for Tomorrow LIFE CYCLE OF APPLET Init() Start() Paint() Stop() Destroy()

25 import java.awt.*; import java.applet.*; public class WelcomeApplet extends Applet { public void init() { } public void paint(Graphics g) { g.drawString("Welcome to Java Programming!", 25, 25 ); } import java.awt.*; import java.applet.*; public class WelcomeApplet extends Applet { public void init() { } public void paint(Graphics g) { g.drawString("Welcome to Java Programming!", 25, 25 ); } extends allows us to inherit the capabilities of class Applet. Method paint is guaranteed to be called in all applets. Its first line must be defined as above. Engineered for Tomorrow Simple applet program

Engineered for Tomorrow 1 // Fig. 3.6: WelcomeApplet.java 2 // A first applet in Java. 3 4 // Java packages 5 import java.awt.Graphics; // import class Graphics 6 import javax.swing.JApplet; // import class JApplet 7 8 public class WelcomeApplet extends JApplet { 9 10 // draw text on applet ’ s background 11 public void paint( Graphics g ) 12 { 13 // call superclass version of method paint 14 super.paint( g ); // draw a String at x-coordinate 25 and y-coordinate g.drawString( "Welcome to Java Programming!", 25, 25 ); } // end method paint } // end class WelcomeApplet 1 // Fig. 3.6: WelcomeApplet.java 2 // A first applet in Java. 3 4 // Java packages 5 import java.awt.Graphics; // import class Graphics 6 import javax.swing.JApplet; // import class JApplet 7 8 public class WelcomeApplet extends JApplet { 9 10 // draw text on applet ’ s background 11 public void paint( Graphics g ) 12 { 13 // call superclass version of method paint 14 super.paint( g ); // draw a String at x-coordinate 25 and y-coordinate g.drawString( "Welcome to Java Programming!", 25, 25 ); } // end method paint } // end class WelcomeApplet import allows us to use predefined classes (allowing us to use applets and graphics, in this case). extends allows us to inherit the capabilities of class JApplet. Method paint is guaranteed to be called in all applets. Its first line must be defined as above.

Engineered for Tomorrow Make an HTML page with the appropriate tag to load the applet code. Supply a subclass of the JApplet class. Make this class public. Otherwise, the applet cannot be loaded. Eliminate the main method in the application. Do not construct a frame window for the application. Application will be displayed inside the browser. Move any initialization code from the frame window constructor to the init method of the applet. Don't need to explicitly construct the applet object.the browser instantiates it and calls the init method. Remove the call to setSize; for applets, sizing is done with the width and height parameters in the HTML file. Remove the call to setDefaultCloseOperation. An applet cannot be closed; it terminates when the browser exits. If the application calls setTitle, eliminate the call to the method. Applets cannot have title bars. The applet is displayed automatically. Steps for converting an application to an applet.

Engineered for Tomorrow import java.awt.*; import java.applet.*; public class WelcomeApplet2 extends Applet { public void init() { } public void paint(Graphics g) { g.drawString( "Welcome to", 25, 25 ); g.drawString( "Java Programming!", 25, 40 );} } import java.awt.*; import java.applet.*; public class WelcomeApplet2 extends Applet { public void init() { } public void paint(Graphics g) { g.drawString( "Welcome to", 25, 25 ); g.drawString( "Java Programming!", 25, 40 );} } The two drawString statements simulate a newline. In fact, the concept of lines of text does not exist when drawing strings. Applet program to draw a line

Engineered for Tomorrow Invoking an applet An applet may be invoked by using an HTML file and viewing the file through an applet viewer or Java-enabled browser. The tag is the basis for embedding an applet in an HTML file. Below is an example that invokes the "Hello, World" applet: The Hello, World Applet If your browser was Java-enabled, a "Hello, World" message would appear here.

Engineered for Tomorrow getApplet(String) getApplet Gets an applet by name. getApplets() getApplets Enumerate the applets in this context. getAudioClip(URL) getAudioClip Gets an audio clip. getImage(URL) getImage Gets an image. showDocument(URL) showDocument Show a new document. showStatus(String) showStatus Show a status string. AppletContext interface methods