Quiz this Thursday Open written notes and handouts












![public class First. String { public static void main (String[] args) { String my. public class First. String { public static void main (String[] args) { String my.](https://slidetodoc.com/presentation_image_h2/a6b9ff94db400645b08c4d77f010ed45/image-13.jpg)




- Slides: 17
Quiz this Thursday. Open written notes and handouts. Strings and Things Goals Understand how to create and compare Strings.
Learning Strategy • Note taking • During the discussion, create an outline for today’s ideas • Break it down by command • Include an example • Jot down questions in the side margin.
Variables in Programs • Declare the variables inside the main subroutine. • You can also declare variables where you need them. (Declare a looping variable at the loop) – int number. Of. Students; – String name; – double x, y; – boolean is. Finished;
String Objects • Unlike int, boolean and double, String is a class, and when you declare a String variable, you are declaring an object. • The String part of the object is constant, their values cannot be changed after they are created. (It can be copied, replaced, …) • Declarations – String name = “Smith”; • name is a pointer to an object that holds the name “Smith” and different methods. – String river = “Mississippi”; • river is a pointer to an object that holds the name “Missippi” and different methods. – String name 2; • name 2 is a pointer to an uninitialized object, to SOP is to crash. – String name 3 = null; • name 3 is a pointer to null. To SOP is to show NULL – name 3 = “Bob”; Moves the name 3 to point to a String object with the name “Bob”.
Some String Methods We’ll take a little closer look at each of these. • . length(); – Returns the length (number of characters) of the string, – int size = name. length(); • . equals(); // Inherited (extended) from the Object class – if (name. equals(name 2)) • . compare. To(); //Inherited (implemented) from Comparable – if (name. compare. To(name 2) < 0) • . substring(); • – Returns a part of a string. – name 2 = name 1. substring(3); – name 3 = name 1. substring(2, 2); . index. Of() – name. index. Of(substring, begin. Index) – int spot = name. index. Of(“Mr. ”); – int spot 2 = name. index. Of(last. Name, 3);
int size = name. length(); • Returns the length of the strength. – String name = “Smith”; • name is a pointer to an object that holds the name “Smith” and different methods. • Example • int ans = name. length();
if name. equals(name 2) • Returns a true if name equals name 2 – String name = “Smith”; • name is a pointer to an object that holds the name “Smith” and different methods. – String name 2 = “Jones”; • if (name. equals(name 2)) – System. out. println(“The names are the sames”); • What about… • if (name == name 2) • Since name and name 2 are both objects, this compares the addresses of the objects, not the values of the objects.
if name. compare. To(name 2) • This compares the values of the name object with the value of the name 2 object with the following values returned. • If name comes before name 2, a negative value is returned. • If name comes after name 2, a positive value is returned. • If name and name 2 are an exact match, it returns 0.
Review • What is a String? • Name three String methods? • What is different between null and nothing? • How do you compare strings?
name. substring(n) • • Note: The first character is at position 0. Copies from position n to the end. String name = “West Salem Titans”; String name 2 = name. substring(5); Sets name 2 to “Salem Titans”; String name 3 = name. substring(n, m); • Copies from position n to position (m-1). • name 3 = name. substring(5, 10); • Sets name 3 to “Salem”
name. index. Of(substring) • int ans = name. index. Of(substring); • Returns the first index (position) of substring in the string. • String name = “West Salem Titans”; • int pos = name. index. Of(‘e’); • Sets pos to 1, the index of the first occurrence of “e”
name. index. Of(substring, begin. Index) • int ans = name. index. Of(substring, begin. Index); • Returns the first index of substring in the string, starting at the begin. Index and returns -1 if the substring is not in the string. • String name = “West Salem Titans”; • int pos = name. index. Of(‘e’, 3); • Sets pos to 8, the index of the first occurrence of ‘e’ when you start checking at position 3.
public class First. String { public static void main (String[] args) { String my. Sentence = "this is a trial"; String any. String = " "; System. out. println(my. Sentence); System. out. println(any. String); my. Sentence +="Java is great. "; System. out. println(my. Sentence); any. String+= " West Salem High School"; System. out. println(any. String); my. Sentence = 6 + my. Sentence; System. out. println(my. Sentence); my. Sentence = my. Sentence + 6; System. out. println(my. Sentence); Dry Run the following.
if (my. Sentence. equals(any. String)) System. out. println("The same. "); else System. out. println("Different. "); if (my. Sentence. compare. To(any. String)<0) System. out. println("First less"); else if (my. Sentence. compare. To(any. String)==0) System. out. println("Equal"); else System. out. println("First is more. "); for (int pos = 0; pos<my. Sentence. length(); pos++) System. out. println(my. Sentence. substring(pos, pos+1)); System. out. println(my. Sentence. substring(15)); System. out. println("s occurs in the #" + my. Sentence. index. Of('s') + " position");
Strings review • Strings are Objects (Variables with methods) • Use a capital ‘S’ when declaring as a String. • Some common String methods.
Review • • • Write a summary of today’s topics What have you learned? New methods What makes sense What doesn’t make sense Questions
String Program Options • Find last name – Input first and last name into one String – Output only the last name. – Push: Support middle names • Input a word, output whether it is a palindrome. (Is spelled the same forward and backward. ) – Examples of Palindromes • evilolive • racecar • English to Pig-Latin translation program. – The rules of Pig Latin are • If the word begins with a consonant -- such as ``string, '' ``Latin'' -- divide the word at the first vowel, swapping the front and back halves and append ``ay'' to the word -- ``ingstray, '' ``atin. Lay. '' • If the word begins with a vowel -- such as ``am, '' ``are, '' ``i'' -- append ``yay'' to the word -- ``amyay, '' ``areyay, '' ``iyay. '' • If the word has no vowels (other than 'y') -- such as ``my, '' ``thy'' -- append ``yay'' to it -- ``myyay, '' ``thyyay. '' – Level 1, Translate a single word – Level 2: Translate a sentence.