47
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

Embed Size (px)

Citation preview

Page 1: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

Java Programming, 2EIntroductory Concepts

and Techniques

Chapter 2Creating a Java

Application and Applet

Page 2: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

2Chapter 2: Creating a Java Application and Applet

Objectives

• Write a simple Java application

• Use TextPad

• Understand the different types and uses of comments

• Use proper naming conventions for classes and files

Page 3: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

3Chapter 2: Creating a Java Application and Applet

Objectives

• Identify the parts of a class header and method header

• Code output

• Use the println() method

• Compile a Java program

Page 4: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

4Chapter 2: Creating a Java Application and Applet

Objectives

• Understand the common types of errors

• Run a Java Program

• Edit Java source code to insert escape characters and a system date

• Print source code

Page 5: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

5Chapter 2: Creating a Java Application and Applet

Objectives

• Differentiate between an application and an applet

• Create an applet from Java source code

• Write code to display a graphic, text, color, and the date in an applet

• Create an HTML host document

• Run a Java applet

Page 6: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

6Chapter 2: Creating a Java Application and Applet

Introduction

• Users enter data and instructions into a computer and receive feedback from the computer through a user interface

• Programmers can create many types of user interfaces in Java

• We will create a program with two types of user interfaces– Console application

• Command line interface

– Applet• Graphical user interface displayed in a browser

Page 7: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

7Chapter 2: Creating a Java Application and Applet

The Welcome to My Day Program

• This program will display a splash screen– A splash screen is a screen that is displayed

before the main program starts

• The screen will contain a welcome message, user’s name, and system date – The console application will display text only– The applet will contain text, color, and a

graphic

Page 8: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

8Chapter 2: Creating a Java Application and Applet

Page 9: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

9Chapter 2: Creating a Java Application and Applet

Program Develpment

Page 10: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

10Chapter 2: Creating a Java Application and Applet

Analysis and Design

• Verify that the requirements are specific enough

• Design the user interface using a storyboard

• Design the program logic using a flowchart and event diagram

Page 11: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

11Chapter 2: Creating a Java Application and Applet

Page 12: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

12Chapter 2: Creating a Java Application and Applet

Page 13: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

13Chapter 2: Creating a Java Application and Applet

Page 14: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

14Chapter 2: Creating a Java Application and Applet

Using TextPad

• TextPad has several window areas– Coding window – Selector window– Clip Library window

• TextPad can display line numbers– Helpful for finding compiler errors

• TextPad has color-coding capabilities– Save a document before entering code to

enable Java related color-coding

Page 15: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

15Chapter 2: Creating a Java Application and Applet

Coding the Program -Comments as Documentation • Purpose of comments

– Provides clear description when reviewing code– Helps programmer think clearly when coding

• Placement of comments– Use a comment header to identify a file and its

purpose– Place a comment at the beginning of code for each

event and method– Place comments near portions of code that need

clarification

Page 16: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

16Chapter 2: Creating a Java Application and Applet

Coding the Program -Comments as Documentation

Page 17: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

17Chapter 2: Creating a Java Application and Applet

Coding the Program - The Class Header • Identify how the code can be accessed with an

access modifier– public indicates that the code can be accessed by all

objects in the program and can be extended for a subclass

• Specify a unique name for the class– The class name at the beginning of the program must

match the file name exactly– Java is case-sensitive – By convention, uppercase letters are used for class

names and to distinguish words in class names

Page 18: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

18Chapter 2: Creating a Java Application and Applet

Coding the Program - The Class Header

• Use braces {} after the class header to enclose the class body

Page 19: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

19Chapter 2: Creating a Java Application and Applet

Coding the Program -The Method Header • The method header contains modifiers, return value,

method name, and parameters along with their data type• Every stand-alone Java application must contain a

main() method, which is the starting point during execution

Page 20: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

20Chapter 2: Creating a Java Application and Applet

Coding the Program -The Method Header

Page 21: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

21Chapter 2: Creating a Java Application and Applet

Coding the Program -The Method Header • Modifiers set properties for a method

– public allows other programs to invoke this method– static means this method is unique and can be

invoked with creating an instance

• Parameters are pieces of data received by the method to help the method perform its operation– Identifiers are used to name the variable sent to the

method

• Return type is the data type of the data returned by the method– If no data is returned, the keyword void is used

Page 22: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

22Chapter 2: Creating a Java Application and Applet

Coding Output

• Call the System.out.println() method in the SDK to display output to the monitor– System is the class– out is the object representing the output device– println() is the method

Page 23: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

23Chapter 2: Creating a Java Application and Applet

Coding Output

• When calling a method, arguments are placed in parentheses– String literals are placed in quotation marks– Numeric literals and variables do not need quotation

marks

• Period delimiters separate the class, object, and method

• Semicolons must be placed after every statement except headers and braces

• Braces {} enclose the body of a method

Page 24: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

24Chapter 2: Creating a Java Application and Applet

Testing the Solution

• Compile the source code– javac.exe command

• In TextPad, use the Compile Java command• At the command prompt, type javac filename.java

– A new bytecode file for each class is created with a .class extension

• If the compiler detects errors, fix the errors and compile again

• If the compilation was successful, run the program

Page 25: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

25Chapter 2: Creating a Java Application and Applet

Debugging the Solution

• System Errors– System command is not set properly– Software is installed incorrectly– Location of stored files is not accessible

• Syntax Errors– One or more violations of the syntax rules of Java

• Semantic Errors– The code meaning is unrecognizable to the compiler

• Logic and Run-Time Errors– Unexpected conditions during execution of a program

Page 26: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

26Chapter 2: Creating a Java Application and Applet

Debugging the Solution

Page 27: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

27Chapter 2: Creating a Java Application and Applet

Running the Application

• After compilation is successful, run the program to test for logic and run-time errors

• Use the Run Java Application command in TextPad– TextPad automatically finds the class file with the

same name

• Use the java command from the command prompt – Syntax: java classname (no extension)

Page 28: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

28Chapter 2: Creating a Java Application and Applet

Editing the Source Code

Page 29: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

29Chapter 2: Creating a Java Application and Applet

Import Packages

• Use the import statement to access classes in the SDK– The java.lang package is automatically

imported– Place the import statement before the class

header– Use an asterisk (*) after the package name

and period delimiter to import all necessary classes in the package

Page 30: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

30Chapter 2: Creating a Java Application and Applet

Page 31: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

31Chapter 2: Creating a Java Application and Applet

Call a System Date Constructor

• Use the Date class in the java.util package to access the system date

• Store the Date in an object variable• Declare the object variable by calling the Date

constructor– The constructor is a method denoted by the new

keyword followed by the object type and parentheses– Declaration syntax:

objectType variableName = new objectType();

Page 32: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

32Chapter 2: Creating a Java Application and Applet

Format Output Using Escape Characters• Use escape characters inside String arguments

to move the output of data

Page 33: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

33Chapter 2: Creating a Java Application and Applet

Editing the source code - cont.

• Recompile and run the application– The bytecode should be updated after any

changes to the source code

• Print a hard copy of the source code– The final step of the program development

cycle is to document the solution

• Quit TextPad by clicking on the Close button

Page 34: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

34Chapter 2: Creating a Java Application and Applet

Moving to the Web

• Characteristics of an applet – Applets run within a browser/viewer and are usually

delivered to the client machine via the Web– Applets cannot use system resources or files on the

client machine

• Convert the application into an applet– Import two packages– Change the class name and extend the Applet class– Include a paint method to draw text and display color

and a graphic

Page 35: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

35Chapter 2: Creating a Java Application and Applet

Import Applet Packages

• Applet package (java.applet.*)– Allows applets to inherit attributes and

methods

• AWT package (java.awt.*)– Provides access to color, draw methods, and

GUI elements

Page 36: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

36Chapter 2: Creating a Java Application and Applet

Page 37: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

37Chapter 2: Creating a Java Application and Applet

Change the Class Name and Extend the Applet Class• Change the class name and file name to

create a new applet file

• Edit the comment header in the applet file

• Add “extends Applet” in the class header to inherit from the superclass, Applet– Provides the init() method to load the applet in

the browser window

Page 38: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

38Chapter 2: Creating a Java Application and Applet

The paint() Method

• Accepts a Graphics object as a parameter

• The Graphics object is commonly referred to by the variable name g– The variable g is created and initialized in the

init() method– The variable g is a reference variable, or a

specific instance of an object

• The return type is void

Page 39: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

39Chapter 2: Creating a Java Application and Applet

The drawString() Method

• Displays text in the applet window

• Accepts three arguments– The String data

• If the data is not a String object, convert it to a String object using the toString() method

– The horizontal and vertical coordinates of the String

• The coordinates are measured in pixels

• Called by the Graphics object, g

Page 40: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

40Chapter 2: Creating a Java Application and Applet

Draw an Image

• Declare an Image object• Use the getImage() method to load the image

– The getImage() method calls the getDocumentBase() method to pull the image from the current folder

• Use the drawImage() method to set the coordinates of the image

Page 41: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

41Chapter 2: Creating a Java Application and Applet

Set the Background Color

• Use the setBackground() method to change the background color of the applet window– The setBackground() method does not need to be

called from a reference variable

Page 42: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

42Chapter 2: Creating a Java Application and Applet

Creating an HTML Host Document• A host program, such as a Web page executes

the applet

Page 43: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

43Chapter 2: Creating a Java Application and Applet

Creating an HTML Host Document• The Web page contains HTML tags to

define a section or format– A tag consists of a start tag, denoted by <>

and an end tag, denoted by </>

• The tag, <APPLET>…</APPLET>, informs the browser of the applet– The applet tag encloses the name of the

bytecode applet file and the width and height of the applet window

Page 44: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

44Chapter 2: Creating a Java Application and Applet

Running an Applet

• An applet is run by opening the HTML host document

• In TextPad, use the Run Java Applet command• At the command prompt, type appletviewer

followed by the name of the host document• Use Applet Viewer to test the applet

– Ignores irrelevant HTML code– Uses less memory than a browser– Does not have to be Java-enabled

Page 45: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

45Chapter 2: Creating a Java Application and Applet

Chapter Summary

• Use TextPad to write, compile, and run code• Learn the basic form of an application and an

applet • Insert comments as documentation• Code Class and Method headers• Create a console application

– Use the println() method– Format output using escape characters– Import the java.util package– Call a Date constructor

Page 46: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

46Chapter 2: Creating a Java Application and Applet

Chapter Summary

• Compile a Java program• Debug a Java program

– Differentiate between types of errors

• Execute a Java program• Edit the application to create an applet

– Import applet packages– Extend the Applet class– Use the paint(), drawString(), and getImage methods

• Create a HTML Host Document

Page 47: Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet

Java Programming, 2EIntroductory Concepts

and Techniques

Chapter 2 Complete