1 / 41

Java Programming

Java Programming. Goals: Illustrate the basic concepts of programming languages and programs. Gain some experience with a high-level language. Teach some basic programming concepts. Machine Language. Each primitive step is a machine language instruction.

gayora
Télécharger la présentation

Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming Goals: • Illustrate the basic concepts of programming languages and programs. • Gain some experience with a high-level language. • Teach some basic programming concepts.

  2. Machine Language Each primitive step is a machine language instruction. Complicated steps become several primitive instructions. eg. Adding a pair of numbers requires: Move data to a register Move other data to a register Add the contents of one register to the other Move the result to a memory location Machine language is specific to the processor is works for.

  3. Assembly Language Slightly abstracted from machine language. More easily understood than machine language. 00101001 1010100 Becomes LDX $#84 Both mean: “load the register X with the contents of memory location 84.”

  4. High Level Language Programs are usually written in a language closer to natural language (ie. English). Then another program is used to translate the program into machine language. In high level language instead of numbers symbols are used to represent locations in memory. One of the steps in the translation is to convert a symbol to a memory address. Translators: Compiler verses interpreter.

  5. Interpreter vs Compiler Interpreter: line-by-line translation and execution. Compiler: entire translation before execution. Execution is often a separate step.

  6. Java Language • general-purpose • high-level • object-oriented • architectural-neutral • portable • dynamic • supports multiprocessing

  7. Java program • Java platform: Java API (Application Programming Interface) Java VM (Virtual Machine) • Hardware specific platform

  8. Java API • Software components organized into libraries. • Eg. StringBuffer

  9. Types of Programs • Applications Stand alone programs Run from a command line or IDE Include a main method • Applets Run from a HTML document Usually graphical images or interaction with the user Include a paint or run method

  10. Object-oriented Programming • Programs a composed of classes and objects (instances of classes). • Classes include data (fields) and operations (methods).

  11. Inheritance Objects are organized into class relationships. There is a parent-child relationship between related classes. A child class is a subclass of the parent. The child class extends the parent class by adding more data or more operations.

  12. Simple Application Java Syntax import statements class class name { public static void main { main method contents } }

  13. Class Syntax import statements modifiers class class name { any instance variable declarations any method definitions may include constructor definitions }

  14. Method Syntax modifiers return type method name( list of parameters ) { statements } (A constructor is a special method used to create an object.)

  15. Statement Syntax statement;

  16. Block of Statements { statements }

  17. Modifiers Modifiers describe or restrict a class, a method or a field. • public • private • protected • static

  18. Return Type Methods can return a value. The type of the value returned is listed in the header of the method. If the method does not return a value, its return type is void.

  19. Parameters Parameters are a list of variables used to represent information that is passed to a method from a calling program.

  20. Program Style • Meaningful Identifiers • Paragraphing • Comments

  21. Naming Conventions • Classes • Constants • Others

  22. Variables Variables represent locations in memory that store information. Each variable has a data type.

  23. Primitive Data Types • boolean • char • int • double

  24. Declaration Statements type name identifier; int value; boolean isGreater; class name identifier; String name;

  25. Assignment Statements variable = expression; value = 27; isGreater = true;

  26. Instance of a Class class variable = new class name( list of arguments ); account = new Account( name, accountNumber, balance );

  27. Using Object and Class members Static members class name.variable name class name.method name( arguments ) Instance members object name.variable name object name.method name( arguments )

  28. Arguments List of constants or variables in a method call statement. The values are passed to the method when the method is called.

  29. this and super this refers to the object currently being executed. super refers to the parent of the current object.

  30. Imports Import statements tell a program where to find information needed in the program.

  31. Wrapper classes Each primitive class has a corresponding wrapper class. Wrapper classes include methods for converting from one type to another.

  32. Exceptions An event that occurs during execution that makes continued execution impossible. Exceptions are used to catch such an event and do something appropriate.

  33. Input and Output Applications Input and output are in the from of Strings. Applets Input can be button clicks or menu selections, etc. Output can be graphics, etc.

  34. Graphics The Graphics class includes methods to display shapes and text. Eg public drawLine( int x1, int y1, int x2, int y2 ) Draws a line from one point to another, from (x1, y1) to (x2, y2).

  35. Operators Numeric + add - subtract etc. Assignment = assign etc. Boolean && and || or == equals etc.

  36. String operations String operator + concatenate (join) String methods public int length() public int indexOf( String s ) public String( int s, int l ) public boolean equals( String s )

  37. Expressions Use operators to combine constants, values stored in variables and values returned from method calls. Eg count / 2 count < 0 && value – 5 != 0 surname + “Fred”

  38. Precedence of operators Order of evaluation of operators in expressions.

  39. Selection statements Single branch if( boolean expression ) statement or block of statements Two-way branch if( boolean expression ) statement or block of statements else statement or block of statements

  40. Repetition while loop while( boolean expression ) statement or block of statements Counted loop for( initialization; termination; change ) statement or block of statements

  41. Errors • Lexical errors • Syntax errors • Run-time errors • Intent errors

More Related