l1-2:csc210 2014-2015 ©dr. basheer m. nasef lecture #1 by dr. basheer m. nasef

32
L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef Welcome to Object Oriented With JAVA Lecture #1 By Dr. Basheer M. Nasef

Upload: sharon-borton

Post on 15-Dec-2015

223 views

Category:

Documents


7 download

TRANSCRIPT

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Welcome toObject Oriented

With JAVALecture #1

ByDr. Basheer M. Nasef

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef 2

Agenda

Introduction to Java ProgrammingExamples

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef 3

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.

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

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.

4

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

JDK EditionsJava 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.

7

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Popular Java IDEs (Integrated development environment)

NetBeans Open Source by Sun Eclipse Open Source by IBM Borland JBuilder 2007 (Based on Eclipse)JCreator.

8

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Java Program

9

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).

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Java Program

10

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.

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

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!"); }}

11

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Creating and Editing Using NotePad

To use NotePad, type notepad

Welcome.java from the DOS prompt.

12

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Creating, Compiling, and Running Programs

13

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Trace a Program Execution

14

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }}

Enter main method

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Trace a Program Execution

15

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }}

Execute statement

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Trace a Program Execution

16

//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); }}

print a message to the console

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Anatomy of a Java ProgramCommentsPackageReserved wordsModifiersStatementsBlocksClassesMethodsThe main method

20

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Comments

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

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

21

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.

Three types of comments in Java.

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

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.

22

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Reserved WordsReserved 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.

23

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

ModifiersJava 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.”

24

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

StatementsA 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 (;).

25

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Blocks

26

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 22: L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Lecture #1 By Dr. Basheer M. Nasef

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

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.

27

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

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 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.

28

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

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

29

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

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

30

Run 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) .

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

The showMessageDialog Method JOptionPane.showMessageDialog(null, "Welcome to Java!", "Display Message", JOptionPane.INFORMATION_MESSAGE);

31

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

Two Ways to Invoke the MethodThere 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.32

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

PROGRAMMING EXERCISES(Displaying a pattern) Write a program that displays the following pattern:

33

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

PROGRAMMING EXERCISES(Displaying a pattern) Write a program that displays the following pattern:

34

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

PROGRAMMING EXERCISES(Displaying a pattern) Write a program that displays the following pattern:

35

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

PROGRAMMING EXERCISES

36

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

L1-2:CSC210 2014-2015 © Dr. Basheer M. Nasef

PROGRAMMING EXERCISES

37

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