+ All Categories
Home > Documents > Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Date post: 15-Jan-2016
Category:
Upload: toby-roland-craig
View: 220 times
Download: 0 times
Share this document with a friend
Popular Tags:
36
Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class- Centered Approach
Transcript
Page 1: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Chapter 1: Introduction

Object-Oriented Program Development Using Java:

A Class-Centered Approach

Page 2: 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

Page 3: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Object-Oriented Program Development Using Java: A Class-Centered Approach 3

Programming Languages (continued)

• Low-level languages:

– Machine language

– Assembly language

Page 4: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 5: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 6: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 7: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Object-Oriented Program Development Using Java: A Class-Centered Approach 7

Page 8: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 9: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Object-Oriented Program Development Using Java: A Class-Centered Approach 9

The Development of Java

• History:

– Fortran

– COBOL

– BASIC

– Pascal

– C++

– Java

Page 10: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 11: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Object-Oriented Program Development Using Java: A Class-Centered Approach 11

Page 12: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 13: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 14: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 15: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Object-Oriented Program Development Using Java: A Class-Centered Approach 15

Page 16: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 17: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Object-Oriented Program Development Using Java: A Class-Centered Approach 17

Page 18: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Object-Oriented Program Development Using Java: A Class-Centered Approach 18

Page 19: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 20: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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 {}

Page 21: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Object-Oriented Program Development Using Java: A Class-Centered Approach 21

Page 22: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 23: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Object-Oriented Program Development Using Java: A Class-Centered Approach 23

Page 24: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 25: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

Object-Oriented Program Development Using Java: A Class-Centered Approach 25

Page 26: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 27: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 28: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 29: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 30: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 31: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 32: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 33: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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

Page 34: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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;

}

Page 35: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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 */

Page 36: Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach.

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


Recommended