strings
play

Strings Final Project Example How would you make this? Take a - PowerPoint PPT Presentation

Strings Final Project Example How would you make this? Take a deep breath Final Project Example public class SpaceInvaders extends GraphicsProgram { private ArrayList<GOval> aliens; private GRect paddle; public void run() { Setup


  1. Strings

  2. Final Project Example How would you make this? 🤕 Take a deep breath…

  3. Final Project Example public class SpaceInvaders extends GraphicsProgram { private ArrayList<GOval> aliens; private GRect paddle; public void run() { Setup // create objects and scores // add() to canvas setupGame(); while (!gameOver()) { Animate animateObjects(); pause(100); } } private boolean keyPressed() {...} Interact private boolean mousePressed() {...} } Start with a high-level look.

  4. Final Project Example Animate Interact ß Key pressed: On every animation: move ship left • Aliens move • Keyboard Karel à Key pressed: Bouncing move ship right • Balls Mouse pressed: Add torpedo • Racing Cars Rocket Paddle Torpedos move • Rocket Paddle Find worked examples!!

  5. Learning Goals 1. Understand chars and Strings 2. Write methods acting on Strings 3. Learn something interesting

  6. Text Applications

  7. How is text represented?

  8. The variable type String Text is stored using the variable type String . A String is a sequence of characters. public void run() { String text = "hello!"; println(text); }

  9. The variable type String text H e l l o ! 0 1 2 3 4 5 All characters in a string have an in index . • You can access a character in the string via its in index . • char c = text.charAt(index); The le length of a string is one larger than the last valid • index in the string. int len = text.length(); // 6

  10. What String actually is String str = char [] charArray ➕ str.length(); str.charAt(i); str.split(str); str.contains(str); Data storage Useful methods Why do both of these exist in the Java language? • char [] builds off Java’s fundamental data storage • String adds convenient methods to char []

  11. String Methods public void run() { String example = "Hi mom"; int length = example.length(); println(length); char firstLetter = example.charAt(0); println(firstLetter); for ( int i = 0; i < example.length(); i++) { char ch = example.charAt(i); println(ch); } } 🤕

  12. String Methods public void run() { String example = "Hi mom"; 6 // example of length method int length = example.length(); println(length); // prints 6 // example of getCharAt char firstLetter = example.charAt(0); println(firstLetter); // prints 'H’ // loop that prints letters one-by-one for ( int i = 0; i < example.length(); i++) { char ch = example.charAt(i); println(ch); } } example length H i m o m 6 0 1 2 3 4 5

  13. String Methods public void run() { String example = "Hi mom"; 6 H // example of length method int length = example.length(); println(length); // prints 6 // example of getCharAt char firstLetter = example.charAt(0); println(firstLetter); // prints 'H’ // loop that prints letters one-by-one for ( int i = 0; i < example.length(); i++) { char ch = example.charAt(i); println(ch); } } first example length Letter H i m o m 6 'H' 0 1 2 3 4 5

  14. String Methods public void run() { String example = "Hi mom"; 6 H // example of length method H int length = example.length(); println(length); // prints 6 // example of getCharAt char firstLetter = example.charAt(0); println(firstLetter); // prints 'H’ // loop that prints letters one-by-one for ( int i = 0; i < example.length(); i++) { char ch = example.charAt(i); println(ch); } } first example length i ch Letter H i m o m 6 'H' 0 'H' 0 1 2 3 4 5

  15. String Methods public void run() { String example = "Hi mom"; 6 H // example of length method H int length = example.length(); println(length); // prints 6 i // example of getCharAt char firstLetter = example.charAt(0); println(firstLetter); // prints 'H’ // loop that prints letters one-by-one for ( int i = 0; i < example.length(); i++) { char ch = example.charAt(i); println(ch); } } first example length i ch Letter H i m o m 6 'H' 1 'i' 0 1 2 3 4 5

  16. String Methods public void run() { String example = "Hi mom"; 6 H // example of length method H int length = example.length(); println(length); // prints 6 i // example of getCharAt char firstLetter = example.charAt(0); println(firstLetter); // prints 'H’ // loop that prints letters one-by-one for ( int i = 0; i < example.length(); i++) { char ch = example.charAt(i); println(ch); } } first example length i ch Letter H i m o m 6 'H' 2 ' ' 0 1 2 3 4 5

  17. String Methods public void run() { String example = "Hi mom"; 6 H // example of length method H int length = example.length(); println(length); // prints 6 i // example of getCharAt char firstLetter = example.charAt(0); m println(firstLetter); // prints 'H’ // loop that prints letters one-by-one for ( int i = 0; i < example.length(); i++) { char ch = example.charAt(i); println(ch); } } first example length i ch Letter H i m o m 6 'H' 3 'm' 0 1 2 3 4 5

  18. String Methods public void run() { String example = "Hi mom"; 6 H // example of length method H int length = example.length(); println(length); // prints 6 i // example of getCharAt char firstLetter = example.charAt(0); m println(firstLetter); // prints 'H’ o // loop that prints letters one-by-one for ( int i = 0; i < example.length(); i++) { char ch = example.charAt(i); println(ch); } } first example length i ch Letter H i m o m 6 'H' 4 'o' 0 1 2 3 4 5

  19. String Methods public void run() { String example = "Hi mom"; 6 H // example of length method H int length = example.length(); println(length); // prints 6 i // example of getCharAt char firstLetter = example.charAt(0); m println(firstLetter); // prints 'H’ o // loop that prints letters one-by-one m for ( int i = 0; i < example.length(); i++) { char ch = example.charAt(i); println(ch); } } first example length i ch Letter H i m o m 6 'H' 5 'm' 0 1 2 3 4 5

  20. String Methods public void run() { String example = "Hi mom"; 6 H // example of length method H int length = example.length(); println(length); // prints 6 i // example of getCharAt char firstLetter = example.charAt(0); m println(firstLetter); // prints 'H’ o // loop that prints letters one-by-one m for ( int i = 0; i < example.length(); i++) { char ch = example.charAt(i); println(ch); } } first example length i Letter H i m o m 6 'H' 6 0 1 2 3 4 5

  21. How are characters represented?

  22. The variable type char The primitive type char represents a single character or glyph. Some examples: char letterA = 'A'; char plus = '+'; char zero = '0'; char space = ' '; char newLine = '\n'; // special char first = text.charAt(0);

  23. ASCII * This is only the first half of the table The letter A, for example, has the ASCII value 65 Using portions of slides by Eric Roberts

  24. 'A ' à 'Z' are sequential. 'a' à 'Z' are sequential. '0' à ‘9' are sequential.

  25. Useful Character methods public void run() { String str = readLine("Line: "); char ch = str.charAt(0); println("Original first char: " + ch); ch = Character.toUpperCase(ch); println("Uppercase first char: " + ch); if (Character.isLetter(ch)) { println("It ’ s a letter!"); } }

  26. Stay alert! a c char ch = 'a'; A Character.toUpperCase(ch); println(ch); a a char ch = 'a’; c A A ch = Character.toUpperCase(ch); println(ch); A

  27. Useful Character methods static boolean isDigit(char ch) Determines if the specified character is a digit. static boolean isLetter(char ch) Determines if the specified character is a letter. static boolean isLetterOrDigit(char ch) Determines if the specified character is a letter or a digit. static boolean isLowerCase(char ch) Determines if the specified character is a lowercase letter. static boolean isUpperCase(char ch) Determines if the specified character is an uppercase letter. static boolean isWhitespace(char ch) Determines if the specified character is whitespace (spaces and tabs). static char toLowerCase(char ch) Converts ch to its lowercase equivalent, if any. If not, ch is returned unchanged. static char toUpperCase(char ch) Converts ch to its uppercase equivalent, if any. If not, ch is returned unchanged. Using portions of slides by Eric Roberts

  28. Strings have some unique properties

  29. Strings are Immutable l Java strings are im immut utable able : once a string has been created yo you cannot set characters . l To change a string: l Cr Creat ate a ne a new s w str tring ing holding the new value you want it to have via concatenation (+). l Reassigning the String variable (that’s allowed). l Impo Import rtan ant c t conse nseque quenc nce : if you pass a String into a method, that method cannot modify that string.

  30. Strings are often made through concatenation public void run() { String s1 = "Breakout"; String s2 = "it was awesome"; String s3 = "I crushed " + s1 + " and " + s2; println(s3); } s1 "Breakout" s2 "it was awesome" s3 "I crushed Breakout and it was awesome" I crushed Breakout and it was awesome

  31. Reversing a String Many string algorithms use the “loop and construct” pattern.

  32. Reversing a String H e l l o !

  33. Reversing a String H e l l o ! H

  34. Reversing a String H e l l o ! e H

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend