Transcript
Page 1: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

02 A Object-oriented Programming in Java

CS1102S: Data Structures and Algorithms

Martin Henz

January 20, 2010

Generated on Tuesday 19th January, 2010, 19:08

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 1

Page 2: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

1 Classes and Objects

2 Data Types

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 2

Page 3: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

1 Classes and ObjectsMethodsParameter PassingIdentifiers

2 Data Types

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 3

Page 4: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Classes

A class is a data type that specifies the components of andmethods available for its instances.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 4

Page 5: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Classes

A class is a data type that specifies the components of andmethods available for its instances.

Java is object-oriented. Classes can extend other classes andthus inherit components and methods from other classes.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 5

Page 6: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Classes

A class is a data type that specifies the components of andmethods available for its instances.

Java is object-oriented. Classes can extend other classes andthus inherit components and methods from other classes.

Classes can implement interfaces.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 6

Page 7: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Example Class

f i n a l publ ic class MyClassextends otherPackage . YourClassimplements someInter face {. . .

}

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 7

Page 8: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Components of Classes

Component Syntax DescriptionSubclassing abstract / final must/cannot be extendedAccess public (or none) available outside of package

(or not)Class name class name Class name, same as file nameExtends extends name Name of super-classImplements implments list Implemented interfacesBody enclosed in braces Data fields and methods

for the class

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 8

Page 9: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Example Data Fields

publ ic s t a t i c f i n a l i n t UPPER LIMIT ;pr iva te i n t i n t e r n a l c o u n t e r ;protected t rans ien t i n t volume ;

s t a t i c v o l a t i l e i n t g loba l coun te r ;

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 9

Page 10: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Data Access Modifiers

public : available wherever the class is available

private : only available within the class

protected : available in subclasses and within same package

none : available within the same package

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 10

Page 11: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Data Use Modifiers

static : only one data field available for all instances

final : value cannot be modified (constant)

transient : value not persistent when storing object (notused in this module)

volatile : value can be accessed by multiple threadsconcurrently (not used in this module)

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 11

Page 12: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Method Example

publ ic s t a t i c i n t max( i n t x , i n t y ) {i f ( x > y ) {

return x ;} else {return y ;}

}

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 12

Page 13: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Method Use Modifiers

static : invoked on the class, not on the instances; canonly refer to static data fields

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 13

Page 14: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Method Use Modifiers

static : invoked on the class, not on the instances; canonly refer to static data fields

final : method cannot be overridden in a subclass

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 14

Page 15: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Method Use Modifiers

static : invoked on the class, not on the instances; canonly refer to static data fields

final : method cannot be overridden in a subclass

abstract : method must be overridden in every subclass

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 15

Page 16: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Method Use Modifiers

static : invoked on the class, not on the instances; canonly refer to static data fields

final : method cannot be overridden in a subclass

abstract : method must be overridden in every subclass

native : method body not written in Java

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 16

Page 17: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Method Use Modifiers

static : invoked on the class, not on the instances; canonly refer to static data fields

final : method cannot be overridden in a subclass

abstract : method must be overridden in every subclass

native : method body not written in Java(how can that be?)

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 17

Page 18: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Method Use Modifiers

static : invoked on the class, not on the instances; canonly refer to static data fields

final : method cannot be overridden in a subclass

abstract : method must be overridden in every subclass

native : method body not written in Java(how can that be?)

synchronized : method can be run by only one thread ofcontrol at a time

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 18

Page 19: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Method Use Modifiers

static : invoked on the class, not on the instances; canonly refer to static data fields

final : method cannot be overridden in a subclass

abstract : method must be overridden in every subclass

native : method body not written in Java(how can that be?)

synchronized : method can be run by only one thread ofcontrol at a time(what are threads?)

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 19

Page 20: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Invoking a Method

c lass I n t U t i l {. . .p u b l i c vo id p r i n t L a r g e s t ( i n t a , i n t b , i n t c ) {

i n t largerAB = max( a , b ) ;/ / i n t largerAB = I n t U t i l . max( a , b ) ;i n t l a r g e s t = max( largerAB , c ) ;System . out . p r i n t l n ( l a r g e s t +

” i s the l a r g e s t . ” ) ;}. . .

}

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 20

Page 21: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Parameter Passing

Java uses pass-by-value parameter passing.

publ ic s t a t i c void t ryChanging ( i n t a ) {a = 1;return ;

}. . .i n t b = 2;tryChanging ( b ) ;System . out . p r i n t l n ( b ) ;

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 21

Page 22: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Parameter Passing with Objects

publ ic s t a t i c void t ryChanging ( SomeObject ob j ) {obj . someField = 1;ob j = new SomeObject ( ) ;ob j . someField = 2;return ;

}. . .SomeObject someObj = new SomeObject ( ) ;t ryChanging ( someObj ) ;System . out . p r i n t l n ( someObj . someField ) ;

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 22

Page 23: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

MethodsParameter PassingIdentifiers

Identifiers

Java is a typed language.All identifiers must be declared with a type.

f l o a t rad ius ;SomeObject ob j ;

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 23

Page 24: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

1 Classes and Objects

2 Data TypesPrimitive Data TypesArraysUseful Java ClassesExceptionsText I/O

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 24

Page 25: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Wrapper Types

Every primitive data type comes with a corresponding wrappertype, representing objects that hold values of the primitive type.

i n t x = 9;I n tege r i n t O b j e c t = new I n t ege r ( x ) ;System . out . p r i n t l n ( ” Value i s ” +

i n t O b j e c t . i n tVa lue ( ) ) ;

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 25

Page 26: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Primitive Data Types

Category Data Type Wrapper ClassBoolean boolean BooleanCharacter char CharacterInteger byte Byte

short Shortint Integerint Integer

Floating point float Floatdouble Double

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 26

Page 27: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Autoboxing and Auto-unboxing

I n t ege r i n t O b j e c t = 9 ;

i n t x = i n t O b j e c t + 1 ;

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 27

Page 28: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

References

All identifiers that do not represent primitive data types arereferences to objects.

A reference value null indicates that the identifier currently hasno object to refer to.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 28

Page 29: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Overloading of Operators and Methods

Operators such as +, ∗ etc are “overloaded”; they can work onmultiple types and return corresponding values:

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 29

Page 30: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Overloading of Operators and Methods

Operators such as +, ∗ etc are “overloaded”; they can work onmultiple types and return corresponding values:

5 + 5 returns the integer 10.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 30

Page 31: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Overloading of Operators and Methods

Operators such as +, ∗ etc are “overloaded”; they can work onmultiple types and return corresponding values:

5 + 5 returns the integer 10.

5.0 + 5.0 returns the float 10.0.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 31

Page 32: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Overloading of Operators and Methods

Operators such as +, ∗ etc are “overloaded”; they can work onmultiple types and return corresponding values:

5 + 5 returns the integer 10.

5.0 + 5.0 returns the float 10.0.

The method println is also overloaded to accept int , String,float , etc.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 32

Page 33: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Type Promotion

If arithmetic operators are applied to numerical values ofdifferent type, promotion happens according toint → long → float → double

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 33

Page 34: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Type Promotion

If arithmetic operators are applied to numerical values ofdifferent type, promotion happens according toint → long → float → double

Example: 10 + 5.0 results in float 15.0.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 34

Page 35: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

String Conversion

The operator + (and only +) is compiled such that oneargument is converted to a string using toString () as soon asthe other argument is of type String.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 35

Page 36: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Arrays

Array declaration

f i n a l i n t DAYS PER WEEK = 7;double [ ] maxTemps = new double [DAYS PER WEEK ] ;

Array access

System . out . p r i n t l n ( ” Monday value : ” +maxTemps [ 1 ] ) ;

Declaration with initializer list

double [ ] weekDayTemps= {2.0 , 71.5 , 1 .8 , 75.0 , 88 .3} ;

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 36

Page 37: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Multidimensional Arrays

Declaration

f i n a l i n t DAYS PER WEEK = 7;f i n a l i n t WEEKS PER YEAR = 52;double [ ] minTemps

= new double [DAYS PER WEEK ] [ WEEKS PER YEAR ] ;

Access: minTemps[3][3]

Multidimensional initializer list

i n t [ ] [ ] x = {{1 ,2 ,3} ,{4 ,5 ,6}} ;

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 37

Page 38: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

The Object Class

A class A cannot extend another class B that directly orindirectly extends A.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 38

Page 39: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

The Object Class

A class A cannot extend another class B that directly orindirectly extends A.

Classes that do not have extends implicitly extend Object.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 39

Page 40: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

The Object Class

A class A cannot extend another class B that directly orindirectly extends A.

Classes that do not have extends implicitly extend Object.

Theorem

The methods of Object are available for all objects.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 40

Page 41: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

The Object Class

A class A cannot extend another class B that directly orindirectly extends A.

Classes that do not have extends implicitly extend Object.

Theorem

The methods of Object are available for all objects.

Can you prove this theorem?

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 41

Page 42: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Methods of Class Object

publ ic boolean equals ( Object ob j ) / / e q u a l i t y o f r e f

protected void f i n a l i z e ( ) / / f o r garbage c o l l e c t i o n

What is garbage collection?

publ ic i n t hashCode ( ) / / generate a code f o r hashing

What is hashing?

publ ic S t r i n g t o S t r i n g ( ) / / s t r i n g rep resen ta t i on

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 42

Page 43: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Class String

Objects of class String are immutable sequences of characters.Literal strings are instances of String.

publ ic i n t compareTo ( S t r i n g s )/ / negat ive i f the s t r i n g comes a f t e r s/ / 0 i f equal and p o s i t i v e i f before s

publ ic S t r i n g subs t r i ng ( i n t x , i n t y )/ / take subs t r i ng s t a r t i n g a t p o s i t i o n x/ / u n t i l but exc lud ing p o s i t i o n y

publ ic i n t indexOf ( S t r i n g s , i n t x )/ / r e t u r n index o f f i r s t occurrence of s/ / s t a r t l ook ing a t p o s i t i o n x

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 43

Page 44: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Exceptions

Exceptional situations abandon the current executioncontext, for example division by zero.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 44

Page 45: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Exceptions

Exceptional situations abandon the current executioncontext, for example division by zero.

What is “execution context”?

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 45

Page 46: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Exceptions

Exceptional situations abandon the current executioncontext, for example division by zero.

What is “execution context”?

An Exception object is associated with the situation.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 46

Page 47: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Exceptions

Exceptional situations abandon the current executioncontext, for example division by zero.

What is “execution context”?

An Exception object is associated with the situation.

A “catcher” of exceptions can be installed so that executioncan be resumed.

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 47

Page 48: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Example

s t a t i c i n t d i v i d e ( i n t x , i n t y ) {return x / y ;

}

. . .i n t x = computeSomething ( . . . ) ;i n t y = computeSomethingElse ( . . . ) ;showToUser ( d i v i d e ( x , y ) ) ;

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 48

Page 49: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Example

i n t x = computeSomething ( . . . ) ;i n t y = computeSomethingElse ( . . . ) ;t r y {

showToUser ( d i v i d e ( x , y ) ) ;} catch ( A r i thmet i cExcep t ion e ) {

showToUser ( ” The dura t i on must be ” +” a t l e a s t one day ” ) ;

}

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 49

Page 50: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Throwing Exceptions

The programmer can define his/her own exceptions byextending the class Exception.

The keyword throw can be used to generate suchuser-defined exceptions.

throw new PercentageException (” percentage exceeds 100 ” ) ;

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 50

Page 51: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Input using java. util .Scanner

import java . u t i l . Scanner ;. . .i n t sum=0;Scanner kbInput = new Scanner ( System . i n ) ;i n t nextValue = kbInput . n e x t I n t ( ) ;while ( nextValue > 0) {

sum += nextValue ;nextValue = kbInput . n e x t I n t ( ) ;

}kbInput . c lose ( ) ;

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 51

Page 52: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Classes and ObjectsData Types

Primitive Data TypesArraysUseful Java ClassesExceptionsText I/O

Output using System.out

System.out provides println and printf . Examples:

System . out . p r i n t l n ( ” The answer i s ” + answer ) ;

S t r i n g name = ” Jamie ” ;i n t x = 5 , y = 6;i n t sum = x + y ;System . out . p r i n t f ( ”%s , %d + %d = %d ” ,

name, x , y , sum ) ;

CS1102S: Data Structures and Algorithms 02 A Object-oriented Programming in Java 52

Page 53: 02 A Object-oriented Programming in Java - NUS Computingcs1102s/slides/slides_02_A.color.pdf · Classes and Objects Data Types 02 A Object-oriented Programming in Java CS1102S: Data

Recommended