Joyce Farrel-Chapter 1 Slides
Joyce Farrel-Chapter 1 Slides
Chapter 1
1
Objectives
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 2
classroom use.
Learning Programming Terminology (1 of
3)
• Computer program
• A set of written instructions that tells the computer what to do
• Machine language
• The most basic circuitry-level language
• A low-level programming language
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 3
classroom use.
Learning Programming Terminology (2 of
3)
• High-level programming language
• Allows you to use a vocabulary of reasonable terms
• Syntax
• A specific set of rules for the language
• Program statements
• Similar to English sentences
• Commands to carry out program tasks
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 4
classroom use.
Learning Programming Terminology (3 of
3)
• Compiler or interpreter
• Translates language statements into machine code
• Syntax error
• Misuse of language rules
• A misspelled programming language word
• Debugging
• Freeing program of all errors
• Logic errors
• Also called semantic errors
• Incorrect order or procedure
• The program may run but provide inaccurate output
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 5
classroom use.
Comparing Procedural and Object-Oriented
Programming Concepts (1 of 2)
• 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
- Blueprints for an object
• Create objects from classes
• Create applications
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 6
classroom use.
Comparing Procedural and Object-Oriented
Programming Concepts (2 of 2)
• Object-oriented programming was used most frequently for two major types of
applications
• Computer simulations
• Graphical user interfaces (GUIs)
- Not all object-oriented programs are written to use a GUI
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 7
classroom use.
Understanding Classes, Objects, and
Encapsulation (1 of 3)
• Class
• Describes objects with common properties
• A definition
• An instance
• Attributes
• Characteristics that define an object
• Differentiate objects of the same class
• The value of attributes is an object’s state
• Objects
• Specific, concrete instances of a class
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 8
classroom use.
Understanding Classes, Objects, and
Encapsulation (2 of 3)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 9
classroom use.
Understanding Classes, Objects, and
Encapsulation (3 of 3)
• Method
• A self-contained block of program code that carries out an action
• Similar to a procedure
• Encapsulation
• Conceals internal values and methods from outside sources
• Provides security
• Keeps data and methods safe from inadvertent changes
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 10
classroom use.
Understanding Inheritance and
Polymorphism
• Inheritance
• An important feature of object-oriented programs
• Classes share attributes and methods of existing classes but with more specific
features
• Helps you understand real-world objects
• Polymorphism
• Means “many forms”
• Allows the same word to be interpreted correctly in different situations based on
context
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 11
classroom use.
Features of the Java Programming
Language (1 of 5)
• Java
• Developed by Sun Microsystems
• An object-oriented language
• General-purpose
• Advantages
- Security features
- Architecturally neutral
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 12
classroom use.
Features of the Java Programming
Language (2 of 5)
• Java (cont’d.)
• Can be run on a wide variety of computers
• Does not execute instructions on the computer directly
• Runs on a hypothetical computer known as a Java Virtual Machine (JVM)
• Source code
• Programming statements written in high-level programming language
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 13
classroom use.
Features of the Java Programming
Language (3 of 5)
• Development environment
• A set of tools used to write programs
• Bytecode
• Statements saved in a file
• A binary program into which the Java compiler converts source code
• Java interpreter
• Checks bytecode and communicates with the operating system
• Executes bytecode instructions line by line within the Java Virtual Machine
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 14
classroom use.
Features of the Java Programming
Language (4 of 5)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 15
classroom use.
Features of the Java Programming
Language (5 of 5)
• WORA
• Write once, run anywhere
• Console applications
- Support character output
• Windowed applications
- Menus
- Toolbars
- Dialog boxes
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 16
classroom use.
Analyzing a Java Application that Produces
Console Output (1 of 2)
• Even the simplest Java application involves a fair amount of confusing syntax
• Print “First Java application” on the screen
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 17
classroom use.
Analyzing a Java Application that Produces
Console Output (2 of 2)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 18
classroom use.
Understanding the Statement that Produces
the Output (1 of 2)
• Literal string
• Will appear in output exactly as entered
• Written between double quotation marks
• Arguments
• Pieces of information passed to a method
• Method
• Requires information to perform its task
• System class
• Refers to the standard output device for a system
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 19
classroom use.
Understanding the Statement that Produces
the Output (2 of 2)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 20
classroom use.
Understanding the First Class (1 of 6)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 21
classroom use.
Understanding the First Class (2 of 6)
• Access specifier
• Defines how a class can be accessed
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 22
classroom use.
Understanding the First Class (3 of 6)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 23
classroom use.
Understanding the First Class (4 of 6)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 24
classroom use.
Understanding the First Class (5 of 6)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 25
classroom use.
Understanding the First Class (6 of 6)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 26
classroom use.
Understanding the main() Method (1 of 3)
• static
• A reserved keyword
• Means the method is accessible and usable even though no objects of the class exist
• void
• Use in the main() method header
• Does not indicate the main() method is empty
• Indicates the main() method does not return a value when called
• Does not mean that main() doesn’t produce output
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 27
classroom use.
Understanding the main() Method (2 of 3)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 28
classroom use.
Understanding the main() Method (3 of 3)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 29
classroom use.
Indent Style
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 30
classroom use.
Saving a Java Class
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 31
classroom use.
Compiling a Java Class and Correcting
Syntax Errors (1 of 2)
• Compiling a Java class
• Compile the source code into bytecode
• Translate the bytecode into executable statements
- Using a Java interpreter
• Type javac First.java
• Compilation outcomes
• javac is an unrecognized command
• Program language error messages
• No messages indicating successful completion
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 32
classroom use.
Compiling a Java Class and Correcting
Syntax Errors (2 of 2)
• Reasons for error messages
• Misspelled the command javac
• A misspelled filename
• Not within the correct subfolder or subdirectory on the command line
• Improper installation of Java
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 33
classroom use.
Correcting Syntax Errors
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 34
classroom use.
Running a Java Application and Correcting
Logical Errors (1 of 2)
• Run the application from the command line
• Type java First
• Shows the application’s output in the command window
• The class is stored in a folder named Java on the C drive
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 35
classroom use.
Running a Java Application and Correcting
Logical Errors (2 of 2)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 36
classroom use.
Modifying a Compiled Java Class (1 of 2)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 37
classroom use.
Modifying a Compiled Java Class (2 of 2)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 38
classroom use.
Correcting Logic Errors
• Logic error
• The syntax is correct but incorrect results were produced when executed
• Run-time error
• Not detected until execution
• Often difficult to find and resolve
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 39
classroom use.
Adding Comments to a Java Class (1 of 4)
• Program comments
• Nonexecuting statements added to a program for documentation
• Use to leave notes for yourself or others
• Include the author, date, and class’s name or function
• Comment out a statement
• Turn it into a comment
• The compiler does not translate, and the JVM does not execute its command
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 40
classroom use.
Adding Comments to a Java Class (2 of 4)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 41
classroom use.
Adding Comments to a Java Class (3 of 4)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 42
classroom use.
Adding Comments to a Java Class (4 of 4)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 43
classroom use.
Creating a Java Application that Produces
GUI Output (1 of 3)
• JOptionPane
• Produces dialog boxes
• Dialog box
• A GUI object resembling a window
• Messages placed for display
• import statement
• Use to access a built-in Java class
• Package
• A group of classes
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 44
classroom use.
Creating a Java Application that Produces
GUI Output (2 of 3)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 45
classroom use.
Creating a Java Application that Produces
GUI Output (3 of 3)
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 46
classroom use.
Finding Help
• Java API
• Also called the Java class library
• Provides prewritten information about Java classes
• FAQs on the Java Web site
• Java Development Kit (JDK)
• A software development kit (SDK) of programming tools
• Free to download
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 47
classroom use.
Don’t Do It
• Don’t forget the file’s name must match the class name
• Don’t confuse these terms:
• 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
• Don’t forget to end every statement with a semicolon
• Do not end class or method headers with a semicolon
• Don’t forgot to recompile when making changes
• Don’t panic when you see a lot of compiler error messages
• Don’t assume your program is perfect when all compiler errors are eliminated.
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 48
classroom use.
Summary (1 of 3)
• Computer program
• A set of instructions that tells a computer what to do
• Object-oriented programs
• Classes
• Objects
• Applications
• Java Virtual Machine (JVM)
• A standardized hypothetical computer
• Everything in a Java program must be part of a class
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 49
classroom use.
Summary (2 of 3)
• Access specifier
• A word that defines circumstances under which a class can be accessed
• All Java applications must have a method named main()
• Program comments
• Nonexecuting statements
• Add to a file for documentation
• javac
• A compile command
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 50
classroom use.
Summary (3 of 3)
• java
• An execute command
• JOptionPane
• A GUI
• Provides methods for creating dialogs
© 2019 Cengage. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for 51
classroom use.