introduction to java (power point)

23

Click here to load reader

Upload: mark-perkins

Post on 29-Apr-2017

243 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to Java (Power Point)

Java Programming Fifth Edition

Chapter 1Creating Your First Java Classes

Page 2: Introduction to Java (Power Point)

Java Programming, Fifth Edition 2

Learning About Programming

• Program – Set of written instructions that tells computer what to do

• Machine language – Most basic circuitry-level language– Low-level programming language

• High-level programming language – Allows you to use vocabulary of reasonable terms

• Syntax– Rules of language

• Program statements– Similar to English sentences– Carry out tasks of program

Page 3: Introduction to Java (Power Point)

Java Programming, Fifth Edition 3

Learning About Programming (continued)

• Compiler or interpreter – Translates language statements into machine code

• Syntax error– Misuse of language– Misspelled programming language word

• Debugging– Freeing program of all errors

• Logic errors– Also called semantic errors– Incorrect order or procedure

Page 4: Introduction to Java (Power Point)

Java Programming, Fifth Edition 4

Introducing Object-OrientedProgramming Concepts

• Procedural programming – Sets of operations executed in sequence– Variables

• Named computer memory locations that hold values– Procedures

• Individual operations grouped into logical units

• Object-oriented programs – Create classes– Create objects from classes– Create applications

Page 5: Introduction to Java (Power Point)

Java Programming, Fifth Edition 5

Understanding Objects and Classes

• Objects– Made up of attributes and methods

• Attributes– Characteristics that define object– Differentiate objects of same class– Value of attributes is object’s state

• Class – Describes objects with common properties– Definition– Instance

Page 6: Introduction to Java (Power Point)

Understanding Objects and Classes (continued)

Java Programming, Fifth Edition 6

Page 7: Introduction to Java (Power Point)

Java Programming, Fifth Edition 7

Learning About Java

• Java– Developed by Sun Microsystems – Object-oriented language – General-purpose– Advantages

• Security features• Architecturally neutral

• Java (continued) – Can be run on wide variety of computers – Does not execute instructions on computer directly– Runs on hypothetical computer known as Java virtual machine (JVM)

• Source code – Programming statements written in high-level programming langua

Page 8: Introduction to Java (Power Point)

Java Programming, Fifth Edition 8

Learning About Java (continued)

• Bytecode– Statements saved in file– Java compiler converts source code into binary

program • Java interpreter

– Checks bytecode and communicates with operating system

– Executes bytecode instructions line by line within Java virtual machine

Page 9: Introduction to Java (Power Point)

Java Programming, Fifth Edition 9

Learning About Java (continued)

Page 10: Introduction to Java (Power Point)

Java Programming, Fifth Edition 10

Analyzing a Java Application That Uses Console Output (continued)

Page 11: Introduction to Java (Power Point)

Understanding the StatementThat Prints the Output (continued)

Java Programming, Fifth Edition 11

Page 12: Introduction to Java (Power Point)

Java Programming, Fifth Edition 12

Understanding the First Class

• Everything used within Java program must be part of a class• Define Java class using any name or identifier • Requirements for identifiers

– Must begin with:• Letter of English alphabet• Or non-English letter (such as α or π)

– Cannot begin with digit– Can only contain:

• Letters• Digits• Underscores• Dollar signs

– Cannot be Java reserved keyword– Cannot be true, false, or null

Page 13: Introduction to Java (Power Point)

Java Programming, Fifth Edition 13

Understanding the First Class (continued)

Page 14: Introduction to Java (Power Point)

Understanding the First Class

Java Programming, Fifth Edition 14

Page 15: Introduction to Java (Power Point)

Understanding the First Class (continued)

Java Programming, Fifth Edition 15

Page 16: Introduction to Java (Power Point)

Java Programming, Fifth Edition 16

Understanding the main() Method

• static– Reserved keyword – Means method accessible and usable

• Even though no objects of class exist • void

– Use in main() method header– Does not indicate main() method empty– Indicates main() method does not return value

when called– Doesn’t mean main() doesn’t produce output

Page 17: Introduction to Java (Power Point)

Shell Code

Java Programming, Fifth Edition 17

Page 18: Introduction to Java (Power Point)

Java Programming, Fifth Edition 18

Adding Comments to a Java Class

• Program comments– Nonexecuting statements added to program for documentation– Use to leave notes for yourself or others– Include author, date, class’s name or function

• Comment out a statement– Compiler does not translate

• Types of Java comments– Line comments

• Start with two forward slashes (//) • Continue to end of current line • Do not require ending symbol

– Block comments • Start with forward slash and asterisk (/*)• End with asterisk and forward slash (*/)

Page 19: Introduction to Java (Power Point)

Java Programming, Fifth Edition 19

Adding Comments to a Java Class (continued)

• Types of Java comments (continued) – Javadoc comments

• Special case of block comments• Begin with slash and two asterisks (/**) • End with asterisk and forward slash (*/)• Use to generate documentation

Page 20: Introduction to Java (Power Point)

Java Programming, Fifth Edition 20

Creating a Java ApplicationUsing GUI Output

• JOptionPane– Produce dialog boxes

• Dialog box – GUI object resembling window– Messages placed for display

• Package– Group of classes

• import statement– Use to access built-in Java class

Page 21: Introduction to Java (Power Point)

Java Programming, Fifth Edition 21

Creating a Java ApplicationUsing GUI Output (continued)

Page 22: Introduction to Java (Power Point)

Java Programming, Fifth Edition 22

Correcting Errors andFinding Help (continued)

• Parsing – Process compiler uses to divide source code into

meaningful portions• Logic error

– Syntax correct but produces incorrect results when executed

– Usually more difficult to find and resolve• Java API

– Also called the Java class library– Prewritten Java classes

Page 23: Introduction to Java (Power Point)

Don’t Do It

• File’s name must match name of class• Don’t confuse names parentheses, braces,

brackets, curly braces, square brackets, and angle brackets

• Don’t forget to end a block comment• Don’t forget that Java is case sensitive• End every statement with semicolon

– Do not end class or method headers with semicolon• Recompile when making changes

Java Programming, Fifth Edition 23