SlideShare ist ein Scribd-Unternehmen logo
1 von 40
INTRODUCING JAVA
APPLICATIONS AND APPLETS
Chapter 2.1:
Introduction
 Java is the Internet programming language
 It also can be used to develop standalone
applications
 Java is cross-platform, object-oriented,network-
based, and multimedia-ready.
The History of Java
 1991 - developed by James Gosling and
colleagues at Sun Microsystems. The language,
initially called Oak. Designed for use in embedded
consumer electronic applications and intended to
replace C++
 1995- renamed Java designed for Internet
applications.
Characteristics of Java
–Simple
–Object Oriented
–Distributed
–Interpreted
–Robust
– Secure
– Portable
– Architecture
neutral
– High-
performance
– Multithreaded
– Dynamic
The Basics of Java Program
 There are 2 types of Java programs:
 Applications
○ Standalone programs
 Applets
○ Programs designed to run on a Web browser
The Basics of Java Program
 Applications
 single plain-looking terminal window
or
 with a graphical user interface (GUI) use one or more
windows filled with user interface components such as
buttons, text input fields, and menus
The Basics of Java Program
Single plain-looking ApplicationGUI Application
The Basics of Java Program
 Basic unit of a Java program – class
 Therefore application is a collection of one or more
classes.
 A class is a collection of methods and data members
 One of the classes in a Java application program
must have only one method main.
Packages, Classes,
Methods
 Package: A collection of related classes.
 Class: Consists of methods.
 Method: Designed to accomplish a specific task.
Import Statement
 Used to import the components of a package into
a program.
 Reserved word.
 import java.io.*;
Imports the (components of the) package java.io into the
program.
 Primitive data types and the class String:
 Part of the Java language.
 Don’t need to be imported.
Creating a Java Application
Program
 Syntax of a class:
 Syntax of the main method:
Programming Style
 Know common syntax errors and rules.
 Use blanks appropriately.
 Use a semicolon as a statement terminator.
 Important to have well-documented code.
 Good practice to follow traditional rules for naming
identifiers.
Skeleton of Java Program
import statements if any
public class ClassName
{
declare named constants and/or stream objects
public static void main(String[] args) throws
IOException
{
variable declaration
executable statements
}
}
Skeleton of Java Program
import java.io.*;
/*This class is created to manipulate arithmetic operations*/
public class ArithmeticProgram {
static final int NUMBER = 12;
static Scanner in = new Scannner (System.in);
public static void main(String[] args) throws IOException {
int firstNum, secondNum;
firstNum=18;
System.out.println(“Enter an integer:”);
secondNum = in.nextInt();
System.out.println(“Value of firstNum:”+firstNum);
System.out.println(“Value of secondNum:”+secondNum);
firstNum=firstNum+NUMBER+2*secondNum;
System.out.println(“New value of firstNum:”+firstNum);
}
}
Example #1
Skeleton of Java Program/*This class is created to perform arithmetic operation*/
public class Count{
private int num1,num2,addResult,multiplyResult; //list of attributes
public void setData(int n1, int n2){ //to assign a value to num1 and
num2
num1 = n1;
num2 = n2;
}
//to perform addition between num1 and num2
public void performAddOperation(){
addResult = num1 + num2;
}
//to perform multiplication between num1 and num2
public void performMultiplyOperation(){
multiplyResult = num1 * num2;
}
//to display the result of addition and multiplication
public void displayResult(){
System.out.println(num1 + " * " + num2 + " = " +
multiplyResult);
System.out.println(num1 + " +" + num2 + " = " + addResult);
}
Example #2
Skeleton of Java Program
/*This class is created to test/manipulate the Count class
public class TestCount{
public static void main(String[]args){
Count count=new Count();//create an instance of the Count class
count.setData(12, 6);
count.performAddOperation();
count.performMultiplyOperation();
count.displayResult();
}//end main
}//end class
Example #2
Output:
12 * 6 = 72
12 +6 = 18
How to use Java?
 Understand files and folders/directories
 Locate the Java compiler/ Install J2SE
 Set path & Java class path
 Write a simple program (later)
 Save your work
 Compile & run
 Use Dos Command Prompt or IDE
Creating a Java Program
 Use any text editor to create or edit a Java source
code.
 Save the file. Use the same class name. For
example class name: Count, file name should be
Count.java
 Compile the program to translate the source code
into bytecode. If no syntax errors, compiler will
generate file named Count.class
 Execute the program.
DOS Command Window
An Integrated Development
Environment
Creating a Java Program
 Basic elements of a Java program include:
 The main method
 Reserved words
 Special symbols
 Identifiers
 Data types
 Expressions
 Input
 Output
 Statements
Creating a Java Program
 To create a Java application, it is important to
understand:
 Syntax rules.
 Semantic rules.
 How to manipulate strings and numbers.
 How to declare variables and named constants.
 How to receive input and display output.
 Good programming style and form.
File Hello.java
1 public class Hello
2 {
3 public static void main(String[] args)
4 {
5 // display a greeting in the console window
6 System.out.println("Hello, World!");
7 }
8 }
Java Program Elements
 A Java program is made up of class definitions.
 A class definition must contains a header and a
body.
 A class contains zero or more methods
 A method is a named section of code that also has
a header & body
 A method contains program statements
 Single-line (starts with //) and multi-line (enclosed
by /* and */) comments are used to document the
code
Java Program Structure
public class Hello
{
}
// comments about the class
class header
class body
//Comments can be placed almost anywhere
Java Program Structure
public class MyProgram
{
}
// comments about the class
public static void main (String[] args)
{
}
// comments about the method
method header
method body
Compiling and Running
 Type program into text editor
 Save (file name must be similar to class name)
 Open Dos Window
 Change directory to saved file directory
 Compile into byte codes
javac Hello.java
 Execute byte codes
java Hello
Creating a Java Program
JVM
Create/modify source code
Source code
Compile source code
Byte code
Run byte code
Output
Syntax errors
Runtime errors or
incorrect results
Applets
 Applet: a Java program that is embedded within a
Web page and executed by a Web browser
 is a program written in the JavaTM programming
language that can be included in an HTML page.
When you use a Java technology-enabled browser
to view a page that contains an applet, the applet's
code is transferred to your system and executed
by the browser's Java Virtual Machine (JVM).
 Create an applet by extending the class JApplet.
 class JApplet is contained in package javax.swing.
Applets
 A Java application is a stand-alone program with a
main method (like the ones we've seen so far)
 A Java applet is a program that is intended to be
transported over the Web and executed using a
web browser
 An applet also can be executed using the
appletviewer tool of the Java SDK
 An applet doesn't have a main method
 Instead, there are several special methods that
serve specific purposes
Applet Methods
 init method:
 Initializes variables.
 Gets data from user.
 Places various GUI components.
 paint method:
 Performs output.
Applets
 The paint method is executed automatically
whenever the applet’s contents are drawn
 The paint method accepts a parameter that is an
object of the Graphics class
 A Graphics object defines a graphics context on
which we can draw shapes and text
 The Graphics class has several methods for
drawing shapes
//********************************************************************
// Einstein.java Author: Lewis/Loftus
//
// Demonstrates a basic applet.
//********************************************************************
import javax.swing.JApplet;
import java.awt.*;
public class Einstein extends JApplet
{
//-----------------------------------------------------------------
// Draws a quotation by Albert Einstein among some shapes.
//-----------------------------------------------------------------
public void paint (Graphics page)
{
page.drawRect (50, 50, 40, 40); // square
page.drawRect (60, 80, 225, 30); // rectangle
page.drawOval (75, 65, 20, 20); // circle
page.drawLine (35, 60, 100, 120); // line
page.drawString ("Out of clutter, find simplicity.", 110, 70);
page.drawString ("-- Albert Einstein", 130, 100);
}
}
//********************************************************************
// Einstein.java Author: Lewis/Loftus
//
// Demonstrates a basic applet.
//********************************************************************
import javax.swing.JApplet;
import java.awt.*;
public class Einstein extends JApplet
{
//-----------------------------------------------------------------
// Draws a quotation by Albert Einstein among some shapes.
//-----------------------------------------------------------------
public void paint (Graphics page)
{
page.drawRect (50, 50, 40, 40); // square
page.drawRect (60, 80, 225, 30); // rectangle
page.drawOval (75, 65, 20, 20); // circle
page.drawLine (35, 60, 100, 120); // line
page.drawString ("Out of clutter, find simplicity.", 110, 70);
page.drawString ("-- Albert Einstein", 130, 100);
}
}
The HTML applet tag
 An applet is embedded into an HTML file using a
tag that references the bytecode file of the applet
 The bytecode version of the program is
transported across the web and executed by a
Java interpreter that is part of the browser
<html>
<head>
<title>The Einstein Applet</title>
</head>
<body>
<applet code="Einstein.class" width=350 height=175>
</applet>
</body>
</html>
Skelaton of a Java Applet
import java.awt.Graphics;
import javax.swing.Japplet;
public class WelcomeApplet extends JApplet
{
}
<HTML>
<HEAD>
<TITLE>WELCOME</TITLE>
</HEAD>
<BODY>
<APPLET code = "GrandWelcome.class" width="440" height="50">
</APPLET>
</BODY>
</HTML>
import java.awt.*;
import javax.swing.JApplet;
public class GrandWelcome extends JApplet{
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.setFont(new Font("Courier",Font.BOLD, 24));
g.drawString("Welcome to Hava
Programming",30,30);
}
}
Java Platform
 Platform is a hardware or software environment to
execute a program.
 It also can be defined as a combination of
operating system and hardware.
 The popular platform are Windows 2000, Linux,
Solaris, and MacOS.
 Java platform is different with other platform
because it is only a software to execute an
application on different hardware platform.
Java Platform
 It has 2 components :
 Java Virtual Machine (JVM)
 Java Application Programming Interface (Java API)
 (source : Website http://java.sun.com).
Java Virtual Machine (JVM)
 Also called a "Java Interpreter"- Converts bytecode into
OS specific commands. In addition to governing the
execution of an application's bytecodes, JVM handles
related tasks such as managing the system's memory,
providing security against malicious code, and
managing multiple threads of program execution. JVM
sits on top of a native environment (OS) and allows for
portability of applications across various hardware as
long as the hardware has the JVM on it.
 JVM executes instructions that a Java compiler
generates. This run time environment, is embedded in
various products, such as web browsers, servers, and
operating systems.
java.sun.com/developer/onlineTraining/new2java/programming/
learn/unravelingjava.html

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Java notes
Java notesJava notes
Java notes
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java essential notes
Java essential notesJava essential notes
Java essential notes
 
Core Java
Core JavaCore Java
Core Java
 
Java platform
Java platformJava platform
Java platform
 
Chapter 1 introduction to java technology
Chapter 1 introduction to java technologyChapter 1 introduction to java technology
Chapter 1 introduction to java technology
 
Java Notes
Java Notes Java Notes
Java Notes
 
Lecture 3 java basics
Lecture 3 java basicsLecture 3 java basics
Lecture 3 java basics
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
Java notes
Java notesJava notes
Java notes
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
 
Introduction to java
Introduction to java Introduction to java
Introduction to java
 
SMI - Introduction to Java
SMI - Introduction to JavaSMI - Introduction to Java
SMI - Introduction to Java
 
1.introduction to java
1.introduction to java1.introduction to java
1.introduction to java
 
Programming in Java
Programming in JavaProgramming in Java
Programming in Java
 
Fundamentals of JAVA
Fundamentals of JAVAFundamentals of JAVA
Fundamentals of JAVA
 
JAVA Program Examples
JAVA Program ExamplesJAVA Program Examples
JAVA Program Examples
 
Introduction to java technology
Introduction to java technologyIntroduction to java technology
Introduction to java technology
 
Java seminar
Java seminarJava seminar
Java seminar
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 

Andere mochten auch (6)

Dense Matrix Multiplication in Java
Dense Matrix Multiplication in JavaDense Matrix Multiplication in Java
Dense Matrix Multiplication in Java
 
Applet
AppletApplet
Applet
 
9 multiplication table
9 multiplication table9 multiplication table
9 multiplication table
 
Java applets
Java appletsJava applets
Java applets
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Simple Java Programs
Simple Java ProgramsSimple Java Programs
Simple Java Programs
 

Ähnlich wie Chapter 2.1

Introduction
IntroductionIntroduction
Introductionrichsoden
 
Java lab1 manual
Java lab1 manualJava lab1 manual
Java lab1 manualnahalomar
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to javaAli Baba
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software DevelopmentZeeshan MIrza
 
java basic for begginers
java basic for begginersjava basic for begginers
java basic for begginersdivaskrgupta007
 
Java basics notes
Java basics notesJava basics notes
Java basics notesNexus
 
INTRODUCTION TO JAVA APPLICATION
INTRODUCTION TO JAVA APPLICATIONINTRODUCTION TO JAVA APPLICATION
INTRODUCTION TO JAVA APPLICATIONAjit Yadav
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECSreedhar Chowdam
 
Java for Mainframers
Java for MainframersJava for Mainframers
Java for MainframersRich Helton
 
Introduction to computer science
Introduction to computer scienceIntroduction to computer science
Introduction to computer scienceumardanjumamaiwada
 
java 1 new.pdf
java 1 new.pdfjava 1 new.pdf
java 1 new.pdfSulSya
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Pratima Parida
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Pratima Parida
 

Ähnlich wie Chapter 2.1 (20)

Javalecture 1
Javalecture 1Javalecture 1
Javalecture 1
 
Introduction
IntroductionIntroduction
Introduction
 
Java lab1 manual
Java lab1 manualJava lab1 manual
Java lab1 manual
 
01slide
01slide01slide
01slide
 
01slide
01slide01slide
01slide
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Java introduction
Java introductionJava introduction
Java introduction
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
 
java basic for begginers
java basic for begginersjava basic for begginers
java basic for begginers
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 
Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 
INTRODUCTION TO JAVA APPLICATION
INTRODUCTION TO JAVA APPLICATIONINTRODUCTION TO JAVA APPLICATION
INTRODUCTION TO JAVA APPLICATION
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Java for Mainframers
Java for MainframersJava for Mainframers
Java for Mainframers
 
lecture 6
 lecture 6 lecture 6
lecture 6
 
Introduction to computer science
Introduction to computer scienceIntroduction to computer science
Introduction to computer science
 
java 1 new.pdf
java 1 new.pdfjava 1 new.pdf
java 1 new.pdf
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
 

Mehr von sotlsoc

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 newsotlsoc
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 newsotlsoc
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 newsotlsoc
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 newsotlsoc
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1sotlsoc
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3sotlsoc
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4sotlsoc
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3sotlsoc
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5sotlsoc
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2sotlsoc
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0sotlsoc
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2sotlsoc
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1sotlsoc
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0sotlsoc
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0sotlsoc
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3sotlsoc
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4sotlsoc
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1sotlsoc
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1sotlsoc
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0sotlsoc
 

Mehr von sotlsoc (20)

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 new
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 new
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 new
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0
 

Kürzlich hochgeladen

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 

Kürzlich hochgeladen (20)

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 

Chapter 2.1

  • 1. INTRODUCING JAVA APPLICATIONS AND APPLETS Chapter 2.1:
  • 2. Introduction  Java is the Internet programming language  It also can be used to develop standalone applications  Java is cross-platform, object-oriented,network- based, and multimedia-ready.
  • 3. The History of Java  1991 - developed by James Gosling and colleagues at Sun Microsystems. The language, initially called Oak. Designed for use in embedded consumer electronic applications and intended to replace C++  1995- renamed Java designed for Internet applications.
  • 4. Characteristics of Java –Simple –Object Oriented –Distributed –Interpreted –Robust – Secure – Portable – Architecture neutral – High- performance – Multithreaded – Dynamic
  • 5. The Basics of Java Program  There are 2 types of Java programs:  Applications ○ Standalone programs  Applets ○ Programs designed to run on a Web browser
  • 6. The Basics of Java Program  Applications  single plain-looking terminal window or  with a graphical user interface (GUI) use one or more windows filled with user interface components such as buttons, text input fields, and menus
  • 7. The Basics of Java Program Single plain-looking ApplicationGUI Application
  • 8. The Basics of Java Program  Basic unit of a Java program – class  Therefore application is a collection of one or more classes.  A class is a collection of methods and data members  One of the classes in a Java application program must have only one method main.
  • 9. Packages, Classes, Methods  Package: A collection of related classes.  Class: Consists of methods.  Method: Designed to accomplish a specific task.
  • 10. Import Statement  Used to import the components of a package into a program.  Reserved word.  import java.io.*; Imports the (components of the) package java.io into the program.  Primitive data types and the class String:  Part of the Java language.  Don’t need to be imported.
  • 11. Creating a Java Application Program  Syntax of a class:  Syntax of the main method:
  • 12. Programming Style  Know common syntax errors and rules.  Use blanks appropriately.  Use a semicolon as a statement terminator.  Important to have well-documented code.  Good practice to follow traditional rules for naming identifiers.
  • 13. Skeleton of Java Program import statements if any public class ClassName { declare named constants and/or stream objects public static void main(String[] args) throws IOException { variable declaration executable statements } }
  • 14. Skeleton of Java Program import java.io.*; /*This class is created to manipulate arithmetic operations*/ public class ArithmeticProgram { static final int NUMBER = 12; static Scanner in = new Scannner (System.in); public static void main(String[] args) throws IOException { int firstNum, secondNum; firstNum=18; System.out.println(“Enter an integer:”); secondNum = in.nextInt(); System.out.println(“Value of firstNum:”+firstNum); System.out.println(“Value of secondNum:”+secondNum); firstNum=firstNum+NUMBER+2*secondNum; System.out.println(“New value of firstNum:”+firstNum); } } Example #1
  • 15. Skeleton of Java Program/*This class is created to perform arithmetic operation*/ public class Count{ private int num1,num2,addResult,multiplyResult; //list of attributes public void setData(int n1, int n2){ //to assign a value to num1 and num2 num1 = n1; num2 = n2; } //to perform addition between num1 and num2 public void performAddOperation(){ addResult = num1 + num2; } //to perform multiplication between num1 and num2 public void performMultiplyOperation(){ multiplyResult = num1 * num2; } //to display the result of addition and multiplication public void displayResult(){ System.out.println(num1 + " * " + num2 + " = " + multiplyResult); System.out.println(num1 + " +" + num2 + " = " + addResult); } Example #2
  • 16. Skeleton of Java Program /*This class is created to test/manipulate the Count class public class TestCount{ public static void main(String[]args){ Count count=new Count();//create an instance of the Count class count.setData(12, 6); count.performAddOperation(); count.performMultiplyOperation(); count.displayResult(); }//end main }//end class Example #2 Output: 12 * 6 = 72 12 +6 = 18
  • 17. How to use Java?  Understand files and folders/directories  Locate the Java compiler/ Install J2SE  Set path & Java class path  Write a simple program (later)  Save your work  Compile & run  Use Dos Command Prompt or IDE
  • 18. Creating a Java Program  Use any text editor to create or edit a Java source code.  Save the file. Use the same class name. For example class name: Count, file name should be Count.java  Compile the program to translate the source code into bytecode. If no syntax errors, compiler will generate file named Count.class  Execute the program.
  • 21. Creating a Java Program  Basic elements of a Java program include:  The main method  Reserved words  Special symbols  Identifiers  Data types  Expressions  Input  Output  Statements
  • 22. Creating a Java Program  To create a Java application, it is important to understand:  Syntax rules.  Semantic rules.  How to manipulate strings and numbers.  How to declare variables and named constants.  How to receive input and display output.  Good programming style and form.
  • 23. File Hello.java 1 public class Hello 2 { 3 public static void main(String[] args) 4 { 5 // display a greeting in the console window 6 System.out.println("Hello, World!"); 7 } 8 }
  • 24. Java Program Elements  A Java program is made up of class definitions.  A class definition must contains a header and a body.  A class contains zero or more methods  A method is a named section of code that also has a header & body  A method contains program statements  Single-line (starts with //) and multi-line (enclosed by /* and */) comments are used to document the code
  • 25. Java Program Structure public class Hello { } // comments about the class class header class body //Comments can be placed almost anywhere
  • 26. Java Program Structure public class MyProgram { } // comments about the class public static void main (String[] args) { } // comments about the method method header method body
  • 27. Compiling and Running  Type program into text editor  Save (file name must be similar to class name)  Open Dos Window  Change directory to saved file directory  Compile into byte codes javac Hello.java  Execute byte codes java Hello
  • 28. Creating a Java Program JVM Create/modify source code Source code Compile source code Byte code Run byte code Output Syntax errors Runtime errors or incorrect results
  • 29. Applets  Applet: a Java program that is embedded within a Web page and executed by a Web browser  is a program written in the JavaTM programming language that can be included in an HTML page. When you use a Java technology-enabled browser to view a page that contains an applet, the applet's code is transferred to your system and executed by the browser's Java Virtual Machine (JVM).  Create an applet by extending the class JApplet.  class JApplet is contained in package javax.swing.
  • 30. Applets  A Java application is a stand-alone program with a main method (like the ones we've seen so far)  A Java applet is a program that is intended to be transported over the Web and executed using a web browser  An applet also can be executed using the appletviewer tool of the Java SDK  An applet doesn't have a main method  Instead, there are several special methods that serve specific purposes
  • 31. Applet Methods  init method:  Initializes variables.  Gets data from user.  Places various GUI components.  paint method:  Performs output.
  • 32. Applets  The paint method is executed automatically whenever the applet’s contents are drawn  The paint method accepts a parameter that is an object of the Graphics class  A Graphics object defines a graphics context on which we can draw shapes and text  The Graphics class has several methods for drawing shapes
  • 33. //******************************************************************** // Einstein.java Author: Lewis/Loftus // // Demonstrates a basic applet. //******************************************************************** import javax.swing.JApplet; import java.awt.*; public class Einstein extends JApplet { //----------------------------------------------------------------- // Draws a quotation by Albert Einstein among some shapes. //----------------------------------------------------------------- public void paint (Graphics page) { page.drawRect (50, 50, 40, 40); // square page.drawRect (60, 80, 225, 30); // rectangle page.drawOval (75, 65, 20, 20); // circle page.drawLine (35, 60, 100, 120); // line page.drawString ("Out of clutter, find simplicity.", 110, 70); page.drawString ("-- Albert Einstein", 130, 100); } }
  • 34. //******************************************************************** // Einstein.java Author: Lewis/Loftus // // Demonstrates a basic applet. //******************************************************************** import javax.swing.JApplet; import java.awt.*; public class Einstein extends JApplet { //----------------------------------------------------------------- // Draws a quotation by Albert Einstein among some shapes. //----------------------------------------------------------------- public void paint (Graphics page) { page.drawRect (50, 50, 40, 40); // square page.drawRect (60, 80, 225, 30); // rectangle page.drawOval (75, 65, 20, 20); // circle page.drawLine (35, 60, 100, 120); // line page.drawString ("Out of clutter, find simplicity.", 110, 70); page.drawString ("-- Albert Einstein", 130, 100); } }
  • 35. The HTML applet tag  An applet is embedded into an HTML file using a tag that references the bytecode file of the applet  The bytecode version of the program is transported across the web and executed by a Java interpreter that is part of the browser <html> <head> <title>The Einstein Applet</title> </head> <body> <applet code="Einstein.class" width=350 height=175> </applet> </body> </html>
  • 36. Skelaton of a Java Applet import java.awt.Graphics; import javax.swing.Japplet; public class WelcomeApplet extends JApplet { }
  • 37. <HTML> <HEAD> <TITLE>WELCOME</TITLE> </HEAD> <BODY> <APPLET code = "GrandWelcome.class" width="440" height="50"> </APPLET> </BODY> </HTML> import java.awt.*; import javax.swing.JApplet; public class GrandWelcome extends JApplet{ public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); g.setFont(new Font("Courier",Font.BOLD, 24)); g.drawString("Welcome to Hava Programming",30,30); } }
  • 38. Java Platform  Platform is a hardware or software environment to execute a program.  It also can be defined as a combination of operating system and hardware.  The popular platform are Windows 2000, Linux, Solaris, and MacOS.  Java platform is different with other platform because it is only a software to execute an application on different hardware platform.
  • 39. Java Platform  It has 2 components :  Java Virtual Machine (JVM)  Java Application Programming Interface (Java API)  (source : Website http://java.sun.com).
  • 40. Java Virtual Machine (JVM)  Also called a "Java Interpreter"- Converts bytecode into OS specific commands. In addition to governing the execution of an application's bytecodes, JVM handles related tasks such as managing the system's memory, providing security against malicious code, and managing multiple threads of program execution. JVM sits on top of a native environment (OS) and allows for portability of applications across various hardware as long as the hardware has the JVM on it.  JVM executes instructions that a Java compiler generates. This run time environment, is embedded in various products, such as web browsers, servers, and operating systems. java.sun.com/developer/onlineTraining/new2java/programming/ learn/unravelingjava.html