introduction to java - department of math/cs - homeliang, introduction to java programming, ninth...

36
1 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. Introduction to Java

Upload: others

Post on 16-Jul-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

1Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Introduction to Java

Page 2: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

2Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Programs

Computer programs, known as software, are instructions to the computer.

You tell a computer what to do through programs.

Programs are written using programming languages.

Page 3: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

3Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Programming LanguagesMachine Language Assembly Language High-Level Language

1101101010011010

Computers can run instructions only in machine language!

Page 4: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

4Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Programming LanguagesMachine Language Assembly Language High-Level Language

Humans are not comfortable with machine languages, so they made low-level languages like Assembly.

ADDF3 R1, R2, R3

… ADDF3 R1, R2, R3 …

Assembly Source File

Assembler

… 1101101010011010 …

Machine Code File

Page 5: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

5Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Programming LanguagesMachine Language Assembly Language High-Level Language

Then high-level languages were invented which are English-like and easy to learn and program.

Sum = 4 + 5;

Area = 5 * 5 * 3.1415;

Page 6: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

6Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Popular High-Level Languages

Language Description

Ada

BASIC

C

C++

C#

COBOL

FORTRAN

Java

Pascal

Python

Visual Basic

Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada language was developed for the Department of Defense and is used mainly in defense projects.

Beginner’s All-purpose Symbolic Instruction Code. It was designed to be learned and used easily by beginners.

Developed at Bell Laboratories. C combines the power of an assembly language with the ease of use and portability of a high-level language.

C++ is an object-oriented language, based on C.

Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by Microsoft.

COmmon Business Oriented Language. Used for business applications.

FORmula TRANslation. Popular for scientific and mathematical applications.

Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform-independent Internet applications.

Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a simple, structured, general-purpose language primarily for teaching programming.

A simple general-purpose scripting language good for writing short programs.

Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop graphical user interfaces.

Page 7: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

7Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Interpreting/Compiling Source Code

A program written in a high-level language is called a source program or source code.

Source Code → Machine Code (Or Virtual Machine Code) → Execute

The translation can be done using another programming tool called an interpreter or a compiler.

Page 8: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

8Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Interpreting Source CodeAn interpreter reads one statement from the source code, translates it to the machine code or virtual machine code, and then executes it right away.

… area = 5 * 5 * 3.1415; ...

High-level Source File

Interpreter Output

Page 9: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

9Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Compiling Source CodeA compiler translates the entire source code into a machine-code file, and the machine-code file is then executed, as shown in the following figure.

… area = 5 * 5 * 3.1415; ...

High-level Source File

Compiler Executor Output …

0101100011011100 1111100011000100 … ...

Machine-code File

Page 10: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

10Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

A Simple Java Program

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

Page 11: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

11Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Creating and Editing Using geditTo use , type

gedit Welcome.java from the terminal.

Page 12: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

12Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

javac Welcome.java

Compiling Java program

javac is the java compiler It translates Java source code to java bytecode (a type of virtual machine code)

Page 13: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

13Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

java Welcome

Running Java program

Java Virtual Machine is a software that can execute Java bytecode.

Page 14: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

15Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

Trace a Program ExecutionEnter main method

Page 15: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

16Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

Trace a Program ExecutionExecute statement

Page 16: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

17Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

Trace a Program Execution

print a message to the console

Page 17: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

18Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Hierarchy of a Java Program

� A book (program) consists of a number of chapters (classes)

� Each chapter (class) consists of a number of paragraphs (methods)

� Each paragraph (method) consists of a number of sentences (statements)

� Each sentence (statement) must obey the syntax rules in the English (Java) language

Page 18: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

19Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

Class

Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome.

Page 19: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

20Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

Method

A method contains a set of statements!

In order to run a class, the class must contain a method named main. The program is executed from the main method.

Page 20: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

21Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

StatementA statement represents an action or a sequence of actions.

Page 21: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

22Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

Statement Terminator

Every statement in Java ends with a semicolon (;).

Page 22: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

23Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

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.

Page 23: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

24Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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 24: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

25Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Special Symbols

Character Name Description

{}

() [] // " "

;

Opening and closing braces

Opening and closing parentheses

Opening and closing brackets

Double slashes

Opening and closing quotation marks

Semicolon

Denotes a block to enclose statements.

Used with methods.

Denotes an array. Precedes a comment line. Enclosing a string (i.e., sequence of characters).

Marks the end of a statement.

Page 25: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

26Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

{ … }

Page 26: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

27Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

( … )

Page 27: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

28Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

;

Page 28: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

29Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

// …

Page 29: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

30Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

" … "

Page 30: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

31Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Programming Style and Documentation

� Appropriate Comments� Naming Conventions� Proper Indentation and Spacing

Lines� Block Styles

Page 31: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

32Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Appropriate Comments

Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses.

Page 32: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

33Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Naming Conventions

� Choose meaningful and descriptive names.� Class names:

– Capitalize the first letter of each word in the name. For example, the class name ComputeExpression.

Page 33: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

34Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Proper Indentation and Spacing

� Indentation– Indent two spaces.

� Spacing – Use blank line to separate segments of the code.

Page 34: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

35Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Block Styles

Use end-of-line style for braces.

  public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } }

public class Test { public static void main(String[] args) {

System.out.println("Block Styles"); } }

End-of-line style

Next-line style

Page 35: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

36Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

JDK (Java Development Kit)� - The Java Development Kit (JDK) is a software

development environment used for developing Java applications.

� - It includes the Java Runtime Environment (JRE), an executer/launcher (java), a compiler (javac), an archiver (jar), a documentation generator (javadoc) and other tools needed in Java development.

� - Newest Version:

JDK 1.7 (2011) a. k. a. JDK 7 or Java 7

Page 36: Introduction to Java - Department of Math/CS - HomeLiang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 3 Programming Languages

37Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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, Java ServerPages, and Java ServerFaces.

� Java Micro Edition (J2ME). – J2ME can be used to develop applications for mobile

devices such as cell phones.