380 likes | 563 Views
Introduction to java. Learning About Programming . Program Set of written instructions that tells computer what to do Machine language Most basic circuitry-level language Low-level programming language High-level programming language
E N D
Learning About Programming • Program • Set of written instructions that tells computer what to do • Machine language • Most basic circuitry-level language • Low-level programming language • High-level programming language • Allows you to use vocabulary of reasonable terms(read, write, add, etc) • Syntax • Rules of the language
Program statements (aka commands) • Similar to English sentences • Carry out tasks of program • Compiler or interpreter • Translates language statements into machine code • Compiler translate the entire program before carrying out the statement (called executing) • Interpreter translate one program statement at a time, executing it as soon as it is translated • Debugging • Freeing program of all errors
Syntax error • Misuse of language • Misspelled programming language word • Logic errors • Also called semantic errors • Incorrect order or procedure
Procedural programming • Sets of operations executed in sequence • Variables • Named computer memory locations that hold values • Procedures • Individual operations grouped into logical units • Object-oriented programs • Create classes • Create objects from classes • Create applications that use those objects
Object-oriented programming was used most frequently for two major types of applications • Computer simulations • Graphical user interfaces (GUIs) • Not all object-orientated programs written to use GUI • Object-oriented programming differs from traditional procedural programming • Basic concepts • Polymorphism • Inheritance • Encapsulation
Understanding Classes, Objects, and Encapsulation • Class • Describes a group or collection of objects with common properties • Class definition • describes what attributes its objects will have & what those objects will be able to do • Instance • Class Attributes • Characteristics that define an object • They are the properties of the object • The values in its properties differentiate objects of the same class • The values of the properties is called the object’s state
Class Method • Self-contained block of program code that carries out some action
Objects • Specific, concrete instance of a class • When you create an object instance, you instantiate it • In OOP grammer: • object = noun • Attribute = adjective • Methods = verbs
Example • Class - Automoblie • Class definition - describes what Automobile objects are like • Automobile attributes – make, model, year, color • Each automobile object possesses the same attributes, but not the same values for those attributes • Automobile methods – moveForward, moveBackward. carFill, carWash
Encapsulation • The hiding of data and methods within an object • Keeps data and methods safe from inadvertent changes • Sometimes referred to as “black box” • a programmer can access & use the methods & data in the black box but can’t change them
Understanding Inheritance and Polymorphism • Inheritance • Important feature of object-oriented programs • Ability to create classes that share the attributes and methods of existing classes but with more specific features • Helps you understand real-world objects • Polymorphism • Means “many forms” • Allows the same word or symbol to be interpreted correctly in different situations based on the context
Features of the Java Programming Language • Java • Developed by Sun Microsystems • Object-oriented language • General-purpose • Advantages • Security features • Architecturally neutral
Bytecode • Statements saved in file • Java compiler converts source code into binary program consisting of this • Java interpreter • Checks bytecode and communicates with operating system • Executes bytecode instructions line by line within Java virtual machine
Java Program Types • Applets • Programs embedded in Web page • Java applications • Called Java stand-alone programs • Console applications • Support character output • Easiest to create • Windowed applications • Creates a GUI w/elements such as: • Menus • Toolbars • Dialog boxes Java Programming, Sixth Edition
Understanding the Statementthat Produces the Output • Literal string • Will appear in output exactly as entered • Written between double quotation marks • Cannot be broken and put on multiple lines • Arguments • Pieces of information passed to a method • Method • Usually requires the information to perform its task or carry out its purpose • Systemclass • Refers to the standard input/output device for a system
Print() vsprintln() • print() – insertion point remains on the same line as the output • println() – once the message is displayed, the insertion point appears on the following line
Understanding a Class • Everything used within a Java program must be part of a class • Define Java class using any name or identifier • Requirements for identifiers • Must begin with: • A letter of the English alphabet • Anon-English letter (such as α or π) • An underscore • A dollar sign • Cannot begin with a digit
Understanding a Class (cont'd.) • Can only contain: • Letters • Digits • Underscores • Dollar signs • Cannot be a Java reserved keyword • Cannot be true, false, or null • Access specifier • Defines how a class can be accessed & the other classes that have the right to use a class
Program Style • For every opening curly brace ( { ) in a Java program, there must be a corresponding closing curly brace ( } ) • Placement of the opening and closing curly braces is not important to the compiler • Whitespace: • Optional • Used to organize your program code and make it easier to read • Cannot use it within any identifier or keyword
Understanding the main() Method Static means this method works without instantiating an object of the cass.
Understanding the main() Method • static • Reserved keyword • Means a method is accessible & usable even though no objects of the class exist • void • Use in main() method header • Does not indicate main()method is empty • Indicates main() method does not return any value when called • Doesn’t mean main()doesn’t produce output
Adding Comments to a Java Class • Program comments • Nonexecuting statements added to a program for documentation • Used to leave notes for yourself or others • Should include the author, the date, the class name or function, and a brief description of the purpose of each method you create • Comment out a statement • Turn statement into a comment • Compiler does not translate, and the JVM does not execute its command
Adding Comments to a Java Class (cont'd.) • Types of Java comments • Line comments • Start with two forward slashes (//) • Continues to the end of the current line • Does not require an ending symbol • Can appear on a line by itself or at the end (and to the right) of a line • Block comments • Start with forward slash and asterisk (/*) • End with asterisk and forward slash (*/) • Can appear on a line by itself, before code, or after code
Adding Comments to a Java Class (cont'd.) • Types of Java comments (cont'd.) • Javadoc comments • Special case of block comments • Begin with slash and two asterisks (/**) • End with asterisk and forward slash (*/) • Used to generate documentation
Saving, Compiling, Running, and Modifying a Java Application • Saving a Java class • Save the class in a file with exactly the same name and .java extension • For public classes • Class name and filename must match exactly • Compiling a Java class • Compile source code into bytecode • Translate bytecode into executable statements
Correcting Errors andFinding Help • First line of error message displays: • Name of file where error found • Line number • Nature of error • Next lines identify: • Symbol • Location • Compile-time error • Compiler detects violation of language rules • Refuses to translate class to machine code
Correcting Errors andFinding Help (cont'd.) • Parsing • Process compiler uses to divide source code into meaningful portions • Logic error • Syntax correct but produces incorrect results when executed • Usually more difficult to find and resolve • Java API • Also called the Java class library • Prewritten Java classes
Don’t Do It • File’s name must match name of class • Don’t confuse these terms: • Parentheses, braces, brackets, curly braces, square brackets, and angle brackets • Don’t forget to end a block comment • Don’t forget that Java is case sensitive • End every statement with semicolon • Do not end class or method headers with semicolon • Recompile when making changes
Summary • Computer program • Set of instructions that tells a computer what to do • Object-oriented programs • Classes • Objects • Applications • Java virtual machine (JVM) • Standardized hypothetical computer • Everything in a Java program must be part of a class
Summary (cont'd.) • Access specifier • Word that defines circumstances under which class can be accessed • All Java applications must have method named main() • Program comments • Nonexecuting statements • Add to file for documentation • javac • Compile command
Summary (cont'd.) • java • Execute command • JOptionPane • GUI • Provides methods for creating dialogs