CTIS251-Week 1 Engineering Software and Java Dr. Ozlem Albayrak

73
CTIS251-Week 1 Engineering Software and Java Dr. Ozlem Albayrak

description

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. Introduction to CTIS251. - PowerPoint PPT Presentation

Transcript of CTIS251-Week 1 Engineering Software and Java Dr. Ozlem Albayrak

Page 1: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

CTIS251-Week 1 Engineering Software andJava

Dr. Ozlem Albayrak

Page 2: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

2

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

Page 3: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

3

Introduction to CTIS251

Course Elements:

- Instructor

- Students

- Course Material

Page 4: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

4

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]

Page 5: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

5

The Students

Attendance - Bonuses Get to Know Why are you here? Expectations Interests

Page 6: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

6

Office Hour?

Tuesday 14:40 – 15:30Thursday 8:40 – 10:30

viae-mail or phone for

appointment

Page 7: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

7

Course Material

Not limited to: Syllabus The main text book References All related high quality sources

Page 8: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

8

Syllabus

Distributed syllabus will be reviewed– Subjects– Grading (bonus questions)– Labs

Page 9: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

9

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

Page 10: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

10

Syllabus (2)9 Array of Objects, Some handy Java Classes; Arrays, String,

StringBuffer, StringTokenizer, Vector7

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 and containers, AWT (Abstract Window Toolkit), swing

11, 12, 13

14 GUI Programming, event driven programming, components and containers, AWT (Abstract Window Toolkit), swing

11, 12, 13

15 GUI Programming, Applet 14

16 Review

17 FINAL

Page 11: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

11

What is Engineering?

Page 12: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

12

Webster’s Definitionsen·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

Page 13: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

13

How is engineering software different from engineering bridges?

Page 14: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

14

Continuous– Calculus

– Testing/analysis is easy: if the bridge holds for 1M kg, it also probably holds 0.99Mkg

Discrete– Logic, Discrete

Mathematics

– Testing/analysis is difficult

Bridges Software

Page 15: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

15

Made of physical stuff– Some costs are obvious

– Changes after construction are hard

Made of virtual stuff– All costs are non-

obvious

– Changes should be easy (but they’re not)

Bridges Software

for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { nextStates [i][j] = getCellAt (i, j).getNextState (); } }

Page 16: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

16

Obvious when it fails– Bridge makers get sued

– Architects need licenses

Falls down quietly (usually)– Software vendors blame user,

charge for upgrades

– Anyone can make software, no one gets sued

Bridges Software

Page 17: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

17

Requirements are (usually) obvious and easy to describe

A good design is apparent to everyone immediately

Requirements are mysterious and hard to describe

A good design is only apparent to “experts” but has impact later on

Bridges Software

Grid

CellAutomata

Cell

CellState

GridDisplay

ConwayLifeCell

is a subtype of(extends)

Page 18: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

18

JAVA

Page 19: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

19

Google search about Java....

Returns approximately yaklaşık 307.000.000 result!

Page 20: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

20

Java Sources on the net

http://en.wikipedia.org/wiki/Java_%28programming_language%29

http://java.net/ http://java.sun.com/docs/books/tutorial/ http://www.javaturkiye.com/ Books:

http://www.oreilly.com/pub/topic/java

Page 21: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

21

What is Java?

A. Island in Indonesia known for coffee and volcanoes

B. A Programming Language (Java)

C. A Portable Low-Level Language (JVML)

D. A Platform (JavaVM)

E. A (semi-)successful marketing strategy– JavaScript is not related to Java or Java

F. All of the above

Page 22: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

22

Compiling C++ Programs

#include <stdio>main( int argc, char *argv[]) { // do something}

Page 23: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

23

The Java Virtual Machine

class Hello { public static void main(String[] args) { System.out.println( “Hello World” );}

Hello.java

…Method Hello() 0 aload_0 1 invokespecial #1 <Method java.lang.Object()> 4 returnMethod 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

Hello.class

javac

Page 24: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

24

The Java Virtual Machine

class Hello { public static void main() { // do something}

Mac JVM Linux JVM Win JVM

Hello.class

Hello.class

Hello.class

Page 25: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

25

Java : Programming Language

“A simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, and dynamic language.”

[Sun95]By the end of the course, you should have a good idea if this is a true statement.

Page 26: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

26

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

Page 27: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

27

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

Page 28: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

28

Programming Systems

Scheme Interpreter

Scheme Program

C++ Compiler

C++ Program

Machine

Object Files

Page 29: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

29

Java VM

Portability– If you can implement a Java VM

on your machine, then you can run all Java programs

Security– A VM can limit what programs

can do to the real machine

Simplicity– VM instructions can be simpler

than machine instructions

Java Compiler

Java Program

Java Virtual Machine

Class Files

Machine

Why use a virtual machine?

Page 30: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

30

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

Page 31: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

Introduction toJava Programming with JBuilder, 3E

Y. Daniel Liang

Page 32: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

32

Sequence of the Topics

Fundamentals of Programming

Object-Oriented Programming

Java API

GUI Framework Exception Handling Framework

Input/Output Framework

Collections Framework

Multithreading Framework

Database Programming Framework

(bonus chapter)

Servlets/JSP Framework

(bonus chapter)

Other application frameworks not

covered in this book

Internalization Framework

(bonus chapter)

Fundamentals of Programming

Object-Oriented Programming

GUI Framework

Exception Handling Framework

Input/Output Framework

Collections Framework

Multithreading Framework

Page 33: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

33

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, )

Page 34: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

34

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

Page 35: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

35

Chapter Dependency

Chart

Chapter 1 Introduction to Java and JBuilder

Chapter 2 Primitive Data Types and Operations

Chapter 16 Input and Output

Chapter 11 Getting Started with GUI Programming

Chapter 3 Control Statements

Chapter 5 Arrays

Chapter 6 Objects and Classes

Chapter 7 Strings

Chapter 8 Inheritance and Polymorphism

Chapter 10 OO Analysis and Design

Chapter 15 Exceptions and Assertions

Chapter 13 Creating User Interfaces

Chapter 14 Applets

Chapter 19 Multithreading

Chapter 20 Multimedia

Chapter 22 Database Programming Chapter 21 Networking

Chapter 17 Java Data Structures

Chapter 4 Methods

Chapter 9 Abstract Classes and Interfaces

Chapter 12 Event-Driven Programming

Chapter 20 Internationalization

Chapter 23 Servlets

Chapter 24 JavaServer Pages

Part V Bonus Chapters on the CD-ROM only

Page 36: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

36

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

Page 37: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

37

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

Page 38: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

38

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

Page 39: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

39

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

Page 40: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

40

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

Page 41: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

41

Chapter 1 Introduction to Java and JBuilder

What Is Java? Getting Started With Java Programming

– Create, Compile and Running a Java Application

Page 42: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

42

What Is Java?

History

Characteristics of Java

Page 43: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

43

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)

Page 44: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

44

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

Page 45: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

45

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) ... ...

Page 46: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

46

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.

Page 47: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

47

Java IDE Tools Forte by Sun MicroSystems Borland JBuilder

Microsoft Visual J++

WebGain Café

IBM Visual Age for Java

IBM WSAD

Page 48: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

48

Getting Started with Java Programming

A Simple Java Application

Compiling Programs

Executing Applications

Page 49: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

49

A Simple ApplicationExample 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!"); }}

RunRun

SourceSourceIMPORTANT 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) .

Page 50: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

50

Anatomy of a Java Program Comments Package Reserved words Modifiers Statements Blocks Classes Methods The main method

Page 51: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

51

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 */.

Page 52: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

52

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.

Page 53: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

53

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.

Page 54: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

54

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.”

Page 55: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

55

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 (;).

Page 56: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

56

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

Page 57: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

57

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.

Page 58: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

58

MethodsWhat 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.

Page 59: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

59

main MethodThe 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;

}

Page 60: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

60

Creating, Compiling, and Running Programs

Source Code

Create/Modify Source Code

Compile Source Code i.e., javac Welcome.java

Bytecode

Run Byteode i.e., java Welcome

Result

If compilation errors

If runtime errors or incorrect result

package chapter1; public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }

… 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

stored on the disk

Source code (developed by the programmer)

Byte code (generated by the compiler for JVM to read and interpret, not for you to understand)

Page 61: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

61

Executing Applications

On command line– java classname

JavaInterpreter

on Windows

JavaInterpreter

on Sun Solaris

JavaInterpreteron Linux

Bytecode

...

Page 62: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

62

Example

javac Welcome.java

java Welcome

output:...

Page 63: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

63

JBuilder BasicsInstalling JBuilder

JBuilder IDE Interface

Create a JBuilder project

Create Java programs

Compile and run Java programs

Page 64: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

64

JBuilder IDE Interface

Page 65: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

65

Creating a JBuilder project

Choose File, New Project to display the project wizard

Page 66: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

66

Creating a JBuilder project, cont.

Set output path, backup path, working directory, and source path

Page 67: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

67

Creating a JBuilder project, cont.Optional project description

Page 68: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

68

Creating a Java Program

Choose File, New Class to display the class wizard

Page 69: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

69

Compiling and Running a Program

Where are the files stored in the directory?

Local Disk (C:)

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

.

.

.

Page 70: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

70

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.”

RunRun

SourceSource 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) .

Page 71: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

71

The showMessageDialog Method JOptionPane.showMessageDialog(null, "Welcome to Java!", "Example 1.2", JOptionPane.INFORMATION_MESSAGE));

Page 72: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

72

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.

Page 73: CTIS251-Week  1 Engineering Software  and Java Dr. Ozlem Albayrak

73

Review

Introducttion to course elements Introduction to Java basics Review Next:

– Primitive Data Types and operations