38
Object Oriented Programming- Java Rebaz M. Nabi Email: [email protected] 1 OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Oop lecture1-chapter1(review of java)

Embed Size (px)

Citation preview

Page 1: Oop lecture1-chapter1(review of java)

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

1

Object Oriented Programming-Java

Rebaz M. Nabi

Email: [email protected]

Page 2: Oop lecture1-chapter1(review of java)

2

Class Policy

RESPECTOOP 2nd Class TCI-SPU Rebaz M. nabi

@2015-2016

Page 3: Oop lecture1-chapter1(review of java)

3

Background• Deitel, H.M. & Deitel, P.J. (2009). Java: How to Program. Prentice Hall (8th ed.). • Flanagan, D. (2005). Java in a nutshell : a desktop quick reference. O’Reilly (5th ed.). • Flanagan, D. (2004). Java examples in a nutshell : a tutorial companion to Java in a

nutshell. O’Reilly (3rd ed.). • Gamma, E., Helm, R., Johnson, R. & Vlissides, A. (1995). Design patterns: elements

of reusable object-oriented software. Addison-Wesley. • Bloch, J. & Gafter, N. (2005). Java puzzlers. Addison-Wesley.• Object Oriented Modeling and Design with UML Michael Blaha and James

Rambaugh – PEARSON second edition.• UML Distilled: A Brief Guide to the Standard Object Modeling Language (3rd

Edition) by Martin Fowle.• Douglas Bell. Software Engineering. Third Edition. 2000.• Cay Horstmann, “ Big Java”, 3rd Edition, John Wiley and Sons, 2008, ISBN: 978-0-

470-10554-2

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 4: Oop lecture1-chapter1(review of java)

4

Objectives• The goal of this course is to provide students with the ability to

write programs in Java and make use of the concepts of Object-Oriented Programming (the principles of OOP which is inheritance, polymorphism, abstract, and encapsulation). Examples and discussions will use Java primarily, but other languages may be used to illustrate specific points where appropriate. As well as this course considered the GUI programs in java and JDBC to access database applications from java code. The course is designed to accommodate students with diverse programming backgrounds; it is taught with a mixture of lectures and practical sessions where students can work at their own pace from a course handbook. Each practical class will culminate in an assessed exercise.

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 5: Oop lecture1-chapter1(review of java)

5

AimsAfter learning the course the students should be able to:• Be familiar with the main features and limitations of the Java language;• Be able to write a Java program to solve a well specified problem;• Understand the principles of OOP;• Understand how to use GUI components in Java • Be able to demonstrate good object-oriented programming skills in Java;• Be able to describe, recognise, apply and implement selected design

patterns in Java;• Develop database-driven applications• Identify exception handling methods.• Implement multithreading in object oriented programs.• Prepare UML diagrams for software system

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 6: Oop lecture1-chapter1(review of java)

6

Course Content• Revision of principles of programming in java• Principles of Object-Oriented Programming• Classes and Objects in Java• Graphical User Interface• Encapsulation and inner class• Packages and Standard libraries• Interface and Abstract class• Exceptions Handling• Input Output stream• Java Database Connectivity JDBC• Collection Classes• Concurrent programming • Software Testing and Flow Diagram

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 7: Oop lecture1-chapter1(review of java)

7

Basic Definitions

• A computer is a machine that performs calculations and processes information – A computer works under the control of a computer program

• A computer program is a set of instructions that tell a computer what to do

• Hardware refers to the electronic and mechanical components of a computer

• Software refers to the programs that control the hardware

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 8: Oop lecture1-chapter1(review of java)

8

Computers

HardwareSoftware

Computers

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 9: Oop lecture1-chapter1(review of java)

9

Hardware

• A computer's hardware is organized into several main subsystems or components

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 10: Oop lecture1-chapter1(review of java)

10

Software

• There are two main types of software:• Application software

– designed to provide a particular task or service– word processors, computer games, spreadsheet programs,

and Web browsers• System software

– includes programs that perform the basic operations that make a computer usable

– An example of the system software is operating systems, which contains programs that manage the data stored on the computer's disks, such as Windows or Linux

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 11: Oop lecture1-chapter1(review of java)

11

Programming Languages• Programmers write instructions in various programming

languages, some directly understandable by computers and others requiring intermediate translation steps. Hundreds of computer languages are in use today. These may be divided into three general types:

1. Machine languages( Numbers)2. Assembly languages ( Keywords)3. High-level languages(Close to Human English )4. New Generation Languages

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 12: Oop lecture1-chapter1(review of java)

12

Machine languages• Any computer can directly understand only its own

machine language. Machine language is the "natural language" of a computer and as such is defined by its hardware design. Machine languages generally consist of strings of numbers (ultimately reduced to 1s and 0s) that instruct computers to perform their most elementary operations one at a time.

Samples– +1300042774 – +1400593419 – +1200274027

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 13: Oop lecture1-chapter1(review of java)

13

assembly languages• Machine-language programming was simply too slow and

tedious for most programmers. These abbreviations formed the basis of assembly languages. Translator programs called assemblers were developed to convert early assembly-language programs to machine language at computer speeds.

• Although such code is clearer to humans, it is incomprehensible to computers until translated to machine language.– load– add – store

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 14: Oop lecture1-chapter1(review of java)

14

High level languages• To speed the programming process, high-level languages were

developed in which single statements could be written to accomplish substantial tasks. Translator programs called compilers convert high-level language programs into machine language. High-level languages allow programmers to write instructions that look almost like everyday English and contain commonly used mathematical notations.

• grossPay = basePay + overTimePay –

C, C++, Microsoft's .NET languages (e.g., Visual Basic .NET, Visual C++ .NET and C#) and Java are among the most widely used high-level programming languages.

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 15: Oop lecture1-chapter1(review of java)

15

Compilers and Interpreters

• The process of compiling a high-level language program into machine language can take a considerable amount of computer time. Interpreter programs were developed to execute high-level language programs directly.

• Translators are used to translate a high-level or source code program (in C++ or Java) into machine language code or object code

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 16: Oop lecture1-chapter1(review of java)

16

Compilers and Interpreters e)

• Source code translators come in two varieties:– An interpreter translates a single line of source code

directly into machine language and executes the code before going on to the next line of source code• for example PHP and Perl.

– A compiler translates the entire source code program into executable object code. The object code can be run directly without further translation• for example C, C++, and Pascal• Java programs use both compilation and interpretation in

their translation process

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 17: Oop lecture1-chapter1(review of java)

17

History of Java• Java evolved from C++, which evolved from C.• The C language was evolved from B by Dennis Ritchie(Father of

Computer Programming(my point of view) at Bell Laboratories and was originally implemented in 1972. It initially became widely known as the development language of the UNIX operating system. Today, most of the code for general-purpose operating systems (e.g., those found in laptops, desktops, workstations and small servers) is written in C or C++.

• C++, an extension of C, was developed by Bjarne Stroustrup in the early 1980s at Bell Laboratories (now part of Lucent). C++ provides a number of features that "spruce up" the C language, but more important, it provides capabilities for object-oriented programming

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 18: Oop lecture1-chapter1(review of java)

18

History of Java (Cont.)• Microprocessors are having a profound impact in

intelligent consumer-electronic devices. Recognizing this, Sun Microsystems in 1991 funded an internal corporate research project code-named Green, which resulted in the development of a C++-based language that its creator, James Gosling, called Oak after an oak tree outside his window at Sun.

• It was later discovered that there already was a computer language called Oak. When a group of Sun people visited a local coffee shop, the name Java was suggested, and it stuck.

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 19: Oop lecture1-chapter1(review of java)

19

Why Java? Object Oriented

Object-oriented languages divide programs into separate modules, called objects, that encapsulate the program's data and operations

Robust meaning that errors in Java programs don't cause system crashes as often as errors

in other programming languages Platform Independent

Platform is a particular kind of computer system, such as a Macintosh or Windows system

Java program can be run without changes on different kinds of computers Distributed Language

which means that its programs can be designed to run on computer networks Secure

Java designed to be used on networks, Java contains features that protect against untrusted code

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 20: Oop lecture1-chapter1(review of java)

20

Software Development Process

• source code is written in plain text files with the .java extension

• source files are then compiled into .class files by the java compiler

• A .class contains bytecodes: the machine language of the Java Virtual Machine (Java VM).

• The java runs the application with an instance of the Java VM

• OOP 2nd Class TCI-SPU Rebaz M. nabi

@2015-2016

Page 21: Oop lecture1-chapter1(review of java)

21

Fundamentals of Programmingin Java

• Java VM is available on many different operating systems

• The same .class files are capable of running on – Microsoft Windows – Solaris OS – Linux – Mac OS

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 22: Oop lecture1-chapter1(review of java)

22

The Java Platforms

• The Java platform runs on top of other hardware-based platforms like Windows, Linux or Mac OS

• has two components: – The Java Virtual Machine (VM)– The Java Application Programming Interface (API)

• The Java Virtual Machine (Java VM) is the base for the Java platform

• The API is a large collection of software components grouped into libraries known as packages

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 23: Oop lecture1-chapter1(review of java)

23

Write a java program• The necessary tools are:

– The Java SE Development Kit 8 (JDK) you can download the latest version here:– http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-

2133151.html– For description and how does it work click below link:– http://docs.oracle.com/javase/7/docs/webnotes/install/index.html – Text pad( will be provided during practical lectures) or you can download it

here – http://www.textpad.com/download/– Eclipse or NetBeans (prefer NetBeans)

• Creating an Application in Windows– Create a source file in .java extension– Compile the source file into a .class java compiler – Run the program

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 24: Oop lecture1-chapter1(review of java)

24

Java Program Development

• Phase 1.0 : Editor • Phase 2.0 : Compiler • Phase 3.1 : Class Loader • Phase 3.2 : Bytecode Verifier • Phase 3.3 : Java Virtual Machine

• Program is edited in a text editor, much as a paper for an English class is edited in a word processor.

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 25: Oop lecture1-chapter1(review of java)

25

Java Conventions

• About Java programs, it is very important to keep in mind the following points.• Case Sensitivity - Java is case sensitive, which means identifier Hello and hello

would have different meaning in Java.• Class Names - For all class names the first letter should be in Upper Case.

If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.Example: class MyFirstJavaClass

• Method Names - All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.Example: public void myMethodName()

• Program File Name - Name of the program file should exactly match the class name. Example : Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved as 'MyFirstJavaProgram.java’

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 26: Oop lecture1-chapter1(review of java)

26

Java Identifiers

• All Java components require names. Names used for classes, variables and methods are called identifiers.

• In Java, there are several points to remember about identifiers. They are as follows:

• All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).

• After the first character identifiers can have any combination of characters.

• A reserved key word cannot be used as an identifier.• Most importantly identifiers are case sensitive• Examples of legal identifiers: age, $salary, _value, __1_value• Examples of illegal identifiers: 123abc, -salary.

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 27: Oop lecture1-chapter1(review of java)

27

Java Modifiers

• Like other languages, it is possible to modify classes, methods, etc., by using modifiers. There are two categories of modifiers:

• Access Modifiers: default, public , protected, private• Non-access Modifiers: final, abstract, static• We will be looking into more details about modifiers in the

next section.

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 28: Oop lecture1-chapter1(review of java)

28

Java Variables

- Local Variables • Local variables are visible only within the declared method,

constructor or block.• Access modifiers cannot be used for local variables.• There is no default value for local variables so local variables

should be declared and an initial value should be assigned before the first use.

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 29: Oop lecture1-chapter1(review of java)

29

Java Variables

- InstanceVariables • Instance variables are declared in a class, but outside a method, constructor or any

block.• Access modifiers can be given for instance variables.• The instance variables are visible for all methods, constructors and block in the

class. Normally, it is recommended to make these variables private (access level). However visibility for subclasses can be given for these variables with the use of access modifiers.

• Instance variables have default values. For numbers the default value is 0, for Booleans it is false and for object references it is null. Values can be assigned during the declaration or within the constructor.

• Instance variables can be accessed directly by calling the variable name inside the class. However within static methods and different class ( when instance variables are given accessibility) should be called using the fully qualified name . ObjectReference.VariableName.

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 30: Oop lecture1-chapter1(review of java)

30

Java Variables

- Class Variables • Class variables are variables declared with in a class, outside

any method, with the static keyword.• Access modifiers can be given for instance variables.• In the case of having static variables or methods, you can

access or use them by using the class’s name following it’s variable or method name

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

Page 31: Oop lecture1-chapter1(review of java)

31

Variables

Definition: a variable is a location in the computer memory which can store a value of a certain type

Each variable has a name, a type, a size, and a value.Examples of variables: 1 byte

byte age = 22; short shortNum; int intNum; age long longNum; char mychar; boolean isMale; Name value

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

22

Page 32: Oop lecture1-chapter1(review of java)

32

Java Keywords

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

abstract Assert boolean breakbyte Case catch charclass Const continue defaultdo Double else enumextends Final finally floatfor Goto if implementsimport Instanceof int interfacelong Native new packageprivate Protected public returnshort static strictfp superswitch synchronized this throwthrows transient try voidVolatile while

Page 33: Oop lecture1-chapter1(review of java)

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

33

Comments

• Java statements direct the operations of a program, while comments are used to help document what the program does.The Java supports three kinds of– /* text */ • The compiler ignores everything from /* to */.

– /** documentation */ • This indicates a documentation comment

– // text • The compiler ignores everything from // to the end of

the line.

Page 34: Oop lecture1-chapter1(review of java)

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

34

Java Data Type

– ** documentation */ • This indicates a documentation comment

– // text • The compiler ignores everything from // to the end of

the line.

Page 35: Oop lecture1-chapter1(review of java)

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

35

Escape SequencesNotation Character represented\n Newline (0x0a)\r Carriage return (0x0d)\f Formfeed (0x0c)\b Backspace (0x08)\s Space (0x20)\t tab\" Double quote\' Single quote\\ backslash\ddd Octal character (ddd)

\uxxxx Hexadecimal UNICODE character (xxxx)

Page 36: Oop lecture1-chapter1(review of java)

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

36

Declares and Initializes Variable

Page 37: Oop lecture1-chapter1(review of java)

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

37

Java Modifiers Types

- Access Control Modifiers• Java provides a number of access modifiers to set

access levels for classes, variables, methods and constructors. The four access levels are:

• Visible to the package, the default. No modifiers are needed.

• Visible to the class only (private).• Visible to the world (public).• Visible to the package and all subclasses (protected).

Page 38: Oop lecture1-chapter1(review of java)

OOP 2nd Class TCI-SPU Rebaz M. nabi @2015-2016

38

Java Modifiers Types

- Non Access Modifiers• Java provides a number of non-access modifiers to achieve

many other functionality.• The static modifier for creating class methods and variables• The final modifier for finalizing the implementations of

classes, methods, and variables.• The abstract modifier for creating abstract classes and

methods.• The synchronized and volatile modifiers, which are used for

threads.