Download presentation
Presentation is loading. Please wait.
Published byJulianna Golden Modified over 9 years ago
1
1. Introduction Based on Java Software Development, 5 th Ed. By Lewis &Loftus
2
Topics Java Programming Language Overview Java Programming Language Overview Program Development Program Development Object-oriented Programming Overview Object-oriented Programming Overview
3
Java Characteristics Object-oriented Language Object-oriented Language Program consists of a collection of classes Program consists of a collection of classes Each class contains Each class contains one or more one or more attributes—data one or more methods—program statements one or more methods—program statements Syntax is like C++. Syntax is like C++. But it is very different from C++ But it is very different from C++ Primitive types have standard sizes Primitive types have standard sizes Automatic garbage collection Automatic garbage collection No pointers No pointers
4
Benefits of Java Portable Portable The same source code can be compiled and executed on any Operating System The same source code can be compiled and executed on any Operating System Secure Secure Designed from the start with security in mind Designed from the start with security in mind Designed for Network Programming Designed for Network Programming Makes network-related programming easy Makes network-related programming easy "Network is the computer"--Sun's company motto "Network is the computer"--Sun's company motto Dynamic Program Dynamic Program A program can load various modules as needed during execution. A program can load various modules as needed during execution. International International Java was developed from the start with international use in mind. E.g., Unicode (16 bits) encoding Java was developed from the start with international use in mind. E.g., Unicode (16 bits) encoding
5
Running a Java Program Source CodeBytecodeOutput Text EditorCompilerJVM
6
To Run a Java Program Suppose source code MyProgram.java exists in C:\310\. Compile the source code to bytecode, which will be named as MyProgram.class C:\310> javac MyProgram.java Compile the source code to bytecode, which will be named as MyProgram.class C:\310> javac MyProgram.java Run (interpret) the bytecode. C:\310> java MyProgram Run (interpret) the bytecode. C:\310> java MyProgram
7
Java Program Structure // This is a comment line class MySample { } Class body Class header
8
Java Program Structure (cont.) // This is a comment line class MySample { public static void main(String args[]) { } } Method body Method header
9
Java Program Structure (E.g.) // This is a sample program class Welcome { // prints a greeting in two lines public static void main(String[] args) { System.out.println(“Hello, Hawaii.”); System.out.println(“Welcome to Kaimuki.”); } }
10
RandomTest.java // This program generates 10 random numbers. import java.util.Random; public class RandomTest { public static void main(String[] args) { Random rand = new Random(); for (int n = 1; n <= 10; n++) { System.out.println(n + ". " + rand.nextFloat ()); } } }
11
Countdown.java // This program counts down numbers from // 10 to 0. class Countdown { public static void main(String[] args) { for (int i = 10; i >=0; i--) { System.out.println(i + "!"); } System.out.println("Lift Off!"); } }
12
Comments Comments do not affect how a program works Comments do not affect how a program works Java comments can take three forms: Java comments can take three forms: // This comment runs to end of line. // This comment runs to end of line. /* This form can be for one line */ /* or it can cover several lines */ /* This form can be for one line */ /* or it can cover several lines */ /** This is javadoc comment */ /** This is javadoc comment */
13
13 Identifiers Identifiers are the names used in a program Identifiers are the names used in a program Can be made up of letters, digits, the underscore character ( _ ), and the dollar sign Can be made up of letters, digits, the underscore character ( _ ), and the dollar sign Cannot begin with a digit Cannot begin with a digit Java is case sensitive - Total, total, and TOTAL are different identifiers Java is case sensitive - Total, total, and TOTAL are different identifiers
14
14 Identifiers (cont.) Java convention: Java convention: Lower case for variables: name, total Lower case for variables: name, total Upper case for constants: MAXIMUM Upper case for constants: MAXIMUM Mixed for compound words: lastDayOfWeek Mixed for compound words: lastDayOfWeek Reserved words (53 in Java) may not be used as identifiers. Reserved words (53 in Java) may not be used as identifiers.
15
15 Reserved Words The Java reserved words: The Java reserved words: abstract assert boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while
16
16 White Space Whte spaces: Whte spaces: spaces, line breaks, and tabs spaces, line breaks, and tabs used to separate words and symbols in a program used to separate words and symbols in a program Extra white space is ignored Extra white space is ignored Enhance program readability with: Enhance program readability with: Blank line to separate major section Blank line to separate major section Indentation for loop body Indentation for loop body Indentation in If statements Indentation in If statements
17
Outline Java Programming Language Overview Java Programming Language Overview Program Development Program Development Object-oriented Programming Overview Object-oriented Programming Overview
18
Programming Language Levels Machine language Machine language 0011 1100 1010 1111 1011 0011 1011 1101 1110 0101 1010 1001 0011 1000 1001 0011 1000 0010 0001 1101 0010 0111 1010 1011 0111 1100 0110 0011 1100 1010 1111 1011 0011 1011 1101 1110 0101 1010 1001 0011 1000 1001 0011 1000 0010 0001 1101 0010 0111 1010 1011 0111 1100 0110 Assembly language Assembly language ori $8,$0,0x2 ori $9,$0,0x3 addu $10,$8,$9 ori $8,$0,0x2 ori $9,$0,0x3 addu $10,$8,$9 High-level language High-level language sum = num1 + num2 sum = num1 + num2 Fourth-generation language Fourth-generation language Report generators Form generators Report generators Form generators Increasing abstraction
19
Program Development Steps in Program Development Steps in Program Development Writing source code Writing source code Compiling source code into machine— readable form Compiling source code into machine— readable form Debugging and Testing program Debugging and Testing program Executing program Executing program
20
Typical Programming Process Write Source code in high-level language—e.g., Java Write Source code in high-level language—e.g., Java Translate source code into machine language Translate source code into machine language Compiler translates the entire source code into single machine program Compiler translates the entire source code into single machine program Interpreter translates and executes the source code one line at a time.) Interpreter translates and executes the source code one line at a time.) Checking for errors and correcting them Checking for errors and correcting them Execute program Execute program Each type of CPU requires machine code specific to it (using operating system) Each type of CPU requires machine code specific to it (using operating system)
21
Basic Program Development errors Edit and save program Compile program Execute program and evaluate results ©2007 Pearson Addison Wesley
22
Java Programming Process Write source code in Java Write source code in Java Translate source code into a bytecode (machine laguage). Translate source code into a bytecode (machine laguage). Execute the bytecode by interpretor (Java Virtual Machine) Execute the bytecode by interpretor (Java Virtual Machine) Each CPU runs the Virtual Machine Each CPU runs the Virtual Machine Which executes the bytecode Which executes the bytecode
23
Java Environment The Java environment consists of the following elements. Java Programming Language Java Programming Language grammar for writing application programs, applets, JavaBeans components, etc. grammar for writing application programs, applets, JavaBeans components, etc. Java Virtual Machine (JVM) Java Virtual Machine (JVM) software for executing Java binary programs, called byte code software for executing Java binary programs, called byte code Java Platform (Java Standard Library, Java API) Java Platform (Java Standard Library, Java API) set of predefined classes set of predefined classes
24
Java Virtual Machine Source code is compiled into a binary file known as a bytecode. Source code is compiled into a binary file known as a bytecode. Bytecode is then interpreted by the Java Virtual Machine, which sits atop an operating system-- Windows, MacOS, Lunux. Bytecode is then interpreted by the Java Virtual Machine, which sits atop an operating system-- Windows, MacOS, Lunux. Since the bytecode does not need to know the kind of OS on which it is being executed--the JVM is tailored to particular OS--a Java code is highly portable from one type of computer system to another. Since the bytecode does not need to know the kind of OS on which it is being executed--the JVM is tailored to particular OS--a Java code is highly portable from one type of computer system to another.
25
25 Java Translation Java source code Machine code Java bytecode Bytecode interpreter Bytecode compiler Java compiler ©2007 Pearson Addison Wesley
26
Terminology Syntax Syntax Rules of language (grammar) Rules of language (grammar) d = a(b + c); -- syntax error d = a(b + c); -- syntax error Semantics Semantics Meaning of the language Meaning of the language speed = distance * time; --syntactically correct, semantically wrong speed = distance * time; --syntactically correct, semantically wrong Errors Errors Compile-time error (e.g., syntax error) Compile-time error (e.g., syntax error) Run-time error (e.g., dividing by 0) Run-time error (e.g., dividing by 0) Logic error (program runs, but produces wrong result) Logic error (program runs, but produces wrong result)
27
Outline Java Programming Language Overview Java Programming Language Overview Program Development Program Development Object-oriented Programming Overview Object-oriented Programming Overview
28
Problem Solving Steps in problem solving: Steps in problem solving: Understand the problem Understand the problem Design a solution Design a solution Consider alternatives and refine the solution Consider alternatives and refine the solution Implement the solution Implement the solution Test the solution Test the solution These steps are not linear—they overlap and repeat These steps are not linear—they overlap and repeat
29
Problem Solving (cont.) The key strategy to solve a complex problem is to break it down into manageable pieces and solve each piece—divide and conquer method. The key strategy to solve a complex problem is to break it down into manageable pieces and solve each piece—divide and conquer method. Object-oriented approach sees a program in terms of a collection of “objects” interacting with each other. Object-oriented approach sees a program in terms of a collection of “objects” interacting with each other. With Java, objects and classes are the basic units of a program With Java, objects and classes are the basic units of a program
30
Objects and Classes An OO program consists of a collection of objects interacting with each other. An OO program consists of a collection of objects interacting with each other. Suppose you are writing a game program that involves several dogs, cats, and cars. (Use your imagination.) Here are some dog objects: Suppose you are writing a game program that involves several dogs, cats, and cars. (Use your imagination.) Here are some dog objects: Fido—a big brown bulldog Fido—a big brown bulldog Lassie—a tall collie Lassie—a tall collie Taro—a small, white akita Taro—a small, white akita
31
Objects Attributes: name = Fido breed = bulldog color = brown weight = 30 kg height = 50 cm Operations: bark() display() Attributes: name = Lassie breed = collie color = brow weight = 25 kg height = 70 cm Operations bark() display() Attributes: name = Taro breed = akita color = white weight = 10kg height = 30 cm Operations bark() display() Dog1 Dog2 Dog3
32
Class A class is an abstraction of the objects of the same type. Fido, Lassie, and Taro are all instances of the Dog class. A class is an abstraction of the objects of the same type. Fido, Lassie, and Taro are all instances of the Dog class. Each objects has a number of Each objects has a number of attributes (with different values) and attributes (with different values) and operations (same methods). operations (same methods). A class is A class is a template for creating (specifying) objects of a particular type. a template for creating (specifying) objects of a particular type. like blueprint for creating objects of a particular type like blueprint for creating objects of a particular type
33
Class (cont.) A class is composed of A class is composed of Class Name Class Name Attributes Attributes Operations (methods) Operations (methods) Once a class is defined, objects can be created (instantiated) from it. Once a class is defined, objects can be created (instantiated) from it.
34
Class Diagram Dog breed color weight height Bark() display() Class name Attributes Operations Car make engineSize color maxSpeed Start() accelerate() stop() display()
35
Class (summary) A class defines an object. A class defines an object. A class is the blueprint, or template, of an object A class is the blueprint, or template, of an object A class represents a concept, and an object represents the embodiment of that concept A class represents a concept, and an object represents the embodiment of that concept Multiple objects can be created from the same class Multiple objects can be created from the same class The class uses methods to define the behaviors of the object The class uses methods to define the behaviors of the object The class that contains the main method of a Java program represents the entire program The class that contains the main method of a Java program represents the entire program
36
Classe and Object Bank Account A class (the concept) John’s Bank Account Balance: $5,257 An object (the realization) Bill’s Bank Account Balance: $1,245,069 Mary’s Bank Account Balance: $16,833 Multiple objects from the same class ©2007 Pearson Addison Wesley
37
Inheritance One class can be used to derive another via inheritance One class can be used to derive another via inheritance Classes can be organized into hierarchies Classes can be organized into hierarchies Bank Account Account Charge Account Savings Account Checking Account ©2007 Pearson Addison Wesley
38
Inheritance (2) One class can be used to derive another via inheritance One class can be used to derive another via inheritance Classes can be organized into hierarchies Classes can be organized into hierarchies Full-time Employee Part-time ] Hourly- wage Annual- salary ©2007 Pearson Addison Wesley
39
Practice Specify some relevant attributes and operations for the following classes. Specify some relevant attributes and operations for the following classes. Bank Account Bank Account Book Book Browser window Browser window Circle (geometric figure) Circle (geometric figure) Rectangle (geometric figure) Rectangle (geometric figure)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.