Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 2 Objectives (continued) • Using the javax.swing Package • Programming Style • Common Programming Errors Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 3 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 4 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 5 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 6 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 7 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 8 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 9 Programming Languages (continued) • Java is both: – Compiled – Interpreted • Java Virtual Machine – Software program – Reads bytecodes produced by compiler and executes them Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 10 Programming Languages (continued) Figure 1.2: Translating a Java program Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 11 Procedure and Object Orientations • Procedure-oriented language – Available instructions are used to create selfcontained units • Object-oriented language – Program must first define objects it will be manipulating • Java object-oriented Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 12 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 13 The Development of Java • History: – – – – – – FORTRAN COBOL BASIC Pascal C++ Java Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 14 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 15 The Development of Java (continued) Figure 1.6: The two distinct Java environments Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 16 Objects and Classes • Objects – Part of Java programming language as component types – Can be custom tailored by programmer – Programmer can define custom objects Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 17 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 18 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, Enhanced Edition 19 From Recipe to Class (continued) Figure 1.11: A programming plan for address labels Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 20 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 21 A First Java Class (continued) Figure 1.13: A sample Java class Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 22 A First Java Class (continued) Table 1.1: Java Class Components Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 23 Constructing a Java Program • Programs can use existing classes • Java program: – Considered executable applications program – Class must contain method named main Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 24 The main Method • public static void main(String [] args) • Every program must have main method • Methods begin and end with {} Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 25 The main Method (continued) Figure 1.17: The structure of a main() method Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 26 The main Method (continued) Figure 1.18: The structure of a Java program Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 27 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, Enhanced Edition 28 Reserved Words (continued) Table 1.2: Java Reserved Words Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 29 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 30 Standard Identifiers (continued) Table 1.3: Subset of Java Standard Identifiers Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 31 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 32 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 33 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, Enhanced Edition 34 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 35 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 36 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, Enhanced Edition 37 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, Enhanced Edition 38 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 39 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 40 Using the javax.swing Package (continued) • JOptionPane.showMessageDialog(null, "message", "title", icon-type); Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 41 Using the javax.swing Package (continued) Figure 1.21: showMessageDialog() dialog boxes Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 42 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 43 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, Enhanced Edition 44 Static and Non-Static Methods (continued) • Static – Does not operate on object – Receives all data as arguments – Example: • showMessageDialog() – Syntax: • ClassName.methodName(arguments); Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 45 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, Enhanced Edition 46 Comments • Explanatory remarks made within program • Comment types in Java: – Line – Block • // Line comment • /* Block comment Spans lines */ Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 47 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 48 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 49 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 Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition 50