CSC186 - 1) Introduction To OOP-Java (Part 2)
CSC186 - 1) Introduction To OOP-Java (Part 2)
OBJECT ORIENTED
PROGRAMMING
Topic 1:
Introduction To OOP (Java-2)
Topic Covered
◦ Character and String
◦ String manipulation
◦ substring( )
◦ length( )
◦ indexOf( )
◦ charAt()
◦ equals()
◦ Concatenation
◦ Array of primitive
◦ Predefined packages
CHARACTER AND
STRING
Character
◦ In Java, single characters are represented using the
data type char.
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 string is a sequence of characters that is treated as a
single value.
◦ String characteristics:
◦ Sequence of zero or more characters.
◦ Enclosed in double quotation marks “ ”.
◦ Is processed as a single unit.
◦ Null or empty strings have no characters.
◦ The first character is in position 0
String
◦ String Length and Indexing
The
Theposition,
position,ororindex,
index,ofof
the
thefirst
firstcharacter
characterisis0.
0.
STRING
MANIPULATION
SUBSTRING()
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)
STRING
MANIPULATION
LENGTH()
Length( )
5
str1.length( )
4
str2.length( )
0
str3.length( )
1
str4.length( )
STRING
MANIPULATION
INDEXOF()
IndexOf( )
◦ str.indexOf(substr) will return the first position substr occurs in
str.
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” )
STRING
MANIPULATION
CONCATENATION
Concatenation
◦ str1 + str2 will return a new string that is a concatenation of two strings.
◦ If str1 is “pro” and str2 is “gram” , then str1 + str2 will return “program”.
◦ Notice that this is an operator and not a method of the String class.
“JonJava”
str1 + str2
“Jon Java”
str1 + “ “ + str2
“Java, Jon”
str2 + “, “ + str1
“Are you Jon?”
“Are you “ + str1 + “?”
STRING
MANIPULATION
CHARAT()
charAt()
◦ This method returns the character located at the String's specified index. The string
indexes start from zero.
◦ Example :
◦ Output: a
STRING
MANIPULATION
EQUALS()
Equals()
◦ This method compares this string to the specified object.
◦ Example:
String str1 = "Nur";
if(str1.equals("Orked"))
System.out.println("SAME");
else
System.out.println("NOT SAME");
◦ Example:
String str1 = "Nur";
if(str1.equals(“nur"))
System.out.println("SAME");
else
System.out.println("NOT SAME");
◦ Example:
String str1 = "Nur";
if(str1.equalsIgnoreCase(“nur"))
System.out.println("SAME");
else
System.out.println("NOT SAME");
◦ Output : SAME
Test Yourself
1. Declare an array of char. Initialize 4. Find element/value for char
with value: array[3] and first String using
‘p’ ‘r’ ‘o’ ‘g’ ‘r’ ‘a’ ‘m’ ‘m’ ‘i’ ‘n’ ‘g’ charAt(3).
Array Creation
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 Initialization
◦ Like other data types, it is possible to declare and initialize
an array at the same time.
int[] number = { 2, 4, 6, 8 };
number.length 4
samplingData.length 9
monthName.length 12
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 size;
System.out.println("Size of an array:");
size= in.nextInt();
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.
Thank you.