0% found this document useful (0 votes)
27 views31 pages

CSC186 Object Oriented Programming: Topic 1

Uploaded by

2022905879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views31 pages

CSC186 Object Oriented Programming: Topic 1

Uploaded by

2022905879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

CSC186

OBJECT ORIENTED
PROGRAMMING
Topic 1:
Introduction To OOP (Part 2)
TOPIC COVERED

 Character and String


 String manipulation
substring( )
length( )
indexOf( )
concatenation

 Array of primitive
 Predefined packages
CHARACTER

 InJava, single characters are represented using the data


type char.
 Characterconstants are written as symbols enclosed in
single quotes ‘ ’.
 Charactersare stored in a computer memory using some
form of encoding.
 ASCII,
which stands for American Standard Code for
Information Interchange, is one of the document coding
schemes widely used today.
 Javauses 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 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

 Contains operations to manipulate strings.


 String:
Sequence of zero or more characters.
Enclosed in double quotation marks “ ”.
Is processed as a single unit .
Null or empty strings have no characters.
Every character in a string has a relative position
in that string , the first character is in position 0 .
STRING
 Length of the string is the number of characters in it .
 Numeric strings consist of integers or decimal
numbers.
 When determining the length of a string , blanks
count .
 Example :
 “”  Empty String has length = 0
 “abc”  has length = 3 , position of a = 0 ,b= 1 , c= 2
 “a boy”  has length = 5
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( )

 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.
 Ifstr 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)
LENGTH( )

 Assume str is a String object and properly initialized


to a string.

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


str.

 Ifstr 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( )
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

 Assume str1 and str2 are String objects and properly


initialized.
 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

 More on String API :

 http://docs.oracle.com/javase/7/docs/api/java/lang/String
.html

 Google : Java API String


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 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++)


{
rainfall[i] = Double.parseDouble(
JOptionPane.showinputDialog(null,
"Rainfall for month " + (i+1) ) );
sum += rainfall[i];
}

annualAverage = sum / rainfall.length;


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

int[] number;

size=
Integer.parseInt(JOptionPane.showInputDialog(null,
"Size of an array:"));

number = new int[size];


PREDEFINED-PACKAGE

 A package is a JAVA class library – a collection of classes and interfaces


in the same directory.

 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

 To use objects contained in a package, you need to import the package –


the use of keyword import at the beginning of the JAVA application.
Example : import java.util.*;
PREDEFINED-PACKAGE

 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

 Wu C. Thomas, “An Introduction to Object-Oriented


Programming with Java.”
 Chapter 9
 Chapter 10

Quiz 1 (3B) Quiz 1 (3E/F)

Date : 1 July 2015 (Wednesday) Date : 30 June 2015 (Tuesday)


Time : During class Time : During class
Duration : 30 minutes Duration : 30 minutes

Topic : T1 and T2 (no coding yet) Topic : T1 and T2 (no coding yet)
END.
Thank you.

You might also like