360 likes | 430 Views
Learn about programming languages, class development, Java history, Java structure, and creating Java classes for beginners.
E N D
Chapter 1: Introduction Object-Oriented Program Development Using Java: A Class-Centered Approach
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
Programming Languages (continued) • Low-level languages: • Machine language • Assembly language Object-Oriented Program Development Using Java: A Class-Centered Approach
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
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
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
Object-Oriented Program Development Using Java: A Class-Centered Approach
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
The Development of Java • History: • Fortran • COBOL • BASIC • Pascal • C++ • Java Object-Oriented Program Development Using Java: A Class-Centered Approach
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
Object-Oriented Program Development Using Java: A Class-Centered Approach
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
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
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
Object-Oriented Program Development Using Java: A Class-Centered Approach
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
Object-Oriented Program Development Using Java: A Class-Centered Approach
Object-Oriented Program Development Using Java: A Class-Centered Approach
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
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
Object-Oriented Program Development Using Java: A Class-Centered Approach
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
Object-Oriented Program Development Using Java: A Class-Centered Approach
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
Object-Oriented Program Development Using Java: A Class-Centered Approach
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
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
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
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
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
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
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
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
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
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
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