0% found this document useful (0 votes)
38 views48 pages

Java Programming Sixth Edition Chapter 1: Creating Your First Java Classes

This document provides an overview of key concepts for creating Java classes, including: - Defining basic programming terminology like programs, machine language, high-level languages, compilers, and debugging. - Comparing procedural and object-oriented programming concepts like classes, objects, encapsulation, inheritance, and polymorphism. - Describing features of the Java programming language like security, platform independence, bytecode, and applets vs applications. - Analyzing a simple Java application that produces console output using the println() method. - Explaining how to add comments to a Java class using line comments, block comments, and Javadoc comments.

Uploaded by

Dzudzi Manyuha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views48 pages

Java Programming Sixth Edition Chapter 1: Creating Your First Java Classes

This document provides an overview of key concepts for creating Java classes, including: - Defining basic programming terminology like programs, machine language, high-level languages, compilers, and debugging. - Comparing procedural and object-oriented programming concepts like classes, objects, encapsulation, inheritance, and polymorphism. - Describing features of the Java programming language like security, platform independence, bytecode, and applets vs applications. - Analyzing a simple Java application that produces console output using the println() method. - Explaining how to add comments to a Java class using line comments, block comments, and Javadoc comments.

Uploaded by

Dzudzi Manyuha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 48

Java Programming

Sixth Edition
Chapter 1: Creating Your
First Java Classes
Objectives
• Define basic programming terminology
• Describe procedural and object-oriented
programming concepts
• Describe the features of the Java programming
language
• Analyze a Java application that uses console
output

Java Programming, Sixth Edition 2


Objectives (cont'd.)
• Add comments to a Java class
• Save, compile, run, and modify a Java application
• Create a Java application that produces GUI output
• Correct errors and find help

Java Programming, Sixth Edition 3


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

Java Programming, Sixth Edition 4


Learning About Programming (cont'd.)
• 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

Java Programming, Sixth Edition 5


Learning About Programming (cont'd.)
• 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
Java Programming, Sixth Edition 6
Comparing Procedural and Object-
Oriented Programming 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
Java Programming, Sixth Edition 7
Comparing Procedural and Object-
Oriented Programming Concepts
(cont'd.)
• Object-oriented programming was used most
frequently for two major types of applications
– Computer simulations
– Graphical user interfaces (GUIs)
• Not all object-orientated programs written to use GUI
• Object-oriented programming differs from traditional
procedural programming
– Basic concepts
• Polymorphism
• Inheritance
• Encapsulation
Java Programming, Sixth Edition 8
Understanding Classes, Objects, and
Encapsulation
• Class
– Describes objects with common properties
– Definition
– Instance
• Attributes
– Characteristics that define object
– Differentiate objects of same class
– Value of attributes is object’s state
• Objects
– Specific, concrete instance of a class
Java Programming, Sixth Edition 9
Understanding Classes, Objects, and
Encapsulation (cont'd.)

Java Programming, Sixth Edition 10


Understanding Classes, Objects, and
Encapsulation (cont'd.)
• Method
– Self-contained block of program code
– Similar to procedure
• Encapsulation
– Refers to hiding of data and methods within object
– Provides security
– Keeps data and methods safe from inadvertent
changes

Java Programming, Sixth Edition 11


Understanding Inheritance and
Polymorphism
• Inheritance
– 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 same word to be interpreted correctly in
different situations based on context

Java Programming, Sixth Edition 12


Features of the Java Programming
Language
• Java
– Developed by Sun Microsystems
– Object-oriented language
– General-purpose
– Advantages
• Security features
• Architecturally neutral

Java Programming, Sixth Edition 13


Features of the Java Programming
Language (cont'd.)
• Java (cont'd.)
– 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 language

Java Programming, Sixth Edition 14


Features of the Java Programming
Language (cont'd.)
• 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

Java Programming, Sixth Edition 15


Features of the Java Programming
Language (cont'd.)

Java Programming, Sixth Edition 16


Java Program Types
• Applets
– Programs embedded in Web page
• Java applications
– Called Java stand-alone programs
– Console applications
• Support character output
– Windowed applications
• Menus
• Toolbars
• Dialog boxes

Java Programming, Sixth Edition 17


Analyzing a Java Application that
Produces Console Output
• Even simplest Java application involves fair
amount of confusing syntax
• Print “First Java application” on screen

Java Programming, Sixth Edition 18


Analyzing a Java Application that
Produces Console Output (cont'd.)

Java Programming, Sixth Edition 19


Understanding the Statement
that Produces the Output
• Literal string
– Will appear in output exactly as entered
– Written between double quotation marks
• Arguments
– Pieces of information passed to method
• Method
– Requires information to perform its task
• System class
– Refers to the standard output device for a system

Java Programming, Sixth Edition 20


Understanding the Statement
that Produces the Output (cont'd.)

Java Programming, Sixth Edition 21


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 π)
• Underscore
• Dollar sign
– Cannot begin with digit

Java Programming, Sixth Edition 22


Understanding the First Class
(cont'd.)
• Requirements for identifiers
– Can only contain:
• Letters
• Digits
• Underscores
• Dollar signs
– Cannot be Java reserved keyword
– Cannot be true, false, or null
• Access specifier
– Defines how class can be accessed

Java Programming, Sixth Edition 23


Understanding the First Class
(cont'd.)

Java Programming, Sixth Edition 24


Understanding the First Class
(cont'd.)

Java Programming, Sixth Edition 25


Understanding the First Class
(cont'd.)

Java Programming, Sixth Edition 26


Understanding the First Class
(cont'd.)

Java Programming, Sixth Edition 27


Indent Style
• For every opening curly brace ( { ) in a Java
program, there must be a corresponding closing
curly brace ( } )
• Placement of the opening and closing curly braces
is not important to the compiler

Java Programming, Sixth Edition 28


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

Java Programming, Sixth Edition 29


Understanding the main() Method

Java Programming, Sixth Edition 30


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
– Turn it into a comment
– Compiler does not translate, and the JVM does not
execute its command

Java Programming, Sixth Edition 31


Adding Comments to a Java Class
(cont'd.)
• 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 (*/)

Java Programming, Sixth Edition 32


Adding Comments to a Java Class
(cont'd.)
• Types of Java comments (cont'd.)
– Javadoc comments
• Special case of block comments
• Begin with slash and two asterisks (/**)
• End with asterisk and forward slash (*/)
• Use to generate documentation

Java Programming, Sixth Edition 33


Adding Comments to a Java Class
(cont'd.)

Java Programming, Sixth Edition 34


Saving, Compiling, Running, and
Modifying a Java Application
• Saving a Java class
– Save class in file with exactly same name and .java
extension
• For public classes
• Class name and filename must match exactly
• Compiling a Java class
– Compile source code into bytecode
– Translate bytecode into executable statements
• Using Java interpreter
– Type javac First.java

Java Programming, Sixth Edition 35


Saving, Compiling, Running, and
Modifying a Java Application (cont'd.)
• Compilation outcomes
– javac unrecognized command
– Program language error messages
– No messages indicating successful completion
• Reasons for error messages
– Misspelled command javac
– Misspelled filename
– Not within correct subfolder or subdirectory on
command line
– Java not installed properly
Java Programming, Sixth Edition 36
Running a Java Application
• Run application from command line
– Type java First
• Shows application’s output in command window
• Class stored in folder named Java on C drive

Java Programming, Sixth Edition 37


Running a Java Application (cont'd.)

Java Programming, Sixth Edition 38


Modifying a Java Class
• Modify text file that contains existing class
• Save file with changes
– Using same filename
• Compile class with javac command
• Interpret class bytecode and execute class using
java command

Java Programming, Sixth Edition 39


Creating a Java Application
that Produces GUI Output
• JOptionPane
– Produces dialog boxes
• Dialog box
– GUI object resembling window
– Messages placed for display
• import statement
– Use to access built-in Java class
• Package
– Group of classes

Java Programming, Sixth Edition 40


Creating a Java Application
that Produces GUI Output (cont'd.)

Java Programming, Sixth Edition 41


Correcting Errors and
Finding Help
• First line of error message displays:
– Name of file where error found
– Line number
– Nature of error
• Next lines identify:
– Symbol
– Location
• Compile-time error
– Compiler detects violation of language rules
– Refuses to translate class to machine code
Java Programming, Sixth Edition 42
Correcting Errors and
Finding Help (cont'd.)
• 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
Java Programming, Sixth Edition 43
You Do It
• Your first application
• Adding comments to a class
• Modifying a class
• Creating a dialog box

Java Programming, Sixth Edition 44


Don’t Do It
• File’s name must match name of class
• 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
• End every statement with semicolon
– Do not end class or method headers with semicolon
• Recompile when making changes

Java Programming, Sixth Edition 45


Summary
• Computer program
– Set of instructions that tells a computer what to do
• Object-oriented programs
– Classes
– Objects
– Applications
• Java virtual machine (JVM)
– Standardized hypothetical computer
• Everything in a Java program must be part of a
class
Java Programming, Sixth Edition 46
Summary (cont'd.)
• Access specifier
– Word that defines circumstances under which class
can be accessed
• All Java applications must have method named
main()
• Program comments
– Nonexecuting statements
– Add to file for documentation
• javac
– Compile command
Java Programming, Sixth Edition 47
Summary (cont'd.)
• java
– Execute command
• JOptionPane
– GUI
– Provides methods for creating dialogs

Java Programming, Sixth Edition 48

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy