Chapter 2.1
Upcoming SlideShare
Loading in...5
×
 

Chapter 2.1

on

  • 89 views

 

Statistics

Views

Total Views
89
Views on SlideShare
66
Embed Views
23

Actions

Likes
0
Downloads
0
Comments
0

2 Embeds 23

http://sotlsoc.net 15
http://www.sotlsoc.net 8

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Chapter 2.1 Chapter 2.1 Presentation Transcript

  • 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