CSC186 Object Oriented Programming: Topic 1
CSC186 Object Oriented Programming: Topic 1
OBJECT ORIENTED
PROGRAMMING
Topic 1:
Introduction To OOP (Part 2)
TOPIC COVERED
Array of primitive
Predefined packages
CHARACTER
Declaration
Declarationand and
char ch1, ch2 = ‘X’;
initialization
initialization
System.out.print("ASCII code of
character X is " + (int) 'X' ); Type
Typeconversion
conversion
between
betweenint
intand
and
char.
char.
System.out.print("Character with
ASCII code 88 is " + (char)88 );
This
Thiscomparison
comparison
returns
returnstrue
true
‘A’ < ‘a’ because
becauseASCII
ASCII
value
value of 'A'isis65
of 'A' 65
while
while that of ‘a'isis
that of ‘a'
97.
97.
STRING
A stringis a sequence of characters that is treated as a
single value.
Instances of the String class are used to represent
strings in Java.
There are close to 50 methods defined in the String
class. Example: substring, length, and indexOf.
More:
http://docs.oracle.com/javase/6/docs/api/java/lang/Stri
ng.html
STRING
More examples:
String:“The Mentalist”
Position of ‘M’: ?
Position of second ‘e’: ?
Position of ‘ ’: ?
Length of the String: ?
STRING
String Indexing
The
Theposition,
position,ororindex,
index,ofof
the
thefirst
firstcharacter
characterisis0.
0.
SUBSTRING( )
“so”
text.substring(6,8)
“Espresso”
text.substring(0,8)
“spre”
text.substring(1,5)
“”
text.substring(3,3)
error
text.substring(4,2)
LENGTH( )
5
str1.length( )
4
str2.length( )
0
str3.length( )
1
str4.length( )
INDEXOF( )
Assume str and substr are String objects and properly
initialized.
str.indexOf(substr) will return the first position substr
occurs in str.
If str is “programming” and substr is “gram” , then
str.indexOf(substr) will return 3 because the position of the
first character of substr in str is 3.
If substr does not occur in str, then –1 is returned.
The search is case-sensitive.
EXAMPLE
String str;
str = “I Love Java and Java loves me.” ;
3 7 21
7
str.indexOf( “J” )
21
str.indexOf( “love” )
3
str.indexOf( “ove” )
-1
str.indexOf( “Me” )
CONCATENATION
“JonJava”
str1 + str2
“Jon Java”
str1 + “ “ + str2
“Java, Jon”
str2 + “, “ + str1
“Are you Jon?”
“Are you “ + str1 + “?”
STRING
http://docs.oracle.com/javase/7/docs/api/java/lang/String
.html
Array Creation
Example
Variation 1
double[ ] rainfall;
rainfall = new double[12];
Variation 2
double rainfall [ ];
rainfall = new double[12];
ACCESSING INDIVIDUAL ELEMENTS
Individual elements in an array accessed with the indexed
expression.
double[] rainfall = new double[12];
rainfall
0 1 2 3 4 5 6 7 8 9 10 11
rainfall[2] This
Thisindexed
indexedexpression
expression
refers
refers to the elementatat
to the element
The index of the first position
position#3 #3
position in an array is 0.
ARRAY PROCESSING – SAMPLE1
double[] rainfall = new double[12]; The
Thepublic
publicconstant
constant
length
lengthreturns
returnsthe
the
double annualAverage,sum = 0.0; capacity
capacityof
ofan
anarray.
array.
int[] number = { 2, 4, 6, 8 };
number.length 4
samplingData.length 9
monthName.length 12
VARIABLE-SIZE DECLARATION
int size;
int[] number;
size=
Integer.parseInt(JOptionPane.showInputDialog(null,
"Size of an array:"));
Benefits :-
Package can contain hidden classes that are used by the package
but not visible or accessible outside the package.
Classes in a package can have fields and methods that are visible by
all classes within the same package but not outside.
Different packages can have classes with the same name.
Example : java.awt.Frame and java.photo.Frame
java.lang package
Most common Math class methods : pow(x,y), sqrt(x),
round(x), PI
Example : AreaCircle = Math.PI * Math.pow(r,2);
java.util package
javax.swing package
END OF LECTURE
TEST YOURSELF 4. Find element/value for
char array[3] and first
1. Declare an array of char. String using charAt(3).
Initialize with value:
‘p’ ‘r’ ‘o’ ‘g’ ‘r’ ‘a’ ‘m’ ‘m’ ‘i’ ‘n’ 5. Substring first String with
‘g’ (0,7)
6. Compare from (5) with
2. Declare two Strings with second String. Display
value : “Same word” if true. –
i. “programming” using equals( ).
ii. “program”
7. Concatenate second
String and first String.
3. Find length char array in (1)
and first String in (2).
MORE READING
Topic : T1 and T2 (no coding yet) Topic : T1 and T2 (no coding yet)
END.
Thank you.