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

Slides:



Advertisements
Similar presentations
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Strings in Java 1. strings in java are handled by two classes String &
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Java Strings in 10 minutes
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Fundamental Programming Structures in Java: Strings.
The String Class. Objectives: Learn about literal strings Learn about String constructors Learn about commonly used methods Understand immutability of.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Class T{ public static int id; public static int num = 5; public int n; public boolean c; public static void setNumberOfBicycles(int val) { num=val; //
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Chapter 7: Characters, Strings, and the StringBuilder.
1 Textual Data Many computer applications manipulate textual data word processors web browsers online dictionaries.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Strings Mr. Smith AP Computer Science A. What are Strings? Name some of the characteristics of strings: A string is a sequence of characters, such as.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Strings and ArrayLists April 23, 2012 ASFA AP CS.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
17-Feb-16 String and StringBuilder Part I: String.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
© A+ Computer Science - Magpie Magpie is a lab that focuses on classes, randomness, and Strings. This lab will make sure that you.
The String Class.
Object-Oriented Concepts
Computer Programming ||
Strings, StringBuilder, and Character
Strings.
EKT 472: Object Oriented Programming
Primitive Types Vs. Reference Types, Strings, Enumerations
String Objects & its Methods
A few bits of information
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
The compareTo interface
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. Every time.
Object Oriented Programming in java
Things to Remember.
16 Strings.
Lecture 07 String Jaeki Song.
String and StringBuilder
Introduction to Computer Science
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
String Class.
In Java, strings are objects that belong to class java.lang.String .
String Methods Strings have actions known as method. We will review a few of the methods associated with strings that are a part of the built in Java.
Presentation transcript:

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

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.

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.

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.

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.

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.

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.

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) ) ….

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.

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.

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

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

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!

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

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

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”

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”

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”