Download presentation
Presentation is loading. Please wait.
Published byMervin Strickland Modified over 8 years ago
1
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition
2
2 Objectives You should be able to describe: Computer Science and Programming Languages Objects and Classes Constructing a Java Program The PrintStream Class’s print() and println() Methods
3
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition3 Objectives (continued) Using the javax.swing Package Programming Style Common Programming Errors
4
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition4 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 scientific method
5
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition5 Computer Science and Programming Languages (continued) Fundamental areas of computer science: –Introduction to computer architecture –The Java programming language –Class development and design –Algorithm development –Introduction to data structures
6
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition6 Programming Languages Computer program –Self-contained set of instructions and data used to operate computer to produce specific result –Also called software Programming –Process of developing and writing programs Programming language –Set of instructions that can be used to construct program
7
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition7 Programming Languages (continued) Low-level languages –Run on only one type of computer –Machine language Sequence of binary numbers –Assembly language Substitution of words for binary codes of machine language Translated into machine language program before being executed on computer
8
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition8 Programming Languages (continued) High-level languages –Use instructions that resemble natural languages –Run on variety of computer types –Examples: Pascal Visual Basic C C++ Java
9
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition9 Programming Languages (continued) Source program –Programs written in computer language Interpreted language –Each statement translated individually –Executed immediately upon translation Compiled language –Translated as complete unit before any one statement executed
10
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition10 Programming Languages (continued) Java is both: –Compiled –Interpreted Java Virtual Machine –Software program –Reads bytecodes produced by compiler and executes them
11
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition11 Programming Languages (continued) Figure 1.2: Translating a Java program
12
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition12 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 object-oriented
13
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition13 Application and System Software Application software –Programs written to perform particular tasks required by users System software –Collection of programs that must be readily available to computer system for it to operate at all –Operating system
14
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition14 The Development of Java History: –FORTRAN –COBOL –BASIC –Pascal –C++ –Java
15
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition15 The Development of Java (continued) Web browser –Program located and run on user’s computer to display Web pages –Java can run from Web browser Java provides: –Cross-platform compatibility –Write-once-run-anywhere
16
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition16 The Development of Java (continued) Figure 1.6: The two distinct Java environments
17
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition17 Objects and Classes Objects –Part of Java programming language as component types –Can be custom tailored by programmer –Programmer can define custom objects
18
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition18 A Class Is a Plan Structure for class of objects must be created at start of programming process Class –Explicitly written plan –Complete set of parts and instructions needed to create items
19
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition19 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
20
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition20 From Recipe to Class (continued) Figure 1.11: A programming plan for address labels
21
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition21 A First Java Class Class consists of: –Class header line public class nameofclass –Body Class body –Encloses data and methods that make up class –Typically two sections of code: Types of data that will be used Procedures that will be used on data
22
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition22 A First Java Class (continued) Figure 1.13: A sample Java class
23
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition23 A First Java Class (continued) Table 1.1: Java Class Components
24
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition24 Constructing a Java Program Programs can use existing classes Java program: –Considered executable applications program –Class must contain method named main
25
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition25 The main Method public static void main(String [] args) Every program must have main method Methods begin and end with {}
26
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition26 The main Method (continued) Figure 1.17: The structure of a main() method
27
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition27 The main Method (continued) Figure 1.18: The structure of a Java program
28
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition28 Reserved Words Predefined by programming language for special purpose Can only be used in specified manner for intended purpose Also called keywords in Java
29
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition29 Reserved Words (continued) Table 1.2: Java Reserved Words
30
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition30 Standard Identifiers Java-defined words with predefined purpose –Can be redefined by programmer Names of classes and methods provided in Java Good programming practice –Only use standard identifiers for intended purpose
31
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition31 Standard Identifiers (continued) Table 1.3: Subset of Java Standard Identifiers
32
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition32 Identifiers Programmer-supplied words Common practice: –First letter of each word capitalized Starting with second word Case sensitive –identifiers TOTAL, total, and TotaL represent three distinct and different names
33
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition33 Identifiers (continued) Rules for identifiers: –First character of identifier cannot be digit –Only letters, digits, underscores, and dollar signs may follow initial character Blank spaces not allowed –Identifier cannot be reserved word –Maximum number of characters in identifier name unlimited
34
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition34 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
35
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition35 The PrintStream Class’s print() and println() Methods PrintStream class, methods: –print() –println() –Display data to standard output Package –One or more individual classes –Stored in same directory
36
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition36 The PrintStream Class’s print() and println() Methods (continued) General syntax: –objectName.print(data) –System.out.print("Hello World!"); Parameters –Items passed to method through parenthesis –Also called: Arguments Actual arguments
37
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition37 The PrintStream Class’s print() and println() Methods (continued) print() –Prints output only println() –Prints output and appends new line \n –Newline escape character
38
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition38 Java Documentation Sources for documentation: –http//java.sun.com/docs/search.html –Hard copy books: The Java Class Libraries JFC Swing Tutorial
39
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition39 The System Class Provides methods for examining system-related information such as: –Name of operating system –Java version number Supports basic input and output services
40
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition40 Using the javax.swing Package Classes in package provide means of specifying fully functional GUI with typical components such as: –Check boxes –Data entry fields –Command buttons –Dialogs: Modal Modeless
41
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition41 Using the javax.swing Package (continued) JOptionPane.showMessageDialog(null, "message", "title", icon-type);
42
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition42 Using the javax.swing Package (continued) Figure 1.21: showMessageDialog() dialog boxes
43
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition43 Using the javax.swing Package (continued) Import statement –Found at beginning of program after package declaration –Compiler searches for classes in packages listed for import
44
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition44 Static and Non-Static Methods Non-static –Must be used with objects –Examples: println() displayMessage() –Syntax: objectName.methodName(arguments);
45
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition45 Static and Non-Static Methods (continued) Static –Does not operate on object –Receives all data as arguments –Example: showMessageDialog() –Syntax: ClassName.methodName(arguments);
46
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition46 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; }
47
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition47 Comments Explanatory remarks made within program Comment types in Java: –Line –Block // Line comment /* Block comment Spans lines */
48
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition48 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 program –Omitting semicolon at end of each statement –Forgetting \n to indicate new line
49
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition49 Summary Programming languages come in variety of forms and types In object-oriented languages, basic program unit is a class –Java is object-oriented All Java classes use basic structure consisting of: –Class header line –Class body
50
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition50 Summary (continued) Java program must have main() method PrintStream class provides two methods print() and println() –Used to display text and numerical results Java package consists of one or more individual classes stored in same directory –javax.swing package provides GUI classes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.