1 CS1001 Lecture 13. 2 Overview Java Programming Java Programming.
Introduction to Java Programming with Forte
description
Transcript of Introduction to Java Programming with Forte
![Page 1: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/1.jpg)
Introduction toJava Programming with Forte
Y. Daniel Liang
![Page 2: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/2.jpg)
2
Introduction
Course Objectives Organization of the Book
![Page 3: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/3.jpg)
3
Course Objectives Upon completing the course, you will understand
– Create, compile, and run Java programs
– Primitive data types
– Java control flow
– Methods– Arrays (for teaching Java in two semesters, this could be the end)
– Object-oriented programming
– Core Java classes (Swing, exception, internationalization, multithreading, multimedia, I/O, networking, Java Collections Framework)
![Page 4: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/4.jpg)
4
Course Objectives, cont.
You will be able to – Develop programs using Forte– Write simple programs using primitive data
types, control statements, methods, and arrays.– Create and use methods– Develop a GUI interface and Java applets– Write interesting projects– Establish a firm foundation on Java concepts
![Page 5: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/5.jpg)
5
Book Chapters Part I: Fundamentals of Programming
– Chapter 1 Introduction to Java and Forte
– Chapter 2 Primitive Data Types and Operations
– Chapter 3 Control Statements
– Chapter 4 Methods
– Chapter 5 Arrays
![Page 6: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/6.jpg)
6
Book Chapters, cont. Part II: Object-Oriented Programming
– Chapter 6 Objects and Classes
– Chapter 7 Strings
– Chapter 8 Class Inheritance and Interfaces
– Chapter 9 Object-Oriented Software Development
![Page 7: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/7.jpg)
7
Book Chapters, cont. Part III: GUI Programming
– Chapter 10 Getting Started with GUI Programming
– Chapter 11 Creating User Interfaces
– Chapter 12 Applets and Advanced GUI
![Page 8: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/8.jpg)
8
Book Chapters, cont. Part IV: Developing Comprehensive Projects
– Chapter 13 Exception Handling
– Chapter 14 Internationalization
– Chapter 15 Multithreading
– Chapter 16 Multimedia
– Chapter 17 Input and Output
– Chapter 18 Networking
– Chapter 19 Java Data Structures
![Page 9: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/9.jpg)
9
Chapter 1 Introduction to Java and Forte
What Is Java? Getting Started With Java Programming
– Create, Compile and Running a Java Application
![Page 10: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/10.jpg)
10
What Is Java?
History
Characteristics of Java
![Page 11: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/11.jpg)
11
History James Gosling and Sun Microsystems
Oak
Java, May 20, 1995, Sun World
HotJava – The first Java-enabled Web browser
JDK Evolutions
J2SE, J2ME, and J2EE (not mentioned in the book, but could discuss here optionally)
![Page 12: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/12.jpg)
12
Characteristics of Java Java is simple
Java is object-oriented
Java is distributed
Java is interpreted
Java is robust
Java is secure
Java is architecture-neutral
Java is portable
Java’s performance
Java is multithreaded
Java is dynamic
![Page 13: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/13.jpg)
13
JDK Versions JDK 1.02 (1995) JDK 1.1 (1996) Java 2 SDK v 1.2 (a.k.a JDK 1.2, 1998) Java 2 SDK v 1.3 (a.k.a JDK 1.3, 2000) Java 2 SDK v 1.4 (a.k.a JDK 1.4, 2002)
![Page 14: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/14.jpg)
14
JDK Editions Java Standard Edition (J2SE)
– J2SE can be used to develop client-side standalone applications or applets.
Java Enterprise Edition (J2EE)– J2EE can be used to develop server-side applications
such as Java servlets and Java ServerPages. Java Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile devices such as cell phones.
This book uses J2SE to introduce Java programming.
![Page 15: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/15.jpg)
15
Java IDE Tools Forte by Sun MicroSystems Borland JBuilder
Microsoft Visual J++
WebGain Café
IBM Visual Age for Java
![Page 16: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/16.jpg)
16
Getting Started with Forte
Main Window
![Page 17: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/17.jpg)
17
Getting Started with Forte, cont.
Explorer Window
![Page 18: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/18.jpg)
18
Getting Started with Forte, cont.
Create a Class
![Page 19: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/19.jpg)
19
Getting Started with Forte, cont.
Source Editor
![Page 20: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/20.jpg)
20
Getting Started with Forte, cont.
Code Completion Assistance in the Source Editor
![Page 21: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/21.jpg)
21
Getting Started with Forte, cont.
Output Window
![Page 22: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/22.jpg)
22
Getting Started with Java Programming
A Simple Java Application
Compiling Programs
Executing Applications
![Page 23: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/23.jpg)
23
A Simple Application
Example 1.1//This application program prints Welcome//to Java! package chapter1;
public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }}
RunRunSourceSource
NOTE: To run the program, install slide files on hard disk.
![Page 24: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/24.jpg)
24
Creating and Compiling Programs
On command line– javac file.java
Source Code
Create/Modify Source Code
Compile Source Code i.e. javac Welcome.java
Bytecode
Run Byteode i.e. java Welcome
Result
If compilation errors
If runtime errors or incorrect result
![Page 25: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/25.jpg)
25
Executing Applications
On command line– java classname
JavaInterpreter
on Windows
JavaInterpreter
on Sun Solaris
JavaInterpreteron Linux
Bytecode
...
![Page 26: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/26.jpg)
26
Example
javac Welcome.java
java Welcome
output:...
![Page 27: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/27.jpg)
27
Compiling and Running a Program
Where are the files stored in the directory?
c:\example
chapter1 Welcome.class
Welcome.java
chapter2
.
.
.
Java source files and class files for Chapter 2
chapter19 Java source files and class files for Chapter 19
Welcome.java~
![Page 28: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/28.jpg)
28
Anatomy of a Java Program Comments Package Reserved words Modifiers Statements Blocks Classes Methods The main method
![Page 29: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/29.jpg)
29
Comments
In Java, comments are preceded by two slashes (//) in a line, or enclosed between /* and */ in one or multiple lines. When the compiler sees //, it ignores all text after // in the same line. When it sees /*, it scans for the next */ and ignores any text between /* and */.
![Page 30: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/30.jpg)
30
Package
The second line in the program (package chapter1;) specifies a package name, chapter1, for the class Welcome. Forte compiles the source code in Welcome.java, generates Welcome.class, and stores Welcome.class in the chapter1 folder.
![Page 31: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/31.jpg)
31
Reserved Words
Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Other reserved words in Example 1.1 are public, static, and void. Their use will be introduced later in the book.
![Page 32: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/32.jpg)
32
Modifiers
Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected. A public datum, method, or class can be accessed by other programs. A private datum or method cannot be accessed by other programs. Modifiers are discussed in Chapter 6, "Objects and Classes."
![Page 33: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/33.jpg)
33
Statements
A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") in the program in Example 1.1 is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;).
![Page 34: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/34.jpg)
34
Blocks
A pair of braces in a program forms a block that groups components of a program.
public class Test { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
Class block
Method block
![Page 35: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/35.jpg)
35
Classes
The class is the essential Java construct. A class is a template or blueprint for objects. To program in Java, you must understand classes and be able to write and use them. The mystery of the class will continue to be unveiled throughout this book. For now, though, understand that a program is defined by using one or more classes.
![Page 36: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/36.jpg)
36
MethodsWhat is System.out.println? It is a method: a collection of statements that performs a sequence of operations to display a message on the console. It can be used even without fully understanding the details of how it works. It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In this case, the argument is "Welcome to Java!" You can call the same println method with a different argument to print a different message.
![Page 37: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/37.jpg)
37
main MethodThe main method provides the control of program flow. The Java interpreter executes the application by invoking the main method. The main method looks like this: public static void main(String[] args) { // Statements;}
![Page 38: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/38.jpg)
38
Displaying Text in a Message Dialog Box
you can use the showMessageDialog method in the JOptionPane class. JOptionPane is one of the many predefined classes in the Java system, which can be reused rather than “reinventing the wheel.”
RunRunSourceSource
![Page 39: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/39.jpg)
39
The showMessageDialog Method JOptionPane.showMessageDialog(null, "Welcome to Java!",
"Example 1.2", JOptionPane.INFORMATION_MESSAGE));
![Page 40: Introduction to Java Programming with Forte](https://reader036.fdocuments.in/reader036/viewer/2022062723/56813e21550346895da8020a/html5/thumbnails/40.jpg)
40
The exit Method
Use Exit to terminate the program and stop all threads.
NOTE: When your program starts, a thread is spawned to run the program. When the showMessageDialog is invoked, a separate thread is spawned to run this method. The thread is not terminated even you close the dialog box. To terminate the thread, you have to invoke the exit method.