Download presentation
Presentation is loading. Please wait.
Published byEdmund Davidson Modified over 9 years ago
2
Lecture 2 Software Concepts Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology
3
Outline Structure of Java standalone applications Basic program elements Executing a program Helpful support for writing software Java applets
4
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
5
Java Program Structure White 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
6
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 */
7
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
8
Java Program Structure: Indentifiers Identifiers in Lincoln.java: class name: Lincoln method name: main, System.out.println variables: args key words: String
9
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
10
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
11
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."
12
Java Program Structure Java 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
13
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
14
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.
15
Java Translation and Execution Standalone applications: Java source code Machine code Java bytecode Java interpreter Bytecode compiler Java compiler
16
Java Translation and Execution Applet Java source code Java bytecode Java compiler Java interpreter Web browser local computer remote computer
17
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
18
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.
19
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
20
Object-Oriented Programming: A class (type) represents a concept and An object (instance) represents the realization of that concept. Car My first car John's car Dad's car Class Objects
21
Object-Oriented Programming: An object contains data and methods. Composition: (reuse objects): Ex, A car has a engine. Car drive(); start(); etc... engine wheel[4] door[2] etc... Class Methods Data
22
Object-Oriented Programming: Objects communication: send messages. Object1 Object2 Object3 message
23
Object-Oriented Programming: Inheritance: reuse the interface. Shape draw(); erase(); Line draw(); erase(); Circle draw(); erase(); Square draw(); erase();
24
Object-Oriented Programming: Polymorphism (dynamic binding). shape.draw() will call the right draw function of circle line or square. ShapeLine Circle draw(); erase(); Square
25
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);
26
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
27
Conclusions Simple Structure of Java: classes Object-oriented concepts Import statement 完成 Lecture 2 休息十分鐘!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.