Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 46

Chapter 1:

Introduction
Object-Oriented Program
Development Using Java:
A Class-Centered
Approach
Objectives
• Computer Science and Programming Languages
• Objects and Classes
• Constructing a Java Program
• The PrintStream Class’s print() and println()
Methods
• Using the javax.swing Package
• Programming Style
• Common Programming Errors

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


Computer Science and
Programming Languages
• Information age
– Almost totally dependent and driven by information
technology
• Computer science
– Science of computers and computing
• Computer scientists
– Solve problems using the scientific method

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


Fundamental Areas of
Computer Science
This text will focus on:
• An introduction to computer architecture
• The Java programming language
• Class development and design
• Algorithm development
• An introduction to data structures

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


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 5


Programming Languages
(continued)
• Low-level languages:

– Machine language

– Assembly language

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


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 7


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 8


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 9


Object-Oriented Program Development Using Java: A Class-Centered Approach 10
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 11


The Development of Java
• History:
– Fortran
– COBOL
– BASIC
– Pascal
– C++
– Java

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


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 13


Object-Oriented Program Development Using Java: A Class-Centered Approach 14
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 15


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 16


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 17


Object-Oriented Program Development Using Java: A Class-Centered Approach 18
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 19


Object-Oriented Program Development Using Java: A Class-Centered Approach 20
Object-Oriented Program Development Using Java: A Class-Centered Approach 21
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 22


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 23


Object-Oriented Program Development Using Java: A Class-Centered Approach 24
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 25


Object-Oriented Program Development Using Java: A Class-Centered Approach 26
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 27


Object-Oriented Program Development Using Java: A Class-Centered Approach 28
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 29


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 30


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 31


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 32


The PrintStream Class’s
print()
and println() Methods
• General syntax:
(continued)
– 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 33


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 34


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 35


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 36


Using the javax.swing Package
• Classes in the package provide the means of
specifying fully functional GUIs with typical
components such as:
– Check boxes
– Data entry fields
– Command buttons
– Dialogs:
• Modal
• Modeless

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


• JOptionPane.showMessageDialog(null,"message","title",ic
on-type);

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


Using the javax.swing Package
(continued)
• Import statement

– Found at the beginning of a program after the package


declaration

– The compiler searches for classes in packages listed for


import

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


Static and non-static Methods
• Non-static
– Must be used with objects

– Examples:
• println()

• displayMessage()

– Syntax:
• objectName.methodName(arguments);

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


Static and non-static Methods
(continued)
• Static
– Does not operate on objects
– Receives all of its data as arguments
– Example:
• showMessageDialog()

– Syntax:
• ClassName.methodName(arguments);

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


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 42


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 43


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

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


Summary
• Programming languages come in a variety of
forms and types
• In object-oriented languages, the basic program
unit is a class
– Java is object-oriented
• All Java classes use basic structure consisting of:
– Class header line
– Class body

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


Summary (continued)
• A Java program must have the main() method

• The PrintStream class provides two methods, print()


and println()
– Used to display text and numerical results

• A Java package consists of one or more individual


classes stored in the same directory
– javax.swing package provides GUI classes

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

You might also like