Programming with Java 1 Chapter 1 Introducing Java

  • Slides: 30
Download presentation
Programming with Java 1 Chapter 1 Introducing Java Mc. Graw-Hill/Irwin © 2002 The Mc.

Programming with Java 1 Chapter 1 Introducing Java Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 2 Objectives • Recognize the strengths of the Java programming language.

Programming with Java 2 Objectives • Recognize the strengths of the Java programming language. • Understand the uses of Java for applets and applications. • Use the import statement to incorporate Java packages into your code. • Declare and add components to an interface. • Modify text using the Color and Font objects. • Write an applet and run it in a browser or applet viewer. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 3 Java • • You can write small programs called applets,

Programming with Java 3 Java • • You can write small programs called applets, which run in a Web browser. You can write stand-alone programs called applications, which are independent of the browser. Programs can be text based or have graphical user interface (GUI). Developed by Sun Microsystems in 1991. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 4 An Official Description • Java is a simple, object-oriented, robust,

Programming with Java 4 An Official Description • Java is a simple, object-oriented, robust, secure, portable, high-performance, architecturally neutral, interpreted, multithreaded, dynamic language. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 5 Object Oriented • Java is a true object oriented programming

Programming with Java 5 Object Oriented • Java is a true object oriented programming language (OOP). • In OOP, programmers create units called classes that contain data and the instructions for handling data. • Well-designed classes are reusable components that can be combined to create new applications. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 6 Object Oriented Continued • • • Applets running in a

Programming with Java 6 Object Oriented Continued • • • Applets running in a browser and GUI applications do not follow a sequential logic. Each user action cause an event to occur, which can trigger an event handler. 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. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 7 Traditional Languages are Compiled Mc. Graw-Hill/Irwin © 2002 The Mc.

Programming with Java 7 Traditional Languages are Compiled Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 8 Java Applets and Applications are Compiled Mc. Graw-Hill/Irwin © 2002

Programming with Java 8 Java Applets and Applications are Compiled Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 9 Java Development Tools • To run a Java program, you

Programming with Java 9 Java Development Tools • To run a Java program, you need the Java Runtime Environment (JRE). • To develop or write Java applications and applets you need the Java Development Kit (JDK) from Sun Microsystems. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 10 The Java API • The Java application programming interface (API)

Programming with Java 10 The Java API • The Java application programming interface (API) consists of classes created by Sun Microsystems and stored in library files called packages. • We use classes from java. lang, java. awt, java. applet. • For the other classes see table on next slide. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 11 Partial List of Core Java API Packages Package Purpose java.

Programming with Java 11 Partial List of Core Java API Packages Package Purpose java. applet Create applets java. awt Graphical components using abstract windows toolkit java. beans Create software components java. io Handle input and output of data java. lang Core functions of the language, automatically included Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 12 Partial List of Core Java API Packages Continued Package Purpose

Programming with Java 12 Partial List of Core Java API Packages Continued Package Purpose java. math Handle math functions, very large integers and decimal values java. net Networking java. rmi Remote objects java. security Manage certificates, signatures, and other security java. sql Query databases java. swing GUI using Java Foundation Classes java. text Manipulate text including searches java. util Provide utilities such as dates Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 13 Java has Several Levels of Security Mc. Graw-Hill/Irwin © 2002

Programming with Java 13 Java has Several Levels of Security Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 14 Classes and Objects • A class is a template or

Programming with Java 14 Classes and Objects • A class is a template or blueprint for an object, but is not the object itself. • Let us take an example of a dog: • 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. • But an actual dog (an instance of the dog class) will have a set of characteristics, such as a name and a size. • This means you instantiate object of the dog class to create an instance of the dog. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 15 Java Objects • In Java, all objects are inherited from

Programming with Java 15 Java Objects • In Java, all objects are inherited from the Object class from the lang package (java. lang. Object). • The Object class is the superclass of all other classes, which are called subclasses. • 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. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 16 Inheritance of Java Objects Mc. Graw-Hill/Irwin © 2002 The Mc.

Programming with Java 16 Inheritance of Java Objects Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 17 Inheritance • An important concept of OOP is inheritance. •

Programming with Java 17 Inheritance • An important concept of OOP is inheritance. • The new class is based on an existing class. • In Java, we say that the new class extends or inherits from the existing class. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 18 Encapsulation • Another important concept of OOP is encapsulation. •

Programming with Java 18 Encapsulation • Another important concept of OOP is encapsulation. • Encapsulation refers to the combination of characteristics of a class. • Encapsulation is often referred to as data hiding. • The class can "expose" certain properties and methods and keep others hidden. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 19 Punctuation • Java is very picky about punctuation. • Every

Programming with Java 19 Punctuation • Java is very picky about punctuation. • Every statement must be terminated by a semicolon (; ). • The braces ({}) enclose a block of statements. • The applet class contains a block of statements and the method inside the class contains another block. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 20 Case Sensitivity • Java is case-sensitive, so it is critical

Programming with Java 20 Case Sensitivity • Java is case-sensitive, so it is critical that you observe the capitalization. • Most errors on first programs are due to incorrect capitalization. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 21 Class Diagram Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies,

Programming with Java 21 Class Diagram Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 22 Naming Rules A Java name: • must contain only letters,

Programming with Java 22 Naming Rules A Java name: • must contain only letters, numbers, and underscores. • must begin with a letter or underscore. • cannot have any embedded spaces. • is case sensitive. • 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. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 23 Naming Conventions To create good Java names, you should: •

Programming with Java 23 Naming Conventions To create good Java names, you should: • 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. • Begin the name with a lowercase prefix that indicates the type of component. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 24 Naming Conventions Continued • Begin the actual name (following the

Programming with Java 24 Naming Conventions Continued • 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. Examples lbl. Message fnt. Large. Text Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 25 Finding and Fixing Errors • Programming errors come in three

Programming with Java 25 Finding and Fixing Errors • Programming errors come in three varieties: compile errors, run-time errors and logic errors. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 26 Using HTML to Run the Applet in a Browser •

Programming with Java 26 Using HTML to Run the Applet in a Browser • 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. • You can set the width and height to the value of your choice, and then omit the resize method in your applet. <HTML> <BODY> <Applet code = Font. Applet. class width = 600 height = 200> </Applet> </BODY> </HTML> Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 27 Using HTML to Run the Applet in a Browser Continued

Programming with Java 27 Using HTML to Run the Applet in a Browser Continued • The codes in HTML tags < > are not case sensitive, but the name of the class file is case sensitive. • Your Java source code file has a. java extension and after the compiler completes, it creates the. class file. • Follow the applet information with the closing tag </Applet>. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 28 Life Cycle of an Applet • The execution of an

Programming with Java 28 Life Cycle of an Applet • The execution of an applet in a browser follows a specific cycle. • Each applet always executes four methods: init, start, stop, and destroy. • The only method you are writing currently is the init method. Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 29 The Methods in the Life Cycle of an Applet Method

Programming with Java 29 The Methods in the Life Cycle of an Applet Method Purpose init Executes the first time an applet is loaded start Follows the init and reexecutes each time that the page displays stop Executes when the browser leaves a Web page containing the applet destroy Executes prior to shut down of the browser Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.

Programming with Java 30 Class Diagram Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies,

Programming with Java 30 Class Diagram Mc. Graw-Hill/Irwin © 2002 The Mc. Graw-Hill Companies, Inc. All rights reserved.