Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Chapter 1 Introducing Java
  2. 2. Objectives <ul><li>Recognize the strengths of the Java programming language. </li></ul><ul><li>Understand the uses of Java for applets and applications. </li></ul><ul><li>Use the import statement to incorporate Java packages into your code. </li></ul><ul><li>Declare and add components to an interface. </li></ul><ul><li>Modify text using the Color and Font objects. </li></ul><ul><li>Write an applet and run it in a browser or applet viewer. </li></ul>
  3. 3. Java <ul><li>You can write small programs called applets, which run in a Web browser. </li></ul><ul><li>You can write stand-alone programs called applications, which are independent of the browser. </li></ul><ul><li>Programs can be text based or have graphical user interface (GUI). </li></ul><ul><li>Developed by Sun Microsystems in 1991. </li></ul>
  4. 4. An Official Description <ul><li>Java is a simple, object-oriented, robust, secure, portable, high-performance, architecturally neutral, interpreted, multithreaded, dynamic language. </li></ul>
  5. 5. Object Oriented <ul><li>Java is a true object oriented programming language (OOP). </li></ul><ul><li>In OOP, programmers create units called classes that contain data and the instructions for handling data. </li></ul><ul><li>Well-designed classes are reusable components that can be combined to create new applications. </li></ul>
  6. 6. Object Oriented Continued <ul><li>Applets running in a browser and GUI applications do not follow a sequential logic. </li></ul><ul><li>Each user action can cause an event to occur, which can trigger an event handler. </li></ul><ul><li>The clicking causes the event listener to notify your program, and the program automatically jumps to a procedure you have written to do the event. </li></ul>
  7. 7. Traditional Languages are Compiled
  8. 8. Java Applets and Applications are Compiled
  9. 9. Java Development Tools <ul><li>To run a Java program, you need the Java Runtime Environment (JRE). </li></ul><ul><li>To develop or write Java applications and applets you need the Java Development Kit (JDK) from Sun Microsystems. </li></ul>
  10. 10. The Java API <ul><li>The Java application programming interface (API) consists of classes created by Sun Microsystems and stored in library files called packages . </li></ul><ul><li>We use classes from java.lang, java.awt, java.applet. </li></ul><ul><li>For the other classes see table on next slide. </li></ul>
  11. 11. Partial List of Core Java API Packages Core functions of the language, automatically included java.lang Handle input and output of data Create software components java.beans Graphical components using abstract windows toolkit java.awt Create applets java.applet Purpose Package
  12. 12. Partial List of Core Java API Packages Continued Purpose Package Provide utilities such as dates java.util Manipulate text including searches java.text GUI using Java Foundation Classes java.swing Query databases java.sql Manage certificates, signatures, and other security Remote objects java.rmi Networking Handle math functions, very large integers and decimal values java.math
  13. 13. Java has Several Levels of Security
  14. 14. Classes and Objects <ul><li>A class is a template or blueprint for an object, but is not the object itself. </li></ul><ul><li>Let us take an example of a dog: </li></ul><ul><li>We can describe the characteristics of a certain breed of dog, such as typical coloring and general size and whether the breed barks, bites, or runs fast and descriptions are the properties and methods of the class. </li></ul><ul><li>But an actual dog (an instance of the dog class) will have a set of characteristics, such as a name and a size. </li></ul><ul><li>This means you instantiate object of the dog class to create an instance of the dog. </li></ul>
  15. 15. Java Objects <ul><li>In Java, all objects are inherited from the Object class from the lang package (java.lang.Object). </li></ul><ul><li>The Object class is the superclass of all other classes, which are called subclasses . </li></ul><ul><li>See Fig 1.6 for the inheritance of the applet class, which inherits from the Panel class, which inherits from the Container class, which inherits from the Component, which inherits from the Object. </li></ul>
  16. 16. Inheritance of Java Objects
  17. 17. Inheritance <ul><li>An important concept of OOP is inheritance . </li></ul><ul><li>The new class is based on an existing class. </li></ul><ul><li>In Java, we say that the new class extends or inherits from the existing class. </li></ul>
  18. 18. Encapsulation <ul><li>Another important concept of OOP is encapsulation. </li></ul><ul><li>Encapsulation refers to the combination of characteristics of a class. </li></ul><ul><li>Encapsulation is often referred to as data hiding . </li></ul><ul><li>The class can &quot;expose&quot; certain properties and methods and keep others hidden. </li></ul>
  19. 19. Punctuation <ul><li>Java is very picky about punctuation. </li></ul><ul><li>Every statement must be terminated by a semicolon (;). </li></ul><ul><li>The braces ({}) enclose a block of statements. </li></ul><ul><li>The applet class contains a block of statements and the method inside the class contains another block. </li></ul>
  20. 20. Case Sensitivity <ul><li>Java is case-sensitive, so it is critical that you observe the capitalization. </li></ul><ul><li>Most errors on first programs are due to incorrect capitalization. </li></ul>
  21. 21. Class Diagram
  22. 22. Naming Rules <ul><li>A Java name: </li></ul><ul><li>must contain only letters, numbers, and underscores. </li></ul><ul><li>must begin with a letter or underscore. </li></ul><ul><li>cannot have any embedded spaces. </li></ul><ul><li>is case sensitive. </li></ul><ul><li>Cannot be one of Java's reserved words, such as boolean , public , or import . (Note that all of the Java reserved words are lowercase. If you use uppercase as part of your names, you will never have a naming conflict. </li></ul>
  23. 23. Naming Conventions <ul><li>To create good Java names, you should: </li></ul><ul><li>Make all names meaningful. Do not use names such as a, b, c, or x. Always create names that a person reading your code can tell the purpose of. And do not abbreviate unless using a standard abbreviation that has a clearly understood meaning. </li></ul><ul><li>Begin the name with a lowercase prefix that indicates the type of component. </li></ul>
  24. 24. Naming Conventions Continued <ul><li>Begin the actual name (following the prefix) with an uppercase letter. Use mixed upper- and lowercase for the name, with a capital to begin each new word. </li></ul><ul><li>Examples </li></ul><ul><li>lblMessage </li></ul><ul><li>fntLargeText </li></ul>
  25. 25. Finding and Fixing Errors <ul><li>Programming errors come in three varieties: compile errors, run-time errors and logic errors. </li></ul>
  26. 26. Using HTML to Run the Applet in a Browser <ul><li>To run your applet in a browser, you need to create an HTML file that calls the applet, and then open the HTML file with the browser. </li></ul><ul><li>You can set the width and height to the value of your choice, and then omit the resize method in your applet. </li></ul><ul><li><HTML> </li></ul><ul><li><BODY> </li></ul><ul><li><Applet code = FontApplet.class width = 600 height = 200> </li></ul><ul><li></Applet> </li></ul><ul><li></BODY> </li></ul><ul><li></HTML> </li></ul>
  27. 27. Using HTML to Run the Applet in a Browser Continued <ul><li>The codes in HTML tags < > are not case sensitive, but the name of the class file is case sensitive. </li></ul><ul><li>Your Java source code file has a .java extension and after the compiler completes, it creates the .class file. </li></ul><ul><li>Follow the applet information with the closing tag </Applet>. </li></ul>
  28. 28. Life Cycle of an Applet <ul><li>The execution of an applet in a browser follows a specific cycle. </li></ul><ul><li>Each applet always executes four methods: init, start, stop , and destroy. </li></ul><ul><li>The only method you are writing currently is the init method. </li></ul>
  29. 29. The Methods in the Life Cycle of an Applet Executes prior to shut down of the browser destroy Executes when the browser leaves a Web page containing the applet stop Follows the init and reexecutes each time that the page displays start Executes the first time an applet is loaded init Purpose Method
  30. 30. Class Diagram