0% found this document useful (0 votes)
16 views

CSC186 - 1) Introduction To OOP-Java (Part 2)

Uploaded by

2022623562
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

CSC186 - 1) Introduction To OOP-Java (Part 2)

Uploaded by

2022623562
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

CSC186

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.

◦ Character constants are written as symbols enclosed


in single quotes ‘ ’.

◦ Java uses Unicode, which includes ASCII, for


representing char constants.
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 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( )

◦ Assume str is a String object and properly initialized


to a string.

◦ str.substring( i, j ) will return a new string by extracting


characters of str from position i to j-1 where 0  i 
length of str, 0  j  length of str, and i  j.

◦ If str is “programming” , then str.substring(3, 7) will


create a new string whose value is “gram”.

◦ The original string str remains unchanged.


Example

String text = “Espresso”;

“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( )

◦ str.length( ) will return the number of characters in


str.

◦ If str is “programming” , then str.length( ) will return


11 because there are 11 characters in it.
Example

String str1, str2, str3, str4;


str1 = “Hello” ;
str2 = “Java” ;
str3 = “” ; //empty string
str4 = “ “ ; //one space

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.

◦ 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” )
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.

◦ The strings str1 and str2 remains the same.


Example

String str1, str2;


str1 = “Jon” ;
str2 = “Java” ;

“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 :

String txt = "programmer";


char result = txt.charAt(5);
System.out.println(result);

◦ 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");

◦ Output : NOT SAME


Equals()
◦ This method compares this string to the specified object.

◦ Example:
String str1 = "Nur";
if(str1.equals(“nur"))
System.out.println("SAME");
else
System.out.println("NOT SAME");

◦ Output : NOT SAME


EqualsIgnoreCase()
◦ This method compares this string to the specified object.

◦ 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).

5. Substring first String with (0,7)


2. Declare two Strings with value :
i. str1 = “programming”
6. Compare (5) with second String.
ii. str2= “program”
7. Concatenate second String and first
String.
3. Find length char array in (1) and first
String in (2).
Test Yourself
Write a program to accept a person’s name and perform the
following tasks:

◦ Find the first character of the string.


◦ Check whether the name is Orked.
◦ Find the sub word for location 2 until 4.
ARRAY OF PRIMITIVE
Arrays of Primitives
 Array Declaration

<data type> [ ] <variable> //variation 1


<data type> <variable>[] //variation 2

 Array Creation

<variable> = new <data type> [ <size> ]


Arrays of Primitives
◦ 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 Initialization
◦ Like other data types, it is possible to declare and initialize
an array at the same time.

int[] number = { 2, 4, 6, 8 };

double[] samplingData = { 2.443, 8.99, 12.3, 45.009, 18.2,


9.00, 3.123, 22.084, 18.08 };

String[] monthName = { "January", "February", "March",


"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };

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.

for (int i = 0; i < rainfall.length; i++)


{
System.out.print("Rainfall for month " +
(i+1) ) );
rainfall[i] = sc.nextDouble(); //input
sum = sum + rainfall[i];
}

annualAverage = sum / rainfall.length;


Variable-size Declaration
 In Java, we are not limited to fixed-size array declaration.
 The following code prompts the user for the size of an array and declares an array of
designated size:

int size;

System.out.println("Size of an array:");
size= in.nextInt();

int[] number = new int[size];


Array Processing – Sample 2
double[] rainfall = new double[12];
String[] monthName = new String[12];
monthName[0] = "January";
The
Thesame
samepattern
patternfor
for
monthName[1] = "February"; the remaining ten
the remaining ten
… months.
months.
double annualAverage, sum = 0.0;

for (int i = 0; i < rainfall.length; i++) {


rainfall[i] = Double.parseDouble(
JOptionPane.showinputDialog(null,
"Rainfall for "
+ monthName[i] ));
sum += rainfall[i];
}
annualAverage = sum / rainfall.length; The
Theactual
actualmonth
month
name
nameinstead
insteadofofaa
number.
number.
Test Yourself (Convert into Java)
Test Yourself

Write a Java application that accepts 10 integer


numbers from the user and store into an array.
Display the number that can be divisible by 3.
PREDEFINED
PACKAGES
Predefined-package
 A package is a JAVA class library – a collection of classes and interfaces in
the same directory.

 The use of keyword import at the beginning of the JAVA application.


Example : import java.util.*;

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.

You might also like