1 / 79

Chapter 16 Slides

Exposure Java-A 2006. Chapter 16 Slides. String Methods and Number Systems. PowerPoint Presentation created by: Mr. John L. M. Schram. From Materials Created by Mr. Leon Schram. String Definition. A String is a set of characters that behaves as a single unit.

Download Presentation

Chapter 16 Slides

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exposure Java-A 2006 Chapter 16 Slides String Methods and Number Systems PowerPoint Presentation created by: Mr. John L. M. Schram From Materials Created by Mr. Leon Schram

  2. String Definition • A String is a set of characters that behaves as a single unit. • The characters in a String include upper-case and lower-case letters, numerical characters and a large set of characters for a variety of purposes like: • ! @ # $ % ^ & * ( ) _ +

  3. String Literal Definition A string literal is a set of characters delimited with double quotations like: "Seymour Snodgrass"and "SSN: 123-45-6789"

  4. // Java1601.java // This program demonstrates how to declare five String objects. // Note that all five string objects store the same information. public class Java1601 { public static void main (String args[]) { String s1 = "Tango"; System.out.println("s1: " + s1); String s2; s2 = "Tango"; System.out.println("s2: " + s2); String s3 = new String("Tango"); System.out.println("s3: " + s3); String s4 = new String(); s4 = "Tango"; System.out.println("s4: " + s4); char Dance[] = {'T','a','n','g','o'}; String s5 = new String(Dance); System.out.println("s5: " + s5); System.out.println(); } }

  5. Mathematical Addition 100 + 200 = 300 int x = 100; x += 200; When used with int or double the plus sign ( + ) performs addition.

  6. String Concatenation “100” + “200” = “100200” String x = “100”; x += “200”; When used with Strings the plus sign ( + ) performs String Contatenation.

  7. // Java1602.java // This program shows how to concatenate strings using the // + operator and the <concat> method. public class Java1602 { public static void main (String args[]) { String s1 = "Argentine"; String s2 = "Tango"; String s3 = s1 + " " + s2; System.out.println("s3: " + s3); String s4 = "Argentine"; s4 = s4.concat(" Tango"); System.out.println("s4: " + s4); System.out.println(); } }

  8. String Method concat s1 = s2.concat("hiss"); concat concatenates the method argument to the object String. If s2 == "boo" thens1will become"boohiss"

  9. // Java1603.java // This program demonstrates the use of the <length> method. public class Java1603 { public static void main (String args[]) { String s1 = "Argentine"; String s2 = "Tango"; String s3 = s1 + " " + s2; System.out.println(s1 + " has " + s1.length() + " characters."); System.out.println(s2 + " has " + s2.length() + " characters."); System.out.println(s3 + " has " + s3.length() + " characters."); System.out.println(); } } Do not confuse the length() method of String with the length field of a Java static Array!

  10. // Java1604.java // This program demonstrates how to access individual characters of // a String object with the <charAt> method. public class Java1604 { public static void main (String args[]) { String s1 = "Madam I'm Adam"; String s2 = ""; int N = s1.length() - 1; for (int K = N; K >= 0; K--) s2 += s1.charAt(K); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); } }

  11. String Method charAt letter = s.charAt(k); Method charAt returns the character stored at the kth index location of the String object s. The first character is at index 0. If s == "Aardvark" and k == 4 then letter becomes v

  12. // Java1605.java // This program demonstrates how to access specified characters of // a string with the <substring(SI,EI)> method, where SI is the // StartIndex and EI is one less than the EndIndex. public class Java1605 { public static void main (String args[]) { String s = "Racecar"; int n = s.length(); for (int k = 1; k <= n; k++) System.out.println(s.substring(0,k)); System.out.println(); for (int k = 0; k <= n-3; k++) System.out.println(s.substring(k,k+3)); System.out.println(); } }

  13. String Method substring s1 = “Aardvark”; s2 = s1.substring(0,k); Method substring returns a set of consecutive characters from String s1, starting at index 0, and ending at index k-1. s3 = s1.substring(4,8); s2 becomes "Aard" and s3 becomes "vark"

  14. // Java1606.java // This program shows the <indexOf> method, which returns the index of the first // occurrence of the string argument, and the <lastIndexOf> method, which returns the // index of the last occurrence of the string argument. public class Java1606 { public static void main (String args[]) { String s1 = "racecar"; String s2 = "racecar in the carport"; String s3 = "car"; int index1 = s1.indexOf(s3); int index2 = s1.lastIndexOf(s3); int index3 = s2.indexOf(s3); int index4 = s2.lastIndexOf(s3); int index5 = s1.indexOf("qwerty"); System.out.println("With \"" + s1 + "\" car starts at " + index1 + " and last shows up at " + index2); System.out.println("With \"" + s2 + "\" car starts at " + index3 + " and last shows up at " + index4); System.out.println("With \"" + s3 + "\" Qwerty shows up at " + index5); System.out.println(); } }

  15. By the way, its the State Fish of Hawaii. String MethodsindexOf & lastIndexOf indexOf returns the first occurrence of a substring s1.indexOf(“hum”); returns 0 s1.indexOf(“ku”); returns 10 s1.indexOf(“qwerty”); returns -1 lastIndexOf returns the last occurrence of a substring s1.lastIndexOf(“hum”); returns 4 s1.lastIndexOf(“ku”); returns 14 s1.lastIndexOf(“qwerty”); returns -1 Note: The i in indexOf is lowercase, but in lastIndexOf it’s capital!

  16. // Java1607.java // This program demonstrates the <replace(Old,New)> String // method, which replaces all occurrences of the Old character // with the New character. // This method creates a new String object with the replaced // characters. public class Java1607 { public static void main (String args[]) { String s1 = "racecar"; String s2 = s1.replace('r','l'); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println(); } }

  17. String Method replace String s2 = s1.replace('m','b'); Method replace returns a string such that every occurrence of the old character ('m') is replaced by the new character ('b') . If s1 == "madam" then s becomes "badab"

  18. // Java1608.java // This program demonstrates the String <toUpperCase> and <toLowerCase> methods. public class Java1608 { public static void main (String args[]) { String s1 = "racecar"; String s2 = "RaCeCaR"; String s3 = "RACECAR100"; String s4 = s1.toUpperCase(); String s5 = s2.toUpperCase(); String s6 = s3.toUpperCase(); System.out.println("s1 --> s4: " + s4); System.out.println("s2 --> s5: " + s5); System.out.println("s3 --> s6: " + s6); System.out.println(); s1 = s4.toLowerCase(); s2 = s5.toLowerCase(); s3 = s6.toLowerCase(); System.out.println("s4 --> s1: " + s1); System.out.println("s5 --> s2: " + s2); System.out.println("s6 --> s3: " + s3); System.out.println(); } }

  19. String Methods toUpperCaseand toLowerCase s1 = s2.toLowerCase; s3 = s1.toUpperCase; Method toLowerCase returns a string with lower-case letters. Method toUpperCase returns a string with upper-case letters. Any characters that are not letters will be ignored by both methods and returned in their same relative string position.

  20. // Java1609.java // This program demonstrates the <valueOf> method of the String class, // which is shown to convert four data types to a string. // Note that <valueOf> is a static method and must be called using // <String.valueOf>. public class Java1609 { public static void main (String args[]) { String s1 = String.valueOf(1000); String s2 = String.valueOf(123.321); String s3 = String.valueOf(true); String s4 = String.valueOf('A'); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println("s3: " + s3); System.out.println("s4: " + s4); System.out.println(); } }

  21. String static Method valueOf String s1 = String.valueOf(1000); String s2 = String.valueOf(123.321); String s3 = String.valueOf(true); String s4 = String.valueOf('A'); Method valueOf converts the provided parameter and returns a string. Four overloaded valueOf methods are displayed. Note that the valueOf method is a static method (or class method) that is called with the String class identifier.

  22. // Java1610.java // This program converts string values to integer and double values // using the <parseInt> and <parseDouble> methods of the // <Integer> and <Double> classes. public class Java1610 { public static void main (String args[]) { String s1 = "12345"; String s2 = "123.321"; int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); System.out.println(n1 + " + " + n1 + " = " + (n1 + n1)); System.out.println(n2 + " + " + n2 + " = " + (n2 + n2)); System.out.println(); } }

  23. Integer static method parseInt and Double static method parseDouble int n1 = Integer.parseInt(s1); double n2 = Double.parseDouble(s2); Method parseInt converts a String into an int. Method parseDouble converts a String into a double. Parameters that include non-numerical characters will compile, but will cause a run-time error.

  24. // Java1611.java // This program checks the equality of two strings with the == operator. // The program executes as you might expect. public class Java1611 { public static void main (String args[]) { String s1 = "Foxtrot"; String s2 = "Waltz"; String s3 = "Foxtrot"; if (s1 == s2) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1 == s3) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); } }

  25. // Java1612.java // This program checks equality of strings, but this time a string entered at the keyboard // is used for comparison. This program has unexpected results. import java.util.Scanner; public class Java1612 { public static void main (String args[]) { Scanner input = new Scanner(System.in); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = input.nextLine(); if (s1 == s2) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1 == s3) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); } }

  26. // Java1613.java // This program uses the <trim> method, which removes "white space" from both ends of // the string argument. This method is used to try and solve the problem of the previous // program. import java.util.Scanner; public class Java1613 { public static void main (String args[]) { Scanner input = new Scanner(System.in); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = input.nextLine(); if (s1 == s2) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1 == s3.trim()) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); } }

  27. String Method trim s1 = s2.trim(); String method trim returns a string with "white space" removed from both ends of the String object.

  28. // Java1614.java // This program demonstrates the <equals> method, which is capable of // testing equality of string objects correctly. import java.util.Scanner; public class Java1614 { public static void main (String args[]) { Scanner input = new Scanner(System.in); String s1 = "Foxtrot"; String s2 = "Waltz"; System.out.print("Enter a string ===>> "); String s3 = input.readLine(); if (s1.equals(s2)) System.out.println(s1 + " == " + s2); else System.out.println(s1 + " != " + s2); if (s1.equals(s3)) System.out.println(s1 + " == " + s3); else System.out.println(s1 + " != " + s3); System.out.println(); } }

  29. What Is Going On? Part 1 The == operator does not work with objects. This is because it compares the memory addresses.

  30. What Is Going On? Part 2 The equals method should be used with objects like Strings. This is because it compares the actual information that is stored.

  31. // Java1615.java // This program demonstrates the <compareTo> method, which returns an integer value. // The value is 0 when the strings are equal, otherwise a value is returned that // indicates the relative distance between the strings. public class Java1615 { public static void main (String args[]) { String s1 = "AARDVARK"; String s2 = "ZEBRA"; String s3 = "AARDVARK"; String s4 = "BART"; int value1 = s1.compareTo(s2); int value2 = s1.compareTo(s3); int value3 = s2.compareTo(s1); int value4 = s1.compareTo(s4); System.out.println("value1: " + value1); System.out.println("value2: " + value2); System.out.println("value3: " + value3); System.out.println("value4: " + value4); System.out.println(); } }

  32. String methodsequals and compareTo if (s1.equals(s2)) int distance = s3.compareTo(s4); Method equals returns true is s1 == s2, and false otherwise. Method compareTo returns 0 if s3 == s4, otherwise an integer is returned based on the difference between s3 and s4. If s3 < s4, the returned value is negative. If s3 > s4, the returned value is positive.

  33. String Objects are Immutable A mutatoris a method that mutatesor alters object values. The character contents of a String object cannot be altered, which means that String objects are immutable. You need to construct a StringBuffer object if it is necessary to change the character content after the object is constructed.

  34. What do you mean "A String cannot be altered?!" If you look at these statements: it sure looks like name is being altered. In reality what happens is the old String object is destroyed and a new object (with a new memory address) is created. String name = "Bob"; name = "Joe";

  35. Number Systems

  36. AP Exam Alert Number System conversion questions have shown up on recent APCS Examinations. Even if you are not taking the AP Exam, a good understanding of Number Systems is vital for most technology fields.

  37. Counting In Other Number Systems Counting is something that you will likely take for granted, at least counting in base-10. In base-10 there are ten different single digits from 0 to 9. Counting in base-10 requires cycling through these ten digits. Every time 9 is reached the counting starts over at 0, and at the same time the digit in the next column is incremented by one. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 However, consider the fact that there are 60 seconds in a minute, 60 minutes in an hour. How about 12 inches to a foot, 3 feet to a yard, and 1,760 yards to a mile. Not everything is done in base-10.

  38. The Odometer Analogy The odometer is the part of the car that tells you how many miles you have driven. Example: Since each place holder can hold 10 possible digits (0-9), we can say that this odometer is counting in base-10. If we continued to let it count, we would see the following:

  39. The Broken Odometer Now suppose somebody decides to remove all of the 8s and 9s from your car’s odometer. Since each place holder can hold 8 possible digits (0-7), we can say that this odometer is counting in base-8. If we continued to let it count, we would see the following:

  40. Counting Rules for Numbers of All Bases There are as many single digits as the base value. The largest single digit is 1 less than the base value. You could say that in base N the range of single digits is from 0 to N-1. Base 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Base 9 0, 1, 2, 3, 4, 5, 6, 7, 8 Base 8 0, 1, 2, 3, 4, 5, 6, 7 Base 7 0, 1, 2, 3, 4, 5, 6 Base 6 0, 1, 2, 3, 4, 5 Base 5 0, 1, 2, 3, 4 Base 4 0, 1, 2, 3 Base 3 0, 1, 2 Base 2 0, 1

  41. List the next 10 base-8 numbers after 321 322 323 324 325 326 327 330 331 332 333 List the next 10 base-8 numbers after 406 407 410 411 412 413 414 415 416 417 420 List the next 10 base-4 numbers after 121 122 123 130 131 132 133 200 201 202 203 List the next 10 base-3 numbers after 11 12 20 21 22 100 101 102 110 111 112 List the next 10 base-2 numbers after 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111

  42. Counting In Base-16 Counting in base-16 is significant in computer science. You will see that computer memory addresses are displayed in base-16 or hexadecimal. A computer’s NIC (Network Interface Card) has a special address which is a base-16 number. Future IP addresses will use base-16 as well. There is a unique relationship between base-2 and base-16 that we will look at later. This relationship is the reason base-16 is used in Assembly Language. Right now you need to learn how to count in base-16.

  43. The Really Messed Up Odometer Now suppose somebody decides to add 6 digits (A-F) to your car’s odometer. Since each place holder can hold 16 possible digits (0-F), we can say that this odometer is counting in base-16. If we continued to let it count, we would see the following:

  44. Counting In Base-16 (continued)

  45. List the next 10 base-16 numbers after 100 101 102 103 104 105 106 107 108 109 10a List the next 10 base-16 numbers after 64 65 66 67 68 69 6a 6b 6c 6d 6e List the next 10 base-16 numbers after 1001 1002 1003 1004 1005 1006 1007 1008 1009 100a 100b List the next 10 base-16 numbers after abc abd abe abf ac0 ac1 ac2 ac3 ac4 ac5 ac6 List the next 10 base-16 numbers after 999 99a 99b 99c 99d 99e 99f 9a0 9a1 9a2 9a3

  46. Converting from Any Base to Base 10

  47. Flashback to Elementary School Do you remember learning about the One’s Column, Ten’s Column, Hundred’s Column’s, Thousand’s column, etc. back in elementary school. Understanding how Base-10 works is essential to understanding how other bases work. Consider the representation of the number 1,234,567 below: 1,000,000 + 200,000 + 30,000 + 4,000 + 500 + 60 + 7 = 1,234,567

  48. Scientific Notation Flashback Do you remember learning about powers of 10, exponents, and Scientific Notation. This is also essential. Consider the representation of the number 1,234,567 below: 1*106 + 2*105 + 3*104 + 4*103 + 5*102 + 6*101 + 7*100 = 1,234,567 In base-10, every column is a power of 10.

More Related