Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Methods and What You Need to Know for the AP Exam

Similar presentations


Presentation on theme: "The Methods and What You Need to Know for the AP Exam"— Presentation transcript:

1 The Methods and What You Need to Know for the AP Exam
String Operations The Methods and What You Need to Know for the AP Exam

2 The Java String Class Java provides a number of “String Type” classes, but the only one you need to know for the AP Exam is the String class. One other class that is available in Java, for example, is the StringBuffer class. Objects of the String class are immutable, which means that once a String object is constructed and initialized … it cannot be changed. However, you can construct another String object from that one using a part of it or all of it plus additional string values and then re-reference the new String object with your string variable.

3 java.lang.String The String class is part of the java.lang package. This package is always automatically imported into every Java program, so there is no need to import it using … import java.lang.String; One reason the College Board chose for you to know the String class is because String objects are immutable and that makes them safe. You may not have enough experience with them to understand why they are safe, but assume for the present time that since they are immutable that they cannot be accidentally changed.

4 Constructing String Objects
It is not necessary to call the String constructor to create a String object and reference it with a variable. Consider the following: String str = “Hello World”; This line of code constructs a new String object whose contents are “Hello World”. The String object is referred to by the variable str. The code: String name = reader.nextLine(); constructs a String object from the input from the keyboard and the String objects is then referenced by name.

5 String Operations You Know
length() - returns the number of characters in the string object. trim() - makes a copy of the contents of the String object but removes the leading and trailing blank spaces. equals() - checks the contents of two String objects to see if they contain exactly the same characters.

6 The length() Method length() - returns the number of characters in the string object. If we have … String str = “Hello World”; System.out.println( str.length() ); The number 11 is printed. The blank space between Hello and World is counted.

7 The trim() Method trim() - makes a copy of the contents of the String object but removes the leading and trailing blank spaces. If we have … String name = reader.nextLine(); name = name.trim(); The last line of code makes a new String object and copies a trimmed version of the string value in the original String object. The variable name is then set to reference the new String object leaving the old String object to be garbage collected.

8 The equals() Method equals() - checks the contents of two String objects to see if they contain exactly the same characters. If we have … String word1 = “Java”; String word2 = “Java”; There are two different String objects that both contain the string values “Java” and a call to the equals method returns true as in … if ( word1.equals(word2) ) ….

9 The String Subset of Methods
There are many methods in the String class if you look up its API on line. However, we only want to work with a few methods of the String class … those in particular that you are expected to know for the AP Exam. You might not receive full credit on a free response coding question if you try to use methods other than the ones specified by the College Board. The specified methods and what they do are listed on the next slide.

10 The String Subset of Methods
String method What the Operation Does int length ( ) returns the logical size of the list String substring (int from, int to) returns the substring beginning at from and ending at to-1 (that’s to minus 1) Throws an IndexOutOfBoundsException if from is negative, or to is larger than the length of this String object, or from is larger than to. The length of the substring is (to - from). String substring (int from) returns substring ( from, length() ) Throws an IndexOutOfBoundsException if from is negative or larger than the length of this String object. int indexOf (String str) returns the index of the first occurrence of str; returns -1 if not found int compareTo (String other) returns a value < 0 if this is less than other returns a value = 0 if this is equal to other returns a value > 0 if this is greater than other Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

11 String substring (int from, int to)
The following code retrieves a substring from a beginning index (from) up to but not including the ending index (to) … Assume the code: String word = “JavaIsCool”; String phrase1 = word.substring(0, 4); stores “Java” in phrase1 String phrase2 = word.substring(4, 6); stores “Is” in phrase2 String phrase3 = word.substring(6, 10); stores “Cool” in phrase3 Note: for phase3 it is possible to use 10 for the second parameter, which is the first index value after the last character “l’ in the string. In other words, “JavaIsCool” is stored in indices 0 through 9 with the “l” in index 9. Using 10 is OK, but using 11 produces a StringIndexOutOfBoundsException. Also, note that to retrieve the first character of word, you can use String firstLetter = word.substring(0, 1);

12 String substring (int from)
The following code retrieves a substring from a beginning index represented by from … up to the end of the string including the last character. Assume the code: String word = “JavaIsCool”; The next line stores “JavaIsCool” in phrase4. String phrase4 = word.substring(0); The next line stores “IsCool” in phrase5. String phrase5 = word.substring(4); The next line stores “Cool” in phrase6. String phrase6 = word.substring(6);

13 int indexOf (String str)
The following code finds the index of the starting point of a substring inside of the string “JavaIsCool”. Assume the code: String word = “JavaIsCool”; The next line stores 0 in index1 . int index1 = word.indexOf(“Java”); The next line stores 4 in index2. int index2 = word.indexOf(“IsCool”); The next line stores 6 in index3. int index3 = word.indexOf(“Cool”); Think of indexOf as doing a search!

14 int compareTo (String other)
The following code compares different string values. String word1 = reader.nextLine(); String word2 = reader.nextLine(); if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2);

15 int compareTo (String other)
If word1 comes before word2 (alphabetically), then a negative int is returned. If word1 comes after word2, then a positive int is returned. If word1 exactly equals word2, the zero is returned. We use compareTo usually when we want to sort strings. if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2);

16 int compareTo (String other)
Example code: String word1 = “apple”; String word2 = “banana”; if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2); Output: “apple comes before banana”

17 int compareTo (String other)
Example code: String word1 = “kiwi”; String word2 = “banana”; if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2); Output: “kiwi comes after banana”

18 int compareTo (String other)
Example code: String word1 = “banana”; String word2 = “banana”; if (word1.compareTo (word2) < 0) System.out.println(word1 + “ comes before ” + word2); else if (word1.compareTo (word2) > 0) System.out.println(word1 + “ comes after ” + word2); else if (word1.compareTo (word2) == 0) System.out.println(word1 + “ is equal to ” + word2); Output: “banana is equal to banana”


Download ppt "The Methods and What You Need to Know for the AP Exam"

Similar presentations


Ads by Google