Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.
Post on 15-Jan-2016
219 Views
Preview:
Transcript
Chapter 1: Introduction
Object-Oriented Program Development Using Java:
A Class-Centered Approach
Object-Oriented Program Development Using Java: A Class-Centered Approach 2
Programming Languages
• Computer program
– A self-contained set of instructions and data used to operate a computer to produce specific results
– Also called software
• Programming is the process of developing and writing a program
• A programming language is a set of instructions that can be used to construct a program
Object-Oriented Program Development Using Java: A Class-Centered Approach 3
Programming Languages (continued)
• Low-level languages:
– Machine language
– Assembly language
Object-Oriented Program Development Using Java: A Class-Centered Approach 4
Programming Languages (continued)
• High-level languages:– Use instructions that resemble natural languages
– Can be run on a variety of computer types
– Examples:
• Pascal
• Visual Basic
• C
• C++
• Java
Object-Oriented Program Development Using Java: A Class-Centered Approach 5
Programming Languages (continued)
• Source program
– Programs written in a computer language
• Interpreted language
– Each statement is translated individually and executed immediately upon translation
• Compiled language
– All statements are translated as a complete unit before any one statement is executed
Object-Oriented Program Development Using Java: A Class-Centered Approach 6
Programming Languages (continued)
• Java is both:
– Compiled
– Interpreted
• Java Virtual Machine
– Software program that can read bytecode produced by the compiler and execute it
Object-Oriented Program Development Using Java: A Class-Centered Approach 7
Object-Oriented Program Development Using Java: A Class-Centered Approach 8
Procedure and Object Orientations
• Procedure-oriented language
– Available instructions are used to create self-contained units
• Object-oriented language
– Program must first define objects it will be manipulating
• Java is object-oriented
Object-Oriented Program Development Using Java: A Class-Centered Approach 9
The Development of Java
• History:
– Fortran
– COBOL
– BASIC
– Pascal
– C++
– Java
Object-Oriented Program Development Using Java: A Class-Centered Approach 10
The Development of Java (continued)
• Web browser
– A program located and run on a user’s computer to display Web pages
– Java can run from a Web browser
• Java provides:
– Cross-platform compatibility
– Write-once-run-anywhere capability
Object-Oriented Program Development Using Java: A Class-Centered Approach 11
Object-Oriented Program Development Using Java: A Class-Centered Approach 12
Objects and Classes
• Objects
– Part of the Java programming language as component types
– Can be custom tailored by programmers
– Programmers can define custom objects
Object-Oriented Program Development Using Java: A Class-Centered Approach 13
A Class Is a Plan
• The structure for a class of objects must be created at the start of the programming process
• Class
– Explicitly written plan
– Complete set of parts and instructions needed to create items
Object-Oriented Program Development Using Java: A Class-Centered Approach 14
From Recipe to Class
• Data declaration section
– Description of data to be used
• Methods section
– Defines how to combine data components to produce desired result
Object-Oriented Program Development Using Java: A Class-Centered Approach 15
Object-Oriented Program Development Using Java: A Class-Centered Approach 16
A First Java Class
• A class consists of a class header line and a body
• The class header line includes the words public class nameofclass
• Class body
– Encloses data and methods that make up class
– Typically two sections of code:
• The types of data that will be used
• Procedures that will be used on the data
Object-Oriented Program Development Using Java: A Class-Centered Approach 17
Object-Oriented Program Development Using Java: A Class-Centered Approach 18
Object-Oriented Program Development Using Java: A Class-Centered Approach 19
Constructing a Java Program
• Programs can use existing classes
• A Java program is:
– Considered an executable applications program
– A class that must contain a method named main
Object-Oriented Program Development Using Java: A Class-Centered Approach 20
The main Method
• public static void main(String [] args)
• Every program must have the main method
• Methods begin and end with {}
Object-Oriented Program Development Using Java: A Class-Centered Approach 21
Object-Oriented Program Development Using Java: A Class-Centered Approach 22
Reserved Words
• Predefined by programming language for special purpose
• Can only be used in specified manner for intended purpose
• Also called keywords in Java
Object-Oriented Program Development Using Java: A Class-Centered Approach 23
Object-Oriented Program Development Using Java: A Class-Centered Approach 24
Standard Identifiers
• Java-defined words that have predefined purpose
– Can be redefined by a programmer
• Names of classes and methods provided in Java
• A good programming practice is to only use standard identifiers for their intended purpose
Object-Oriented Program Development Using Java: A Class-Centered Approach 25
Object-Oriented Program Development Using Java: A Class-Centered Approach 26
Identifiers• Programmer-supplied words
• Can be made up of any combination of:– Letters
– Digits
– Underscores (_)
– Dollar signs ($)
• Common practice:– The first letter of each word, starting with the second
word in a multiword identifier, is capitalized
Object-Oriented Program Development Using Java: A Class-Centered Approach 27
Rules for Identifiers in Java
• The first character of the identifier cannot be a digit
• Only letters, digits, underscores, and dollar signs may follow the initial character
– Blank spaces are not allowed
• Identifiers cannot be reserved words
• Maximum number of characters in the identifier name is unlimited
Object-Oriented Program Development Using Java: A Class-Centered Approach 28
What Is Syntax?
• Set of rules for formulating grammatically correct language statements
• Program has proper form specified for compiler
• Individual statement or program can be syntactically correct and still be logically incorrect
Object-Oriented Program Development Using Java: A Class-Centered Approach 29
The PrintStream Class’s print()
and println() Methods• print() and println() are in the PrintStream class
and are print methods:
– Display data to standard output
• Package
– One or more individual classes stored in the same directory
Object-Oriented Program Development Using Java: A Class-Centered Approach 30
The PrintStream Class’s print()
and println() Methods (continued)
• General syntax:
– objectName.print(data)
– System.out.print("Hello World!");
• Parameters
– Items are passed to a method through parentheses
– Also called
• Arguments
• Actual arguments
Object-Oriented Program Development Using Java: A Class-Centered Approach 31
The PrintStream Class’s print()
and println() Methods (continued)
• print()
– Prints output only
• println()
– Prints output and appends new line
• \n
– Newline escape character
Object-Oriented Program Development Using Java: A Class-Centered Approach 32
Java Documentation
• Sources for documentation:
– http//java.sun.com/docs/search.html
– Hard copy books:
• The Java Class Libraries
• JFC Swing Tutorial
Object-Oriented Program Development Using Java: A Class-Centered Approach 33
The System Class
• Provides methods for examining system-related information, such as:
– Name of the operating system
– Java version number
• Supports basic input and output services
Object-Oriented Program Development Using Java: A Class-Centered Approach 34
Programming Style
• Java ignores whitespace• Proper programming style:
– Makes programs easy to read
– Minimizes mistakes
• Proper style for main method:public static void main(String[] args)
{
program statements in here;
}
Object-Oriented Program Development Using Java: A Class-Centered Approach 35
Comments
• Explanatory remarks made within a program
• Comment types in Java:
– Line
– Block
• // Line comment
• /* Block comment
Spans lines */
Object-Oriented Program Development Using Java: A Class-Centered Approach 36
Common Programming Errors
• Knowing about common errors helps programmers avoid them
• Most common errors:
– Forgetting to save program with same file name as class name used within the program
– Omitting a semicolon at the end of each statement
– Forgetting \n to indicate a new line
top related