260 likes | 380 Views
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
E N D
Lecture 2Software 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 • Java applets
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
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
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 • */
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
Java Program Structure:Indentifiers • Identifiers in Lincoln.java: • class name: Lincoln • method name: main, System.out.println • variables: args • key words: String
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
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
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."
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
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
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.
Java Translation and Execution Standalone applications: Java source code Java bytecode Java compiler Java interpreter Bytecode compiler Machine code
Java Translation and Execution local computer Java compiler Java source code Java bytecode Applet Web browser remote computer Java interpreter
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
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.
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
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
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...
Object-Oriented Programming: • Objects communication: send messages. Object1 message Object2 message message Object3
Object-Oriented Programming: • Inheritance: reuse the interface. Shape draw(); erase(); Circle Line Square draw(); erase(); draw(); erase(); draw(); erase();
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
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);
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
Conclusions • Simple Structure of Java: classes • Object-oriented concepts • Import statement 完成 Lecture 2 休息十分鐘!