Upload
stone-richards
View
23
Download
2
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
Lecture 2Software Concepts
Instructors: Fu-Chiung Cheng
( 鄭福炯 )Associate Professor
Computer Science & EngineeringTatung 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
abstractbooleanbreakbytebyvaluecasecastcatchcharclassconstcontinue
defaultdodoubleelseextendsfalsefinalfinallyfloatforfuturegeneric
gotoifimplementsimportinnerinstanceofintinterfacelongnativenewnull
operatorouterpackageprivateprotectedpublicrestreturnshortstaticsuperswitch
synchronizedthisthrowthrowstransienttruetryvarvoidvolatilewhile
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.appletjava.awtjava.beansjava.iojava.langjava.math
java.netjava.rmijava.securityjava.sqljava.textjava.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 languagesB. 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 sourcecode
Machinecode
Javabytecode
Javainterpreter
Bytecodecompiler
Javacompiler
Java Translation and Execution
Applet
Java sourcecode
Javabytecode
Javacompiler
Javainterpreter
Web browser
local computer
remotecomputer
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
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
Object-Oriented Programming:
• An object contains data and methods.• Composition: (reuse objects): Ex, A car has a engine.
Car
drive();start();
etc...
enginewheel[4]door[2]
etc...
Class
Methods
Data
Object-Oriented Programming:
• Objects communication: send messages.
Object1Object2
Object3
messagemessage
message
Object-Oriented Programming:
• Inheritance: reuse the interface.
Shape
draw();erase();
Line
draw();erase();
Circle
draw();erase();
Square
draw();erase();
Object-Oriented Programming:
• Polymorphism (dynamic binding).• shape.draw() will call the right draw function of circle line or square.
Shape Line
Circledraw();erase();
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