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

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

In Review JAVA C++ GUIs - Windows Webopedia.com.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
The Web Warrior Guide to Web Design Technologies
 2005 Pearson Education, Inc. All rights reserved Introduction.
Chapter 1: Introduction
Object Orientated Programming
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Introduction to Java Programming, 4E
Aalborg Media Lab 21-Jun-15 Software Design Lecture 1 “ Introduction to Java and OOP”
Applets, Graphical User Interfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads.
Java Programming, 3e Concepts and Techniques Chapter 1 An Introduction to Java and Program Design.
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.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
L EC. 01: J AVA FUNDAMENTALS Fall Java Programming.
Chapter 1 Introduction to Programming and C# Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Java Programming, 2E Introductory Concepts and Techniques Chapter 1 An Introduction to Java and Program Design.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Java Programming, 3e Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
A First Program Using C#
S.W. Ma/CIM/LWL41211/2 Prog. IIA Page 1 HKIVE (Lee Wai Lee Campus) Department of CIM Course : Year 2 Module : Programming IIA Textbook : Introduction.
CSCI 224 Introduction to Java Programming. Course Objectives  Learn the Java programming language: Syntax, Idioms Patterns, Styles  Become comfortable.
Java Workshop for Teachers May 6, 2005 A Brief Look at the Java Programming Language.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Microsoft Visual Basic 2005: Reloaded Second Edition
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.
Chapter 1: Creating Java Programs
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Introduction to Java Programming. History F James Gosling and Sun Microsystems F Oak F Java, May 20, 1995, Sun World F HotJava –The first Java-enabled.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
1 JavaScript in Context. Server-Side Programming.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Introduction to Java Programming with Forte Y. Daniel Liang.
Java Programming, 2E Introductory Concepts and Techniques Chapter 1 An Introduction to Java and Program Design.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Introduction to Java Programming. Introduction Course Objectives Organization of the Book.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Applets Yong Choi School of Business CSU, Bakersfield.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
1 JavaScript in Context. Server-Side Programming.
What is Java? Object Oriented Programming Language Sun Microsystems “Write Once, Run Everywhere” Bytecode and Virtual Machine Java Platform (Java VM and.
Creating a Java Application and Applet
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Overview of Programming.
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.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Introduction to Java Programming, 4E Y. Daniel Liang.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Chapter 1 Introduction to Computers, Programs, and Java
Java programming lecture one
Java Applets.
Applet in Java.
Introducing Java.
11.1 Applets & graphics.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Outcome of the Lecture Upon completion of this lecture you will be able to understand Fundamentals and Characteristics of Java Language Basic Terminology.
Presentation transcript:

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

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 2 McGraw-Hill/Irwin 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.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 3 McGraw-Hill/Irwin 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.

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

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 5 McGraw-Hill/Irwin 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.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 6 McGraw-Hill/Irwin Object Oriented Continued Applets running in a browser and GUI applications do not follow a sequential logic. Each user action can 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.

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

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

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 9 McGraw-Hill/Irwin 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. Java Development Tools

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 10 McGraw-Hill/Irwin 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.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 11 McGraw-Hill/Irwin Partial List of Core Java API Packages PackagePurpose java.appletCreate applets java.awtGraphical components using abstract windows toolkit java.beansCreate software components java.ioHandle input and output of data java.langCore functions of the language, automatically included

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 12 McGraw-Hill/Irwin Partial List of Core Java API Packages Continued PackagePurpose java.mathHandle math functions, very large integers and decimal values java.netNetworking java.rmiRemote objects java.securityManage certificates, signatures, and other security java.sqlQuery databases java.swingGUI using Java Foundation Classes java.textManipulate text including searches java.utilProvide utilities such as dates

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

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 14 McGraw-Hill/Irwin 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.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 15 McGraw-Hill/Irwin 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.

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

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 17 McGraw-Hill/Irwin 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.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 18 McGraw-Hill/Irwin 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.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 19 McGraw-Hill/Irwin 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.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 20 McGraw-Hill/Irwin 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.

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

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 22 McGraw-Hill/Irwin 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.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 23 McGraw-Hill/Irwin 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.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 24 McGraw-Hill/Irwin 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 lblMessage fntLargeText Naming Conventions Continued

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

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 26 McGraw-Hill/Irwin 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.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 27 McGraw-Hill/Irwin 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.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 28 McGraw-Hill/Irwin 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.

Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 29 McGraw-Hill/Irwin The Methods in the Life Cycle of an Applet MethodPurpose 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

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