Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
BUILDING JAVA PROGRAMS CHAPTER 3 THE STRING CLASS 1 WHAT IS WRONG? Scanner console = new Scanner(System.in); System.out.print("What is your name? "); String name = console.next(); if (name == "Barney") { System.out.println("I love you, you love me,"); System.out.println("We're a happy family!"); } 2 THE EQUALS METHOD • Strings and Objects are compared using a method named equals. Scanner console = new Scanner(System.in); System.out.print("What is your name? "); String name = console.next(); if (name.equals("Barney")) { System.out.println("I love you, you love me,"); System.out.println("We're a happy family!"); } 3 INDEXES • Characters of a string are numbered with 0-based indexes: String name = "R. Kelly"; index character 0 R 1 . 2 3 K 4 e 5 l 6 l 7 y • • First character's index : 0 • The individual characters are values of type char (seen later) Last character's index : 1 less than the string's length 4 STRING METHODS Method name Description str1.indexOf(str2) index where the start of str2 appears in str1 (-1 if not found) str1.length() number of characters in this string str1.substring(index1, index2) or str1.substring(index1) the characters in this string from index1 (inclusive) to index2 (exclusive); if index2 is omitted, grabs till end of string str1.toLowerCase() a new string with all lowercase letters str1.toUpperCase() a new string with all uppercase letters • These methods are called using the dot notation: String gangsta = "Dr. Dre"; System.out.println(gangsta.length()); // 7 5 STRING METHOD EXAMPLES // index 012345678901 String s1 = "Stuart Reges"; String s2 = "Marty Stepp"; System.out.println(s1.length()); // 12 System.out.println(s1.indexOf("e")); // 8 System.out.println(s1.substring(7, 10)); // "Reg" String s3 = s2.substring(1, 7); System.out.println(s3.toLowerCase()); • // "arty s" Given the following string: // index 0123456789012345678901 String book = "Building Java Programs"; • How would you extract the word "Java" ? 6 MODIFYING STRINGS • Methods like substring and toLowerCase build and return a new string, rather than modifying the current string. String s = "lil bow wow"; s.toUpperCase(); System.out.println(s); // lil bow wow • To modify a variable's value, you must reassign it: String s = "lil bow wow"; s = s.toUpperCase(); System.out.println(s); // LIL BOW WOW • An object that can never be modified after creation is called an immutable object. Strings are immutable. 7 STRING TEST METHODS Method str1.equals(str2) str1. equalsIgnoreCase(str2) str1.startsWith(str2) str1.endsWith(str2) str1.contains(str2) Description whether two strings contain the same characters whether two strings contain the same characters, ignoring upper vs. lower case whether str1 contains str2's characters at start whether str1 contains str2's characters at end whether str1 contains str2’s characters anywhere 8 CHECKPOINT 1 • • Go into your student folder on the X drive Create a new folder named “<LastName>.fraccalc.1” • Example: “bieber.fraccalc.1” • Copy your FracCalc.java file into this folder. • Open Dropbox (on your desktop) • Go into the Period 2 folder • Copy your new <LastName>.fraccalc.1 folder into the dropbox period 2 folder. • Copy the entire <LastName>.fraccalc.1 folder (not just your .java file). 9 LAB • PracticeIt: BJP3 Self-Check 3.20 • Remember to put “” around Strings in your answers • Write a method that accepts a string parameter, and returns the string in reverse order. • Example: reverse(“Good day”)should return “yad dooG“ 10 SCANNER For most objects (including Scanner objects), we create a new instance with the new keyword: TypeName myInstance = new TypeName(any, parameters); For a Scanner, it looks like this: Scanner scanner = new Scanner(System.in); 11 A BIT MORE MAGIC: IMPORT There’s one other thing we have to do before we can start using our Scanner. We have to tell Java where it can find it! We do this with one more magic Java keyword, import, at the top of the Java source code file: import java.util.*; Eclipse will offer to do this for us if we mouse over the word “Scanner” when it has a red squiggly. 12 SCANNER We finally have enough to start using the methods on our Scanner object: import java.util.*; public class MyInteractiveProgram { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Type something: "); String word = scanner.next(); System.out.print("The first word was: " + word); } } 13 SCANNER import java.util.*; public class MyInteractiveProgram { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Type something: "); String word = scanner.next(); System.out.print("The first word was: " + word); } } What will this output? I don’t know! It depends on what you type at runtime! 14 LET’S TRY IT! Start Eclipse and create a “GeometryHelper” project and class. Use Scanner’s nextDouble() method to ask for a radius, and then print the circumference, area, and volume for a circle and sphere with that radius: What is the radius? 3 A circle with radius 3.0 has circumference 18.8495559215 A circle with radius 3.0 has area 28.27433388230 A sphere with radius 3.0 has volume 113.0973355292 𝐶𝑖𝑟𝑐𝑢𝑚𝑓𝑒𝑟𝑒𝑛𝑐𝑒 = 2𝜋𝑟 𝐴𝑟𝑒𝑎 = 𝜋𝑟 2 𝑉𝑜𝑙𝑢𝑚𝑒 = 4 3 𝜋𝑟 3 15 IN YOUR NOTEBOOK The following method signatures are missing their class. List the class to which each method belongs. double abs(double); double nextDouble(); char charAt(int); int ceil(double); int length(); String substring(int, int); 16