Document 7519444

Download Report

Transcript Document 7519444

String & Exception

Lesson plan

• Class & Object • String • Exercise for midterm

Class & Object

• A class definition provides a description of a typical object within that class.

An individual object is an instance of a class.

• A class has its behavior (methods) and attributes (fields).

Class & Object

• Access a dynamic method or field by: . . Example: df.format(loanAmount) . Access a static method by . Example: JOptionPane. showInputDialog(null,"Loan Amount (dollars.cents):");

String

• What is it?

– A String is a sequence of characters that is treated as a single value – String class handles all operations related to String – String class is defined in java.lang

.

Create a String object

• Syntax: String ; =new String(“constant”); OR String ; = “constant”

Create a String object

• Example: String strVar; strVar = new String(“CS 172 Course”); OR String strVar; strVar = “CS 172 Course”;

Internal Representation of a String Individual characters in a string object are indexed from 0

Compute Length of a string

• Method: length() Returns the length of a string • Example: String strVar; strVar = new String(“CS 172 Course”); int len = strVar.length();

Substring

• Method: Extract a substring from a given string by specifying the beginning and ending positions strSubStr =“CS 172” • Example: String strVar, strSubStr; strVar = new String(“CS 172 Course”); strSubStr = strVar.substring(0,6);

Index position of a substring within another string

• Method: Find an index position of a substring within another string.

• Example: String strVar1 = “CS 172 Course”; String strVar2 = “Course”; int index; index = 7 index = strVar1.indexOf(strVar2);

Index position of a substring within another string

• Example: String strVar1 = “CS 172 Course”; String strVar2 = “C”; int index; index = 0 index = strVar1.indexOf(strVar2);

String concatenation

• Method: Create a new string from two strings by concatenating the two strings.

• Example: String strVar1 = “CS 172”; String strVar2 = “Course”; String sumStr; sumStr = strVar1+strVar2;

Practice exercise

Determine values of some variables given codes

String comparison

Two methods: equals equalsIgnoreCase string1.equals(string2) Or string1.equalsIgnoreCase(string2)

String comparison

Methods: equals equalsIgnoreCase compareTo isEqual= false String string1 =“CS 172”; String string2 = “172” Boolean isEqual; isEqual = string1.equals(string2);

String comparison

equalsIgnoreCase Example: isEqual= true String string1 =“COMPSCI”; String string2 = “compsci” Boolean isEqual; isEqual = string1.equals(string2);

String comparison

compareTo Example: compareResult < 0 String string1 =“Adam”; String string2 = “Brian” int compareResult; compareResult = string1.compareTo(string2);

String comparison

- string1.compareTo(string2)

Compares two strings lexicographically will return 0 if two strings are equal will return negative value if string1 is less than 2 string will return positive value if string1 is greater than string 2 The comparison is based on the Unicode value of each character in the strings

String comparison

The comparison is based on the Unicode value of each character in the strings

let k be the smallest index valid for both strings; compareTo returns the difference of the two character values at position k in the two string - that is, the value: character at the position k of string 1 – character at the position k of string 2

Character at position k of a string

Example: String sample=“CS 172 Course”; char aChar;

aChar=‘1’

aChar = sample.charAt(3)

Character at position k of a string

Example: String sample=“CS 172 Course”; char aChar;

aChar=‘1’

aChar = sample.charAt(3)

Implicit type of conversion

Implicit type conversion is common: Example: int num = 172; s=“CS 172” String s = “CS "+num;

Practice (answers) Monday (3) int sum1 = 0; for (int i=0; i<= 5; i++) for (int j=0; j<= 5; j++) { sum1+=i; } System.out.println(" Sum1 is "+ sum1); i=0 …..

j=0 -> sum1 = 0+i = 0+0 = 0 j=1 -> sum1 = 0+i = 0+0 = 0 j=5 -> sum1 = 0+i = 0+0 = 0 i=1 j=0 -> sum1 = 0+i=0+1=1 … j=1 -> sum1= 0+1+ i =0+1*2 j=5 -> sum1 = 0+1*6 = 6

Practice (answers) Monday (3) i=2 …..

j=0 -> sum1 = 6+i = 6+2*1 j=1 -> sum1 = 6+2+2= 6+2*2 j=5 -> sum1 = 6+i = 6+2*6 = 18 i=3 …..

j=0 -> sum1 = 18+3 = 18+3*1 j=1 -> sum1 = 18+3+3= 18+3*2 j=5 -> sum1 = 18+3*6= 36 i=4 …..

j=0 -> sum1 = 36+4*1 j=1 -> sum1 = 36+4*2 j=5 -> sum1 = 36+4*6= 60

SUM1= 90

i=5 …..

j=0 -> sum1 = 60+5*1 j=1 -> sum1 = 60+5*2 j=5 -> sum1 = 60+5*6= 90

Practice (answers) Wednesday (3) int sum =0; int j=0; while (j<11) { j++; int i=5; while (i>j) { sum = sum + (i+j); i--; } } System.out.println(“ Sum = “+ sum);

Practice (answers) Wednesday (3) j=0 j= 1 i=5 sum = sum +(1+5) = 6 i=4 sum = sum + (1+4) = 6+5 = 11 i=3 sum = sum + (1+3) = 11+4 = 15 i=2 sum = sum+(1+2) = 15+3 = 18

Practice (answers) Wednesday (3) j= 2 i=5 sum = sum +(2+5) = 18+7 = 25 i=4 sum = sum + (2+4) =25+ 6 = 31 i=3 sum = sum + (2+3) = 31+5= 36

Practice (answers) Wednesday (3) j= 3 i=5 sum = sum +(3+5) = 36+8 = 44 i=4 sum = sum + (3+4) =44+ 7 = 51

Practice (answers) Wednesday (3) j= 4 i=5 sum = sum +(4+5) = 51+9 = 60

Practice

Write a loop that prints out a string in reverse.

input: “CS 172” output: “271 SC”

Practice

public class StringReverse{ public static void main(String[] args) { String strSource ="CS 172"; String strDes=""; int len = strSource.length()-1; for (int i=len; i>=0; i--) strDes = strDes + strSource.charAt(i); System.out.println(" Reserve string is "+ strDes); } }

Practice

Multiple choices Determine the output of the codes

StringBuffer class

• A String object is immutable. Once it is created, we can’t add/delete/modify characters of a String object • If we need to modify the content of a string directly, we must use StringBuffer class

Create a StringBuffer object

• StringBuffer word = new StringBuffer(“Java”); word.setCharAt(0,’D’); word.setCharAt(1,’i’); word = DIVA

Create a StringBuffer object

• StringBuffer word = new StringBuffer(“Java”); word.setCharAt(0,’D’); word.setCharAt(1,’i’); word = “DIVA”

Delete a substring from a StringBuffer object • StringBuffer word = new StringBuffer(“CS172 Course”); word.delete(0,1); word = “172 Course”

Append a string

• StringBuffer word = new StringBuffer(“CS172”); word.append(“Course”); word = “172 Course”

Insert a string

• StringBuffer word = new StringBuffer(“CS Course”); word.insert(3,“172”); word = “CS 172 Course”

Practice

• Write a method to sort three strings alphabetically

Practice

• Identify objects, class • What will be the output from the following code?