chapter 00 revision

36
LECTURER: NURHANNA AZIZ 1 Object Oriented Programming LECTURER: NURHANNA AZIZ ROOM: 3206 EMAIL: [email protected] HP: 0192223454

Upload: nurhanna-aziz

Post on 26-May-2015

262 views

Category:

Technology


2 download

DESCRIPTION

not 100% wrote by the author.just to share.

TRANSCRIPT

Page 1: Chapter 00 revision

LECTURER: NURHANNA AZIZ

1

Object Oriented Programming

LECTURER: NURHANNA AZIZ

ROOM: 3206

EMAIL : NURHANNA@KUIS .EDU.MY

HP: 0192223454

Page 2: Chapter 00 revision

Prerequisites 2

� Must have taken “Introduction to Programming” / “Fundamental of Programming” subject

� Basic understanding of programming (variables, control flow, branch condition, simple array etc)

Student must be able to write a simple program � Student must be able to write a simple program

Page 3: Chapter 00 revision

Recommended Books3

Deitel & Deitel

� Java How to Program. 2007. Pearson International Edition.

Dennis Liang

� Java Object Oriented Programming. 2009. Thompson Learning

C. Thomas Wu.

� An introduction to Object Oriented Programming with Java. 2004. McGrawHill.

Page 4: Chapter 00 revision

Syllabus4

� Object Oriented Programming� Method� Encapsulation� Inheritance� Polymorphism� Polymorphism� Exceptions� Java Applet� Building Java GUIs� GUI Event Handling� GUI-based applications

Page 5: Chapter 00 revision

Course Evaluation5

� Mid term Test 20%

� Project 20%

� Lab Assessments 15%

� Attendance 5%� Attendance 5%

� Final Exam 40%

Page 6: Chapter 00 revision

Teaching Outcomes6

By the end of this module, student should be :

� Familiar with programming concept within JAVA

� Able to carry out design and development of complex element such as user interfaces

Page 7: Chapter 00 revision

Learning Outcomes7

� An appreciation of the complexities of large programs, and the use of classes and inheritance to manage this complexity

� The ability to design a collection of classes to implement a large programimplement a large program

� An appreciation of good programming style (clarity, elegance)

Page 8: Chapter 00 revision

How to pass the subject?8

� Continuous Assessment (Coursework) 60%

� Final Exam 40%

� For DTCO 3023 – 50% overall to pass� For DTCO 3023 – 50% overall to pass� For DTCO 3103 & DTCO 3113 – 40%

overall to pass

� Attend the lectures� Do assignment, exercise � Do all the practical(lab) as best as you can.� Project Presentation

Page 9: Chapter 00 revision

Attendance9

� Student will be bared from sitting for final exam if

� Not attend for lectures & lab – 3 times continuously; or

� Have 30% and more absent record

Page 10: Chapter 00 revision

10

Revision

Page 11: Chapter 00 revision

Sample of Java Application Program11

VIEW SOURCE RUN PROGRAM

VIEW SOURCE CODE

Page 12: Chapter 00 revision

Source Code12

Name of the class / programs

The “main” method

Page 13: Chapter 00 revision

Why Java?13

� “Write once, Run Anywhere”

� Security

� Network-centric Programming

� Dynamic, Extensible Program� Dynamic, Extensible Program

� Internationalization

� Performance

� Programmer efficiency and time-to-Market

Page 14: Chapter 00 revision

Java: Names & Reserved Names14

� Legal name (variables, method, fields, parameter,class, interface or package)

� Start with:

� Letter/dollar sign($)/ underscore (_)/ digits(0-9)

� Reserved names� Reserved names

abstract char else goto long public

assert class extends if native return

boolean const false implements new short

break continue final import null static

byte default finally instanceof package strictfp

case do float int private super

catch double for interface protected switch

Page 15: Chapter 00 revision

Java Naming Conventions15

� Names of variables, fields, methods:

�Begin with lowercase letter

� shape, myShape

Name of classes and interfaces:� Name of classes and interfaces:

�Begin with UPPERCASE letter

�Cube, ColorCube

Page 16: Chapter 00 revision

16

� Named constants:

� Written fully in UPPERCASE

� Separated by underscore (_)

� CENTER, MAX_VALUE, MIN_VALUECENTER, MAX_VALUE, MIN_VALUE

� If name is composed of several words:

� First word begins with lowercase, first letter of second word begins with UPPERCASE, the rest back to lowercase

� setLayout,addPatientName

Page 17: Chapter 00 revision

Comments17

� Have no effect on the execution of the program

� 2 forms:� one-line comments

� E.g.

Class Comment {Class Comment {

// This is one-line comment, its extends to the end of line }

� delimited comments

� E.g.

Class Comment {

/* This is a delimited comment,

extending over several lines

*/

Page 18: Chapter 00 revision

Types18

� Primitive type

� E.g.

� boolean,char,byte,short, int, long,float,double

� Reference type

� Class type defined/ interface type defined

� Array type� In form [ ]

Page 19: Chapter 00 revision

Variables19

� Syntax

� <Variable-modifier>< type>< variables_name>;

� E.g.public static void main (String[] args){public static void main (String[] args){

int a, b,c;

int x=1, y=2,z=3;

int myDivide=z/x;

double PI=3.1415;

boolean isFound=false;

}

Page 20: Chapter 00 revision

Declaring Variables20

int x; // Declare x to be an

// integer variable;

double radius; // Declare radius todouble radius; // Declare radius to

// be a double variable;

char a; // Declare a to be a

// character variable;

Page 21: Chapter 00 revision

Assignment Statements21

x = 1; // Assign 1 to x;

radius = 1.0; // Assign 1.0 toradius = 1.0; // Assign 1.0 to

a = 'A'; // Assign 'A' to a;

Page 22: Chapter 00 revision

Declaring and Initializing in One Step

22

� int x = 1;

� double d = 1.4;� double d = 1.4;

Page 23: Chapter 00 revision

Constants23

final datatype CONSTANTNAME = VALUE;

final double PI = 3.14159;

final int SIZE = 3;final int SIZE = 3;

Page 24: Chapter 00 revision

The String Type 24

E.g.: String message = "Welcome to Java";

String Concatenation // Three strings are concatenatedString message = "Welcome " + "to " + "Java";String message = "Welcome " + "to " + "Java";

// String Chapter is concatenated with number 2String s = "Chapter" + 2; // s becomes Chapter2

// String Supplement is concatenated with character BString s1 = "Supplement" + 'B'; // s1 becomes SupplementB

Page 25: Chapter 00 revision

Programming Errors25

� Syntax Errors

� Detected by the compiler

� Runtime Errors

� Causes the program to abort

� Logic Errors� Logic Errors

� Produces incorrect result

Page 26: Chapter 00 revision

Converting Strings to Integers26

� use the static parseInt method in the Integer class as follows:

int intValue = Integer.parseInt(intString);

where intString is a numeric string such as “123”.

Page 27: Chapter 00 revision

Converting Strings to Doubles27

�use the static parseDouble method in the Doubleclass as follows:

double doubleValuedouble doubleValue

=Double.parseDouble(doubleString);

where doubleString is a numeric string such as “123.45”.

Page 28: Chapter 00 revision

The Two-way if Statement28

if (boolean-expression) {

statement(s)-for-the-true-case;

}

else {

statement(s)-for-the-false-case;

} }

Boolean

Expression

false true

Statement(s) for the false case Statement(s) for the true case

Page 29: Chapter 00 revision

if...else Example29

if (radius >= 0) {

area = radius * radius * 3.14159;

System.out.println("The area for the “

+ “circle of radius " + radius + + “circle of radius " + radius +

" is " + area);

}

else {

System.out.println("Negative input");

}

Page 30: Chapter 00 revision

Common Errors

30

�Adding a semicolon at the end of an if clause is a common mistake.

if (radius >= 0);

{{

area = radius*radius*PI;

System.out.println(

"The area for the circle of radius " +

radius + " is " + area);

}

Page 31: Chapter 00 revision

switch Statements31switch (status) {

case 0: compute taxes for single filers;

break;

case 1: compute taxes for married file jointly;

break;

case 2: compute taxes for married file separately;case 2: compute taxes for married file separately;

break;

case 3: compute taxes for head of household;

break;

default: System.out.println("Errors: invalid status");

System.exit(0);

}

Page 32: Chapter 00 revision

while Loops32

int count = 0;

while (count < 100) {

System.out.println("Welcome to Java");

count++;

}

Page 33: Chapter 00 revision

while Loop Flow Chart33

while (loop-continuation-condition) {

// loop-body;

Statement(s);

}

int count = 0;

while (count < 100) {

System.out.println("Welcome to Java!");

count++;

}

count = 0;

Loop

Continuation

Condition?

true

Statement(s)

(loop body)

false

(count < 100)?

true

System.out.println("Welcome to Java!");

count++;

false

(A) (B)

count = 0;

Page 34: Chapter 00 revision

do-while Loop34

Loop

Statement(s)

(loop body)

do {

// Loop body;

Statement(s);

} while (loop-continuation-condition);

Loop

Continuation

Condition?

true

false

Page 35: Chapter 00 revision

for Loops35

for (initial-action; loop-continuation-condition; action-after-each-iteration) {// loop body;Statement(s);

}

int i;

for (i = 0; i < 100;

i++) {

System.out.println(

"Welcome to

Java!");

}

Loop

Continuation

Condition?

true

Statement(s)

(loop body)

false

(A)

Action-After-Each-Iteration

Initial-Action

(i < 100)?

true

System.out.println(

"Welcome to Java");

false

(B)

i++

i = 0

Page 36: Chapter 00 revision

Review Questions36

Problems:

1. Wahid just bought himself a set of home theatre. Declare a variables of TV of type String, speakers of type int, price of type String, speakers of type int, price of type double and goodCondition of type boolean.

2. Amir had 5 history books. Declare an array of book he had.