Programming 2 CS112- Lab 2

Programming 2   CS112- Lab 2 Programming 2   CS112- Lab 2 - Start

Added : 2017-04-01 Views :36K

Download Presentation

Programming 2 CS112- Lab 2




Download Presentation - The PPT/PDF document "Programming 2 CS112- Lab 2" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.



Presentations text content in Programming 2 CS112- Lab 2

Slide1

Programming 2 CS112- Lab 2Java

TA: Nouf Al-Harbiwww.acadox.com/class/14347 nouf200@hotmail.com

Slide2

Lab Objectives

The String Class Applications:String.Methods of the String Class.Exercises.

Lab4_1, Programming 2

2

Slide3

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.

Lab4_1, Programming 2

3

Slide4

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

Lab4_1, Programming 2

4

Slide5

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

Lab4_1, Programming 2

5

Slide6

Trace Code

Lab4_1, Programming 2

6

String s = "Java";

s = "HTML";

Slide7

Another Example of Immutability

String str1 = “Hello”;String str2 = “Hello”;

Lab4_1, Programming 2

7

Χ

Slide8

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:

Lab4_1, Programming 2

8

Slide9

Examples

s1 == s2 is false s1 == s3 is true

Lab4_1, Programming 2

9

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.

Slide10

ExampleDescriptionMethod 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 ";

Lab4_1, Programming 2

10

Slide11

String/Character Methods

String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm ";

Lab4_1, Programming 2

11

Example

Description

Method

int

comp=

str.compareTo

(str2);

int

comp2=

str.compareTo

IngoreCase

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

Slide12

ExampleDescriptionMethod 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 ";

Lab4_1, Programming 2

12

Slide13

ExampleDescriptionMethod 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 ";

Lab4_1, Programming 2

13

Slide14

ExampleDescriptionMethodint 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 ";

Lab4_1, Programming 2

14

Slide15

ExampleDescriptionMethod 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 ";

Lab4_1, Programming 2

15

Slide16

ExampleDescriptionMethod 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 ";

Lab4_1, Programming 2

16

Slide17

ExampleDescriptionMethod String upCase=str.toUpperCase(); String lowCase=str.toLowerCase();returns a new String, equivalent to the Upper/lower case of this StringString toUpperCase()String toLowerCase()

String/Character Methods

String str="I Love Java"; String str1= "I Love Java"; String str2=" Programm ";

Lab4_1, Programming 2

17

Slide18

ExampleDescriptionMethodboolean 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 ";

Lab4_1, Programming 2

18

Slide19

DescriptionMethodReturns 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 StringString toUpperCase()String toLowerCase()

Lab4_1, Programming 2

19

Slide20

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”

Print out the

length

of the string.

Use

concat

method which concatenates s1 to s2.

Use

CharAt

method which returns the character of s1 at index 1

Convert all s1 alphabets to

capital letters

and print out the result.

Convert all s1 alphabets to

lower-case

letters and print out the result.

Slide21

Lab4_1, Programming 2

21

Example of String Operations

Use

equals

method which compare s1 to s2.

Use

equalsIgnoreCase

method which compare s1 to s2 ignoring case consideration.

Use

CompareTo

method which compare s1 to s2.

Use

Substring

method which extracting substring from s1.

Slide22

Lab4_1, Programming 2

22

Constructing two strings

Slide23

Using Length method which returns the length of s1

Lab4_1, Programming 2

23

Slide24

Using

concat

method which concatenates s1 to s2

Lab4_1, Programming 2

24

Slide25

Using

CharAt

method which returns the character of s1 at index 1

Lab4_1, Programming 2

25

Slide26

Using

toUpperCase

method which converts all the character of s1 to uppercase

Lab4_1, Programming 2

26

Slide27

Lab4_1, Programming 2

27

Using

toLowerCase

method which converts all the character of s1 to lowercase

Slide28

Using equals

method which compare s1 to s2

Lab4_1, Programming 2

28

Slide29

Using

equalsIgnoreCase

method which compare s1 to s2 ignoring case consideration

Lab4_1, Programming 2

29

Slide30

Using

CompareTo

method which compare s1 to s2

Lab4_1, Programming 2

30

Slide31

Lab4_1, Programming 2

31

Using Substring

method which

extracting substring from s1

Slide32

The Output

Lab4_1, Programming 2

32

Slide33

Exercise1:

Phone keypads

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

Lab4_1, Programming 2

33

Slide34

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.

Lab4_1, Programming 2

34

Slide35

Output

Lab4_1, Programming 2

35

Slide36

Thank you

Any question

Lab4_1, Programming 2

36


About DocSlides
DocSlides allows users to easily upload and share presentations, PDF documents, and images.Share your documents with the world , watch,share and upload any time you want. How can you benefit from using DocSlides? DocSlides consists documents from individuals and organizations on topics ranging from technology and business to travel, health, and education. Find and search for what interests you, and learn from people and more. You can also download DocSlides to read or reference later.
Youtube