Chapter 1 Introduction to Java

Download Report

Transcript Chapter 1 Introduction to Java

Lecture #1 By Dr. Basheer M. Nasef L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef

Agenda

 Introduction to Java Programming  Examples L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 2

Learning Strategies

A programming course is quite different from other courses.

 In a programming course, you learn from examples, from practice, and from mistakes.You need to devote a lot of time to writing programs, testing them, and fixing errors.

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 3

JDK & JRE ?

JRE (Java Runtime) is needed for running Java programs.  JDK (Java Development Kit), which includes JRE plus the development tools (such as compiler and debugger), is need for writing as well as running Java programs.  To write Java Programs, you should install JDK, which includes JRE.

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 4

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 Course (

CSC210

) uses

J2SE

to introduce Java programming.

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 7

Popular Java

IDEs (Integrated development environment )  NetBeans Open Source by Sun  Eclipse Open Source by IBM  Borland JBuilder 2007 (Based on Eclipse)  JCreator.

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 8

Java Program

 Java programs consist of pieces called classes.

 Classes include pieces called methods that perform tasks and return information when they complete them.

 Programmers can create each piece they need to form Java programs. However, most Java programmers take advantage of the rich collections of existing classes in the Java class libraries, which are also known as the Java APIs (Application Programming Interfaces).

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 9

Java Program

 Thus, there are really two aspects to learning the Java "world."  The first is the Java language itself, so that you can program your own classes.

 The second is the classes in the extensive Java class libraries. Throughout this course, we discuss many library classes.

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 10

Creating, Compiling, and Executing a Java Program

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 11

Creating and Editing Using NotePad

To use NotePad, type notepad Welcome.java from the DOS prompt.

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 12

Creating, Compiling, and Running Programs

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 13

Trace a Program Execution

Enter main method //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 14

Trace a Program Execution

Execute statement //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 15

Trace a Program Execution

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef print a message to the console 16

Anatomy of a Java Program

 Comments  Package  Reserved words  Modifiers  Statements  Blocks  Classes  Methods  The main method L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 20

Comments

Three types of comments in Java.

I.

II.

Line comment: A line comment is preceded by two slashes (//) in a line.

Paragraph comment: A paragraph comment is enclosed between /* and */ in one or multiple lines.

javadoc comment

: javadoc comments begin with /** and end with */. They are used for documenting classes, data, and methods. They can be extracted into an HTML file using JDK's javadoc command. L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 21

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.

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 22

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 above program are public, static, and void. Their use will be introduced later.

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 23

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.” L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 24

Statements

A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;).

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 25

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

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 26

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. For now, though, understand that a program is defined by using one or more classes. L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 27

Methods

What 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 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. L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 28

main Method

The 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;

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 29

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.” WelcomeInMessageDialogBox Run L1-2:CSC210 2014-2015

©

IMPORTANT NOTE: To enable the buttons, you must store the entire slide file

Link_Files

files into a directory (e.g., e:\Link_Files_CSC210) . Dr. Basheer M. Nasef 30

The showMessageDialog Method

JOptionPane.showMessageDialog(null, "Welcome to Java!", "Display Message", JOptionPane.INFORMATION_MESSAGE); L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 31

Two Ways to Invoke the Method

There are several ways to use the showMessageDialog method. For the time being, all you need to know are two ways to invoke it.

One is to use a statement as shown in the example: JOptionPane.showMessageDialog(null, x, y, JOptionPane.INFORMATION_MESSAGE); where x is a string for the text to be displayed, and y is a string for the title of the message dialog box.

The other is to use a statement like this: JOptionPane.showMessageDialog(null, x); where x is a string for the text to be displayed.

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 32

PROGRAMMING EXERCISES

(Displaying a pattern) Write a program

that displays the following pattern:

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 33

PROGRAMMING EXERCISES

(Displaying a pattern) Write a program

that displays the following pattern:

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 34

PROGRAMMING EXERCISES

(Displaying a pattern) Write a program

that displays the following pattern:

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 35

PROGRAMMING EXERCISES

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 36

PROGRAMMING EXERCISES

 Write a program that asks the user to enter two numbers, then prints the sum, product, and difference of the two numbers.

L1-2:CSC210 2014-2015

©

Dr. Basheer M. Nasef 37