36
PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI WWW.ACADOX.COM/CLASS/14347 [email protected]

PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI [email protected]

Embed Size (px)

Citation preview

Page 1: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

PROGRAMMING 2 CS112- LAB 2JAVA

TA: NOUF AL-HARBI

. . / /14347WWW ACADOX COM CLASS

[email protected]

Page 2: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 2

LAB OBJECTIVES

The String Class Applications:String.

Methods of the String Class.

Exercises.

Page 3: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 3

STRING

String is a sequence of characters enclosed in quotes. E.g. “Hello”

Once a String object is created it cannot be changed. Stings are Immutable.

To get changeable strings use the class called StringBuffer.

String and StringBuffer classes are declared final, so there cannot be subclasses of these classes.

Page 4: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 4

HOW TO CREATE A STRING

String newString = new String(stringLiteral);

String s1= new String("Welcome to Java");

Since strings are used frequently, Java provides a shorthand initializer for creating a string:

String s1= "Welcome to Java";

Page 5: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 5

STRINGS ARE IMMUTABLE

A String object is immutable: its content cannot be changed.

Does the following code changes the content of the string?

String s = "Java";

s = "HTML";

Page 6: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 6

TRACE CODE

String s = "Java";

s = "HTML";

: String

String object for "Java"

s

After executing String s = "Java";

After executing s = "HTML";

: String

String object for "Java"

: String

String object for "HTML"

Contents cannot be changed

This string object is now unreferenced

s

Page 7: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 7

ANOTHER EXAMPLE OF IMMUTABILITY

String str1 = “Hello”;

String str2 = “Hello”;

√Χ

Page 8: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 8

INTERNED STRINGS

Since strings are immutable and are frequently used, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence.

Such an instance is called interned.

For example, the following statements:

Page 9: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 9

EXAMPLES

s1 == s2 is false s1 == s3

is true

• A new object is created if you use the new operator.

• If you use the string initializer, no new object is created if the interned object is already created.

String s1 = "Welcome to Java"; String s2 = new String("Welcome to Java"); String s3 = "Welcome to Java"; System.out.println("s1 == s2 is " + (s1 == s2)); System.out.println("s1 == s3 is " + (s1 == s3));

: String

Interned string object for "Welcome to Java"

: String

A string object for "Welcome to Java"

s1

s2

s3

Page 10: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 10

Example Description Method

char x=str.charAt(2); Returns the character at the specified index.

char charAt(int index)

STRING/CHARACTER METHODS

String str="I Love Java";

String str1= "I Love Java";

String str2=" Programm ";

Page 11: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 11

STRING/CHARACTER METHODS

String str="I Love Java";

String str1= "I Love Java";

String str2=" Programm ";

Example Description Method

int comp=str.compareTo(str2);int comp2=str.compareToIngoreCase (str2);

Compares this String to another Object or String.returns +ve number, 0, or -ve number if this String is greater than, equal to or less than s.

int compareTo(String s) int compareToIngoreCase(s)

Page 12: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 12

Example Description Method

String str2=" Programm "; String res=str.concat(str2);

Concatenates the specified string to the end of this string.

String concat(String str)

STRING/CHARACTER METHODS

String str="I Love Java";

String str1= "I Love Java";

String str2=" Programm ";

Page 13: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 13

Example Description Method

boolean isEqual=str.equals(str); Compares this string to the specified object.returns true if s the same as this String.

boolean equals(Object anObject)

STRING/CHARACTER METHODS

String str="I Love Java";

String str1= "I Love Java";

String str2=" Programm ";

Page 14: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 14

Example Description Method

int len=str.length(); Returns the length of this string.

int length()

STRING/CHARACTER METHODS

String str="I Love Java";

String str1= "I Love Java";

String str2=" Programm ";

Page 15: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 15

Example Description Method

String res1=str.substring(2); Returns a new string that is a substring of this string.

String substring(int beginIndex)

STRING/CHARACTER METHODS

String str="I Love Java";

String str1= "I Love Java";

String str2=" Programm ";

Page 16: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 16

Example Description Method

String res1=str.substring(2,6); Returns a new string that is a substring of this string.

String substring(int beginIndex, int endIndex)

String res1=str.substring(2,6); Returns a new string that is a substring of this string.

String substring(int beginIndex, int endIndex)

STRING/CHARACTER METHODS

String str="I Love Java";

String str1= "I Love Java";

String str2=" Programm ";

Page 17: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 17

Example Description Method

String upCase=str.toUpperCase(); String lowCase=str.toLowerCase();

returns a new String, equivalent to the Upper/lower case of this String

String toUpperCase()String toLowerCase()

STRING/CHARACTER METHODS

String str="I Love Java";

String str1= "I Love Java";

String str2=" Programm ";

Page 18: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 18

Example Description Method

boolean isLetter=Character.isLetter('r');Ex2:boolean isLetter=Character.isLetter(str.charAt(5));

determines if the specified character is a letter.

Character.isLetter(char ch)

STRING/CHARACTER METHODS

String str="I Love Java";

String str1= "I Love Java";

String str2=" Programm ";

Page 19: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 19

Description Method

Returns the character at the specified index. char charAt(int index)

Compares this String to another Object or String.returns +ve number, 0, or -ve number if this String is greater than, equal to or less than s.

int compareTo(String s) int compareToIngoreCase(s)

Concatenates the specified string to the end of this string.

String concat(String str)

Compares this string to the specified object.returns true if s the same as this String.

boolean equals(Object anObject)

Returns the length of this string. int length()

Returns a new string that is a substring of this string. String substring(int beginIndex)

Returns a new string that is a substring of this string. String substring(int beginIndex, int endIndex)

determines if the specified character is a letter. Character.isLetter(char ch)

returns a new String, equivalent to the Upper/lower case of this String

String toUpperCase()String toLowerCase()

Page 20: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 20

Example of String Operations

Using methods of the String Class.Design and implement a Java program that will do the

following operations to this string s1=“Welcome to java” s2=“Welcome to Java”

1.Print out the length of the string.2.Use concat method which concatenates s1 to s2.3.Use CharAt method which returns the character of s1 at

index 14.Convert all s1 alphabets to capital letters and print out the

result.5.Convert all s1 alphabets to lower-case letters and print out

the result.

Page 21: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 21

Example of String Operations

6.Use equals method which compare s1 to s2.7.Use equalsIgnoreCase method which compare s1 to s2

ignoring case consideration.8.Use CompareTo method which compare s1 to s2.9.Use Substring method which extracting substring from s1.

Page 22: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 22

Constructing two strings

Page 23: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 23

Using Length method which

returns the length of s1

Page 24: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 24

Using concat method which concatenates s1

to s2

Page 25: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 25

Using CharAt method which returns the

character of s1 at index 1

Page 26: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 26

Using toUpperCase

method which converts all the character of s1 to uppercase

Page 27: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 27

Using toLowerCase method which converts all the

character of s1 to lowercase

Page 28: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 28

Using equalsmethod which compare s1

to s2

Page 29: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 29

Using equalsIgnoreCasemethod which compare s1

to s2 ignoring case consideration

Page 30: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 30

Using CompareTomethod which

compare s1 to s2

Page 31: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 31

Using Substringmethod which

extracting substring from s1

Page 32: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 32

The Output

Page 33: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 33

EXERCISE1: PHONE KEYPADS

Problem Description:

The international standard letter/number mapping found on the telephone is shown below:

Page 34: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 34

EXERCISE1: PHONE KEYPADS

Write a method that returns a number, given an uppercase letter, as follows:

public static int getNumber(char uppercaseLetter)

Write a test program that prompts the user to enter a phone number as a string. The input number may contain letters. The program translates a letter (upper- or lowercase) to a digit and leaves all other characters intact.

Page 35: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 35

OUTPUT

Page 36: PROGRAMMING 2 CS112- LAB 2 JAVA TA: NOUF AL-HARBI  NOUF200@HOTMAIL.COM

Lab4_1, Programming 2 36

THANK YOU

Any question