1 / 26

Lecture 2 Software Concepts

Lecture 2 Software Concepts. Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology. Outline. Structure of Java standalone applications Basic program elements Executing a program Helpful support for writing software

Download Presentation

Lecture 2 Software Concepts

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. Lecture 2Software Concepts Instructors: Fu-Chiung Cheng (鄭福炯) Associate Professor Computer Science & Engineering Tatung Institute of Technology

  2. Outline • Structure of Java standalone applications • Basic program elements • Executing a program • Helpful support for writing software • Java applets

  3. Java Program Structure • A program is made up of one or more classes • A class contains one or more methods • A method contains program statements • A Java application always executes the main method • class Lincoln { • public static void main (String[] args) { • System.out.println ("Whatever you are, be a good one."); • } // method main • } // class Lincoln

  4. Java Program StructureWhite Space • white space: Spaces, blank lines, and tabs. • White space is used to separate words and symbols (tokens) in a program • Extra white space is ignored. • A valid Java program can be formatted many different ways: • class Lincoln2 { public static void main (String[] args) • { System.out.println ("Whatever you are, be a good one."); } } • Readability: consistent indentation

  5. Java Program Structure:Comments • Comments: same syntax as C/C++. • One-line comment: // • // comment runs to the end of the line • Multiple-line comment: • /* comment runs to terminating • * symbol, even across line breaks • */

  6. Java Program Structure:Indentifiers • Identifiers: • class name, method name, variables, key words. • Most identifiers have no predefined meaning except as specified by the programmer • An identifier can be made up of letters, digits, the underscore character (_), and the dollar sign • They cannot begin with a digit • Java is case sensitive, therefore Total and total are different identifiers

  7. Java Program Structure:Indentifiers • Identifiers in Lincoln.java: • class name: Lincoln • method name: main, System.out.println • variables: args • key words: String

  8. Good Programming Practice • Naming Conventions: • Classes: first word capitalized • Methods and variables: first word lower case • Intermediate words capitalized • Words run together, no underscores • class IntegerList (X Integer_List) • int listItem (X list_Item) • Constant: all caps with underscores to separate • words • MAX_INTEGER_ARRAY

  9. Java Program Structure:Reserved words • reserved words have specific meanings in Java and cannot be used in other ways abstract boolean break byte byvalue case cast catch char class const continue default do double else extends false final finally float for future generic goto if implements import inner instanceof int interface long native new null operator outer package private protected public rest return short static super switch synchronized this throw throws transient true try var void volatile while

  10. Java Program Structure:Literals • Integer literals: • 25 69 -4288 • Floating point literals: • 3.14159 42.075 -0.5 • String literals: • "The result is: " • "To thine own self be true."

  11. Java Program StructureJava API • The Java Application Programmer Interface (API) is a collection of classes that can be used as needed • Java API: print and println; • Java APIs are not part of the Java language itself. java.applet java.awt java.beans java.io java.lang java.math java.net java.rmi java.security java.sql java.text java.util

  12. Java Program Structure • Operator overloading (+ ) • String concatenation: String + String • String concatenation: String + numeric data • addition: numeric data + numeric data • class Sum { • public static void main (String[] args) { • System.out.println ("6 + 9 = " + (6+9)); • System.out.print (" Java " + " Programming"); • System.out.println ("for Antarctica is " + 672); • } // method main • } // class Sum

  13. Program Languages • Traditional programming languages: • A. high-level languages ==> machine languages • B. Each CPU has its own specific machine language • The Java compiler translates Java source code into • a special representation called bytecode • Java bytecode is not the machine language for any • traditional CPU • Java interpreter translates bytecode into machine • language and executes it.

  14. Java Translation and Execution Standalone applications: Java source code Java bytecode Java compiler Java interpreter Bytecode compiler Machine code

  15. Java Translation and Execution local computer Java compiler Java source code Java bytecode Applet Web browser remote computer Java interpreter

  16. Java Translation and Execution • compiling Java programs into bytecodes: • > javac Lincoln.java • The bytecode of Lincoln.java is Lincoln.class • Java interpreter (Java Virtual Machines): • > java Lincoln

  17. Errors: • A program can have three types of errors: • A. compile-time errors: • syntax errors. • B. run-time errors: • divide by zero • C. logical errors: • incorrect results.

  18. Object-Oriented Programming: • Everything is an object. • Programs are made from objects • Each object has its own memory made up of • other objects. • Objects communication: send messages (requests). • An object contains data and methods • An object is defined by a class (type) • Multiple objects can be created from the same class

  19. My first car John's car Dad's car Object-Oriented Programming: • A class (type) represents a concept and • An object (instance) represents the realization of that concept. Objects Class Car

  20. Object-Oriented Programming: • An object contains data and methods. • Composition: (reuse objects): • Ex, A car has a engine. Car Class Methods Data drive(); start(); etc... engine wheel[4] door[2] etc...

  21. Object-Oriented Programming: • Objects communication: send messages. Object1 message Object2 message message Object3

  22. Object-Oriented Programming: • Inheritance: reuse the interface. Shape draw(); erase(); Circle Line Square draw(); erase(); draw(); erase(); draw(); erase();

  23. Object-Oriented Programming: • Polymorphism (dynamic binding). • shape.draw() will call the right draw function of circle • line or square. Circle draw(); erase(); Shape Line Square

  24. Dynamic Binding • void doStuff(Shape s) { • s.erase(); • // ... • s.draw(); • } • // ... • Circle c = new Circle(); • Triangle t = new Triangle(); • Line l = new Line(); • doStuff(c); • doStuff(t); • doStuff(l);

  25. Importing Packages • Using a class from the Java API: fully qualified name: • java.lang.System.out.println(); • import statement:: • import java.applet.*; • import java.util.Random; • …. • Random coin = new Random(); • The java.lang package: automatically imported • into every Java program

  26. Conclusions • Simple Structure of Java: classes • Object-oriented concepts • Import statement 完成 Lecture 2 休息十分鐘!

More Related