'F' → Fullscreen
length(), substring(), and indexOf(). equals() and compareTo(). // Figure 1 — Common String methodspublic class Program{ public static void main(String[] args) { String word = "programming";
System.out.println(word.length()); // 11 System.out.println(word.substring(0, 4)); // prog System.out.println(word.substring(3)); // gramming System.out.println(word.indexOf("g")); // 3 System.out.println(word.toUpperCase()); // PROGRAMMING }}// Figure 2 — Comparing Strings correctlypublic class Program{ public static void main(String[] args) { String a = "cat"; String b = "cat";
System.out.println(a.equals(b)); // true System.out.println(a.compareTo(b)); // 0 System.out.println(a.compareTo("dog")); // negative }}// Figure 3 — Strings are immutablepublic class Program{ public static void main(String[] args) { String word = "hello";
word.toUpperCase(); System.out.println(word); // still hello
word = word.toUpperCase(); System.out.println(word); // now HELLO }}// Figure 4 — A useful pattern with substring + indexOfpublic class Program{ public static void main(String[] args) { String fullName = "Ada Lovelace"; int spaceIndex = fullName.indexOf(" ");
String firstName = fullName.substring(0, spaceIndex); String lastName = fullName.substring(spaceIndex + 1);
System.out.println(firstName); System.out.println(lastName); }}// Task 1 — String Analyzer//// Create a String named word.// Then print:// 1) The word// 2) Its length// 3) The first character// 4) The last character// 5) The uppercase version//// Hint:// first character: substring(0, 1)// last character: substring(word.length() - 1)
public class Program{ public static void main(String[] args) { String word = "computer";
System.out.println("Word: " + word); System.out.println("Length: " + word.length()); System.out.println("First character: " + word.substring(0, 1)); System.out.println("Last character: " + word.substring(word.length() - 1)); System.out.println("Uppercase: " + word.toUpperCase()); }}// Task 2 — Initials Generator//// Create a String fullName with a first and last name.// Example: "Grace Hopper"//// Use indexOf and substring to print the initials like:// G.H.//// Steps:// 1) Find the space index// 2) Get the first letter of the first name// 3) Get the first letter of the last name// 4) Print them with periods
public class Program{ public static void main(String[] args) { String fullName = "Grace Hopper";
int spaceIndex = fullName.indexOf(" ");
String firstInitial = fullName.substring(0, 1); String lastInitial = fullName.substring(spaceIndex + 1, spaceIndex + 2);
System.out.println(firstInitial + "." + lastInitial + "."); }}// Task 3 — Username Generator//// Create a String fullName.// Generate a username using:// - the first letter of the first name// - the full last name// - all lowercase//// Example:// "Ada Lovelace" -> alovelace//// Use substring, indexOf, and toLowerCase.
public class Program{ public static void main(String[] args) { String fullName = "Ada Lovelace";
int spaceIndex = fullName.indexOf(" ");
String firstInitial = fullName.substring(0, 1); String lastName = fullName.substring(spaceIndex + 1);
String username = (firstInitial + lastName).toLowerCase();
System.out.println("Username: " + username); }}// Task 4 — Word Finder//// Create a sentence String.// Use indexOf to search for a target word.//// Print:// 1) The sentence// 2) The index of the target word// 3) A message saying whether it was found//// Remember:// indexOf returns -1 if the word is not found.
public class Program{ public static void main(String[] args) { String sentence = "The quick brown fox jumps over the lazy dog"; String target = "fox";
int index = sentence.indexOf(target);
System.out.println("Sentence: " + sentence); System.out.println("Index of target: " + index);
if (index == -1) { System.out.println("Target not found."); } else { System.out.println("Target found."); } }}// Task 5 — String Method Practice Mini-Challenge//// Create two Strings and do all of the following:// 1) Check if they are equal using equals// 2) Compare them alphabetically using compareTo// 3) Print both lowercase// 4) Print both uppercase//// Suggested values:// "Apple"// "banana"
public class Program{ public static void main(String[] args) { String a = "Apple"; String b = "banana";
System.out.println("Equals? " + a.equals(b)); System.out.println("compareTo: " + a.compareTo(b)); System.out.println("a lowercase: " + a.toLowerCase()); System.out.println("b lowercase: " + b.toLowerCase()); System.out.println("a uppercase: " + a.toUpperCase()); System.out.println("b uppercase: " + b.toUpperCase()); }}Your program output should something similar to the sample output below.
Word: computerLength: 8First character: cLast character: rUppercase: COMPUTER
G.H.
Username: alovelace
Sentence: The quick brown fox jumps over the lazy dogIndex of target: 16Target found.
Equals? falsecompareTo: -33a lowercase: appleb lowercase: bananaa uppercase: APPLEb uppercase: BANANAYou may write your reflection answers as comments at the bottom of your code.
equals() instead of == for Strings?indexOf() return when the text is not found?substring(0, 3) return only the first three characters?compareTo() different from equals()?Submit your activity and reflection answers to the appropriate dropbox.