Chapter 1 Introduction to Java

Download Report

Transcript Chapter 1 Introduction to Java

CTIS251-Week 1
Engineering
Software and
Java
Dr. Ozlem Albayrak
Menu

Introduction to the CTIS251 elements

What is Engineering?
– Can we engineer software?

Introduction to the java language programming
concepts

Primitive Data Types and Operations
2
Introduction to CTIS251
Course Elements:
- Instructor
- Students
- Course Material
3
Instructor
 Dr.
–
–
–
–
Ozlem Albayrak
Computer Eng. Bilkent, 1992
M.B.A., Bilkent, 1994
M.S., UMCP, 1998
Phd., Ankara, 2002
Room# 212
Phone: 290 5039
E-mail: [email protected]
4
The Students
 Attendance
- Bonuses
 Get to Know
 Why are you here?
 Expectations
 Interests
5
Office Hour?
Tuesday 14:40 – 15:30
Thursday 8:40 – 10:30
via
e-mail or phone for
appointment
6
Course Material
Not limited to:
 Syllabus
 The main text book
 References
 All related high quality sources
7
Syllabus
 Distributed
syllabus will be reviewed
– Subjects
– Grading (bonus questions)
– Labs
8
Syllabus (1)
Week
Chapters
1
Introduction to the course, basic java language
programming concepts:
Primitive Data Types and Operations
1, 2
2
Methods, Control Statements, Arrays
4, 3, 5
3
Object Oriented Programming: Objects and Classes
6
4
Data Member, Member Method, Static and final members.
Constructor
6
5
Visibility Modifiers, Acessors, and Mutators
6
6
Inheritance, Object Class
8
7
Review + Array of Objects, Some handy Java Classes;
Arrays, String, StringBuffer, StringTokenizer, Vector
7
8
MIDTERM
9
Syllabus (2)
9
Array of Objects, Some handy Java Classes; Arrays, String, 7
StringBuffer, StringTokenizer, Vector
10
Concrete class, Abstract Class, Interface
9
11
Polymorphism
8
12
Error Handling, Exception Classes and Custom Java
Exceptions
15
13
GUI Programming, event driven programming, components 11,
and containers, AWT (Abstract Window Toolkit), swing
12,
13
14
GUI Programming, event driven programming, components 11,
and containers, AWT (Abstract Window Toolkit), swing
12,
13
15
GUI Programming, Applet
16
Review
17
14
FINAL
10
What is
Engineering?
11
Webster’s Definitions
en·gi·neer·ing ( n j -nîr ng) n.
The application of scientific and mathematical
principles to practical ends such as the design,
manufacture, and operation of efficient and
economical structures, machines, processes,
and systems.
Problem solvers
12
How is engineering software
different from engineering bridges?
13
Bridges
 Continuous
– Calculus
– Testing/analysis is easy:
if the bridge holds for
1M kg, it also probably
holds 0.99Mkg
Software
 Discrete
– Logic, Discrete
Mathematics
– Testing/analysis is
difficult
14
Bridges
 Made of physical stuff
– Some costs are obvious
– Changes after
construction are hard
Software
 Made of virtual stuff
– All costs are nonobvious
– Changes should be easy
(but they’re not)
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
nextStates [i][j] =
getCellAt (i, j).getNextState ();
}
}
15
Bridges
 Obvious when it fails
– Bridge makers get sued
– Architects need licenses
Software
 Falls down quietly (usually)
– Software vendors blame user,
charge for upgrades
– Anyone can make software,
no one gets sued
16
Bridges
 Requirements are
(usually) obvious and
easy to describe
 A good design is
apparent to everyone
immediately
Software
 Requirements are
mysterious and hard to
describe
 A good design is only
apparent to “experts” but
has impact later on
CellAutomata
GridDisplay
Cell
Grid
CellState
is a subtype of
(extends)
ConwayLifeCell
17
JAVA
18
Google search about Java....
Returns
approximately
yaklaşık 307.000.000 result!
19
Java Sources on the net
 http://en.wikipedia.org/wiki/Java_%28progr
amming_language%29
 http://java.net/
 http://java.sun.com/docs/books/tutorial/
 http://www.javaturkiye.com/
 Books:
http://www.oreilly.com/pub/topic/java
20
What is Java?
A.
B.
C.
D.
E.
Island in Indonesia known for coffee and
volcanoes
A Programming Language (Java)
A Portable Low-Level Language (JVML)
A Platform (JavaVM)
A (semi-)successful marketing strategy
–
F.
JavaScript is not related to Java or Java
All of the above
21
Compiling C++ Programs
#include <stdio>
main( int argc, char *argv[]) {
// do something
}
22
The Java Virtual Machine
Hello.java
class Hello {
public static void main(String[] args) {
System.out.println( “Hello World” );
}
Hello.class
javac
…
Method Hello()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 return
Method void main(java.lang.String[])
0 getstatic #2 <Field java.io.PrintStream out>
3 ldc #3 <String "Hello World!">
5 invokevirtual #4 <Method void println(String)>
8 return
23
The Java Virtual Machine
class Hello {
public static void main() {
// do something
}
Hello.class
Mac JVM
Hello.class
Linux JVM
Hello.class
Win JVM
24
Java : Programming Language
“A simple, object-oriented,
distributed, interpreted, robust, secure,
architecture neutral, portable, highperformance, multithreaded, and
dynamic language.”
[Sun95]
By the end of the course, you
should have a good idea if this
is a true statement.
25
Platform Independence
 C++
compiles to native code for a specific
architecture (Linux, Windows…)
 Java compiles to Java bytecode
 Same bytecode runs on virtual machine for
any platform
– Only VM is platform specific
– Good for downloadable code
– Applets etc
26
Java
 Syntax
– Similar to C++
– Designed to be easy for C and C++
programmers to learn
 Semantics
(what programs mean)
– Similar to Scheme
– Designed to make it easier to reason about
properties of programs
27
Programming Systems
C++ Program
Scheme Program
C++ Compiler
Scheme Interpreter
Object Files
Machine
28
Java VM
Java Program
Why use a virtual machine?

– If you can implement a Java VM
on your machine, then you can run
all Java programs
Java Compiler
Class Files

Security
– A VM can limit what programs
can do to the real machine
Java Virtual Machine

Machine
Portability
Simplicity
– VM instructions can be simpler
than machine instructions
29
Programming in Java
 Program
is divided into classes
 A class:
– Defines a new datatype
– Defines methods and state associated with that
datatype
 We
call a value of a class datatype an object
– Objects package state and code
30
Introduction to
Java Programming with JBuilder, 3E
Y. Daniel Liang
Sequence of the Topics
Fundamentals of Programming
Java API
GUI Framework
Exception Handling
Framework
Input/Output
Framework
Object-Oriented Programming
GUI Framework
Exception Handling Framework
Other application
frameworks not
covered in this book
Object-Oriented Programming
Collections
Framework
Fundamentals of Programming
Multithreading
Framework
Input/Output Framework
Collections Framework
Multithreading Framework
Servlets/JSP
Framework
(bonus chapter)
Database Programming
Framework
(bonus chapter)
Internalization
Framework
(bonus chapter)
32
Course Objectives
 Upon
–
–
–
–
–
completing the course, you will understand
Create, compile, and run Java programs
Primitive data types
Java control flow
Methods
Arrays (for teaching Java in two semesters, this could be
the end)
– Object-oriented programming
– Core Java frameworks (Swing, exception, I/O,
collections, multithreading, multimedia, )
33
Course Objectives, cont.

You will be able to
– Develop programs using various tools
– Write simple programs using primitive data types, control
statements, methods, and arrays
– Understand object-oriented concepts and principles:
abstraction, encapsulation, inheritance, and polymorphism
– Develop a GUI interface and Java applets
– Deal with exceptions in the program
– Store and retrieve data using Java I/O
– Use data structures from the Java Collections framework
– Establish a firm foundation on Java concepts
34
Chapter 1 Introduction to Java and JBuilder
Chapter 2 Primitive Data Types and Operations
Chapter 3 Control Statements
Chapter 4 Methods
Chapter 5 Arrays
Chapter 6 Objects and Classes
Chapter
Dependency
Chart
Chapter 7 Strings
Chapter 8 Inheritance and Polymorphism
Chapter 11 Getting Started with GUI Programming
Chapter 9 Abstract Classes and Interfaces
Chapter 12 Event-Driven Programming
Chapter 10 OO Analysis and Design
Chapter 13 Creating User Interfaces
Chapter 17 Java Data Structures
Chapter 14 Applets
Chapter 19 Multithreading
Chapter 15 Exceptions and Assertions
Chapter 20 Multimedia
Chapter 16 Input and Output
Chapter 21 Networking
Chapter 20 Internationalization
Chapter 22 Database Programming
Chapter 23 Servlets
Part V Bonus Chapters
on the CD-ROM only
Chapter 24 JavaServer Pages
35
Book Chapters
 Part
I: Fundamentals of Programming
– Chapter 1 Introduction to Java and JBuilder
– Chapter 2 Primitive Data Types and Operations
– Chapter 3 Control Statements
– Chapter 4 Methods
– Chapter 5 Arrays
36
Book Chapters, cont.
 Part
II: Object-Oriented Programming
– Chapter 6 Objects and Classes
– Chapter 7 Strings
– Chapter 8 Inheritance and Polymorphism
– Chapter 9 Abstract Classes and Interfaces
– Chapter 10 Object-Oriented Analysis and Design
37
Book Chapters, cont.
 Part
III: GUI Programming
– Chapter 11 Getting Started with GUI Programming
– Chapter 12 Event-Driven Programming
– Chapter 13 Creating User Interfaces
– Chapter 14 Applets
38
Book Chapters, cont.

Part IV: Developing Comprehensive Projects
– Chapter 15 Exceptions and Assertions
– Chapter 16 Input and Output
– Chapter 17 Java Data Structures
– Chapter 18 Multithreading
– Chapter 19 Multimedia
39
Bonus Chapters on the CD-ROM

Part V: Bonus Chapters
– Chapter 20 Internationalization
– Chapter 21 Networking
– Chapter 22 Database Programming
– Chapter 18 Servlets
– Chapter 19 JavaServer Pages
40
Chapter 1 Introduction to Java
and JBuilder
 What
Is Java?
 Getting Started With Java Programming
– Create, Compile and Running a Java
Application
41
What Is Java?
 History
 Characteristics
of Java
42
History
 James
Gosling and Sun Microsystems
 Oak
 Java,
May 20, 1995, Sun World
 HotJava
– The first Java-enabled Web browser
 JDK
Evolutions
 J2SE,
J2ME, and J2EE (not mentioned in the
book, but could discuss here optionally)
43
Characteristics of Java

Java is simple

Java is object-oriented

Java is distributed

Java is interpreted

Java is robust

Java is secure

Java is architecture-neutral

Java is portable

Java’s performance

Java is multithreaded

Java is dynamic
44
JDK Versions
 JDK
1.02 (1995)
 JDK 1.1 (1996)
 Java 2 SDK v 1.2 (a.k.a JDK 1.2, 1998)
 Java 2 SDK v 1.3 (a.k.a JDK 1.3, 2000)
 Java 2 SDK v 1.4 (a.k.a JDK 1.4, 2002)
 ...
 ...
45
JDK Editions
 Java
Standard Edition (J2SE)
– J2SE can be used to develop client-side standalone
applications or applets.
 Java
Enterprise Edition (J2EE)
– J2EE can be used to develop server-side applications such as
Java servlets and Java ServerPages.
 Java
Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile
devices such as cell phones.
This book uses J2SE to introduce Java programming.
46
Java IDE Tools
 Forte
by Sun MicroSystems
 Borland JBuilder
 Microsoft
Visual J++
 WebGain
Café
 IBM
Visual Age for Java
 IBM
WSAD
47
Getting Started with Java
Programming
 A Simple
Java Application
 Compiling
Programs
 Executing Applications
48
A Simple Application
Example 1.1
//This application program prints Welcome
//to Java!
package chapter1;
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Source
Run
IMPORTANT NOTE: To run the program from the Run
button, (1) set c:\jbuilder9\jdk1.4\bin on your path, and
(2) copy both the slide directory and the example
directory from the IR-CD to a directory (e.g.,
c:\LiangIR-CD) .
49
Anatomy of a Java Program
 Comments
 Package
 Reserved
words
 Modifiers
 Statements
 Blocks
 Classes
 Methods
 The
main method
50
Comments
In Java, comments are preceded by two slashes (//) in a
line, or enclosed between /* and */ in one or multiple
lines. When the compiler sees //, it ignores all text after //
in the same line. When it sees /*, it scans for the next */
and ignores any text between /* and */.
51
Package
The second line in the program (package chapter1;)
specifies a package name, chapter1, for the class
Welcome. Forte compiles the source code in
Welcome.java, generates Welcome.class, and stores
Welcome.class in the chapter1 folder.
52
Reserved Words
Reserved words or keywords are words that have a
specific meaning to the compiler and cannot be used for
other purposes in the program. For example, when the
compiler sees the word class, it understands that the word
after class is the name for the class. Other reserved words
in Example 1.1 are public, static, and void. Their use will
be introduced later in the book.
53
Modifiers
Java uses certain reserved words called modifiers that
specify the properties of the data, methods, and
classes and how they can be used. Examples of
modifiers are public and static. Other modifiers are
private, final, abstract, and protected. A public datum,
method, or class can be accessed by other programs. A
private datum or method cannot be accessed by other
programs. Modifiers are discussed in Chapter 6,
“Objects and Classes.”
54
Statements
A statement represents an action or a sequence of actions.
The statement System.out.println("Welcome to Java!") in
the program in Example 1.1 is a statement to display the
greeting "Welcome to Java!" Every statement in Java
ends with a semicolon (;).
55
Blocks
A pair of braces in a program forms a block that groups
components of a program.
public class Test {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Class block
Method block
56
Classes
The class is the essential Java construct. A class is a
template or blueprint for objects. To program in Java,
you must understand classes and be able to write and use
them. The mystery of the class will continue to be
unveiled throughout this book. For now, though,
understand that a program is defined by using one or
more classes.
57
Methods
What is System.out.println? It is a method: a collection
of statements that performs a sequence of operations to
display a message on the console. It can be used even
without fully understanding the details of how it works.
It is used by invoking a statement with a string argument.
The string argument is enclosed within parentheses. In
this case, the argument is "Welcome to Java!" You can
call the same println method with a different argument to
print a different message.
58
main Method
The main method provides the control of program flow.
The Java interpreter executes the application by invoking
the main method.
The main method looks like this:
public static void main(String[] args) {
// Statements;
}
59
Creating, Compiling, and Running Programs
Create/Modify Source Code
Source code (developed by the programmer)
package chapter1;
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Byte code (generated by the compiler for JVM
to read and interpret, not for you to understand)
…
Method Welcome()
0 aload_0
…
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return
Saved on the disk
Source Code
Compile Source Code
i.e., javac Welcome.java
If compilation errors
stored on the disk
Bytecode
Run Byteode
i.e., java Welcome
Result
If runtime errors or incorrect result
60
Executing Applications
 On command line
– java classname
Bytecode
Java
Interpreter
on Windows
Java
Interpreter
on Linux
...
Java
Interpreter
on Sun Solaris
61
Example
javac Welcome.java
java Welcome
output:...
62
JBuilder Basics
Installing
JBuilder
JBuilder
IDE Interface
Create
a JBuilder project
Create
Java programs
Compile
and run Java programs
63
JBuilder IDE Interface
64
Creating a JBuilder project
Choose File, New Project to display the project wizard
65
Creating a JBuilder project, cont.
Set output path, backup path, working directory, and source path
66
Creating a JBuilder project, cont.
Optional project description
67
Creating a Java Program
Choose File, New Class to display the class wizard
68
Compiling and Running a Program
Local Disk (C:)
Where are the files
stored in the
directory?
Example
chapter1.jpx
chapter1.html
chapter1
Welcome.java
Welcome.class
chapter2
Java source files and class files for Chapter 2
.
.
.
chapter19
Java source files and class files for Chapter 19
bak
chapter1
Welcome.java~1~
chapter2
.
.
.
Backup files for Chapter 2
chapter19
Backup files for Chapter 19
69
Displaying Text in a Message
Dialog Box
you can use the showMessageDialog method in the
JOptionPane class. JOptionPane is one of the many
predefined classes in the Java system, which can be
reused rather than “reinventing the wheel.”
Source
Run
IMPORTANT NOTE: To run the program from the Run
button, (1) set c:\jbuilder9\jdk1.4\bin on your path, and
(2) copy both the slide directory and the example
directory from the IR-CD to a directory (e.g.,
c:\LiangIR-CD) .
70
The showMessageDialog Method
JOptionPane.showMessageDialog(null,
"Welcome to Java!",
"Example 1.2",
JOptionPane.INFORMATION_MESSAGE));
71
The exit Method
Use Exit to terminate the program and stop all
threads.
NOTE: When your program starts, a thread is
spawned to run the program. When the
showMessageDialog is invoked, a separate
thread is spawned to run this method. The
thread is not terminated even you close the
dialog box. To terminate the thread, you have
to invoke the exit method.
72
Review
 Introducttion
to course elements
 Introduction to Java basics
 Review
 Next:
– Primitive Data Types and operations
73