CS 106A, Lecture 9 Problem-Solving with Strings
|
|
- Osborn Robinson
- 5 years ago
- Views:
Transcription
1 CS 106A, Lecture 9 Problem-Solving with Strings suggested reading: Java Ch. 8.5 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others.
2 Plan For Today Announcements Recap: Characters and Strings More Strings Practice: Reversing a String Practice: Palindromes Practice: Caesar Cipher 2
3 Plan For Today Announcements Recap: Characters and Strings More Strings Practice: Reversing a String Practice: Palindromes Practice: Caesar Cipher 3
4 Announcements Assignment 2 is due tomorrow! Please submit ahead of time, just in case Questions during lecture: Keep asking! I will start directing some to Piazza J 4
5 Plan For Today Announcements Recap: Characters and Strings More Strings Practice: Reversing a String Practice: Palindromes Practice: Caesar Cipher 5
6 Char A char is a variable type that represents a single character or glyph. char lettera = 'A'; char plus = '+'; char zero = '0'; char space = ' '; char newline = '\n'; char tab = '\t'; char singlequote = '\''; char backslash = '\\'; 6
7 Char Under the hood, Java represents each char as an integer (its ASCII value ). Uppercase letters are sequentially numbered Lowercase letters are sequentially numbered Digits are sequentially numbered char uppercasea = 'A'; // Actually 65 char lowercasea = 'a'; // Actually 97 char zerodigit = '0'; // Actually 48 7
8 Char Math! We can take advantage of Java representing each char as an integer (its ASCII value ): boolean areequal = 'A' == 'A'; // true boolean earlierletter = 'f' < 'c'; // false char uppercaseb = 'A' + 1; int diff = 'c' - 'a'; // 2 int numlettersinalphabet = 'z' 'a' + 1; // or int numlettersinalphabet = 'Z' 'A' + 1; 8
9 Side Note: Type-casting If we want to force Java to treat an expression as a particular type, we can also cast it to that type. 'A' + 1 (char)('a' + 1) // evaluates to 66 (int) // evaluates to 'B' (char) 1 / 2 // evaluates to 0 (int) (double)1 / 2 // evaluates to 0.5 (double) 1 / (double)2 // evaluates to 0.5 (double) 9
10 Character Methods Method Character.isDigit(ch) Character.isLetter(ch) Character.isLetterOrDigit(ch) Character.isLowerCase(ch) Character.isUpperCase(ch) Character.toLowerCase(ch) Character.toUpperCase(ch) Character.isWhitespace(ch) Description true if ch is '0' through '9' true if ch is 'a' through 'z' or 'A' through 'Z' true if ch is 'a' through 'z', 'A' through 'Z' or '0' through '9' true if ch is 'a' through 'z' true if ch is 'A' through 'Z' returns lowercase equivalent of a letter returns uppercase equivalent of a letter true if ch is a space, tab, new line, etc. Remember: tolowercase and touppercase return the new char; they cannot modify an existing char! 10
11 Strings A String is a variable type representing a sequence of characters. String text = "Hi parents!"; index character 'H' 'i' ' ' 'p' 'a' 'r' 'e' 'n' 't' 's' '!' Each character is assigned an index, going from 0 to length-1 There is a char at each index 11
12 Strings vs. Chars Remember: chars and length-1 strings are different! char ch = 'A' DIFFERENT FROM String str = A 'A' + 1 A + 1 // evaluates to 66 (int) // evaluates to A1 (String) 12
13 Creating Strings String str = "Hello, world!"; String empty = ""; println(str); // Read in text from the user String name = readline("what is your name? "); // String concatenation (using + ) String message = name + " is " " cool."; 13
14 From Chars to Strings char c1 = 'a'; char c2 = 'b'; // How do we concatenate these characters? String str = c1 + c2; // ERROR: this is an int! String str = "" + c1 + c2; // 14
15 Plan For Today Announcements Recap: Characters and Strings More Strings Practice: Reversing a String Practice: Palindromes Practice: Caesar Cipher 15
16 Substrings A substring is a subset of a string. String str = "Hello, world!"; String hello = str.substring(0, 5); 'H' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' 16
17 Substrings A substring is a subset of a string. String str = "Hello, world!"; String worldexclm = str.substring(7, 13); 'H' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' 17
18 Substrings A substring is a subset of a string. String str = "Hello, world!"; String worldexclm = str.substring(7); // to end 'H' 'e' 'l' 'l' 'o' ',' ' ' 'w' 'o' 'r' 'l' 'd' '!' 18
19 String Methods s.length() Method name s.charat(index) s.indexof(str) s.substring(index1, index2) or s.substring(index1) s.tolowercase() s.touppercase() Description number of characters in this string char at the given index index where the start of the given string appears in this string (-1 if not found) the characters in this string from index1 (inclusive) to index2 (exclusive); if index2 is omitted, goes until end a new string with all lowercase letters a new string with all uppercase letters These methods are called using dot notation: String classname = "CS 106yay!"; println(classname.length()); // 10 19
20 Strings are Immutable Once you create a String, its contents cannot be changed. // Cannot change individual chars in the string String typo = "Hello, warld! ; typo.charat(8) = o ; // Error! Will not run. To change a String, you must create a new String containing the value you want (possibly using String methods). String corrected = typo.substring(0, 8) + o + typo.substring(9); 20
21 Strings are Immutable String classname = "cs 106a"; classname.touppercase(); // does nothing! classname = classname.touppercase(); // println(classname); // CS 106A 21
22 Comparing Strings String greeting = Hello! ; if (greeting == "Hello!") {... } // Doesn t work! // Instead: if (greeting.equals("hello!")) {... } Always use.equals instead of == and!= 22
23 Comparing Strings Method s1.equals(s2) s1.equalsignorecase(s2) s1.startswith(s2) s1.endswith(s2) s1.contains(s2) Description whether two strings contain the same characters whether two strings contain the same characters, ignoring upper vs. lower case whether s1 contains s2 s characters at start whether s1 contains s2 s characters at end whether s2 is found within s1 23
24 Looping Over Strings A common String programming pattern is looping over a string and operating on each character. String str = "Hello!"; for (int i = 0; i < str.length(); i++) { char ch = str.charat(i); // Do something with ch here } 24
25 Looping Over Strings A common String programming pattern is looping over a string and operating on each character. // Prints out each letter on a separate line String str = "Hello!"; for (int i = 0; i < str.length(); i++) { char ch = str.charat(i); println(ch); } 25
26 Looping Over Strings A common String programming pattern is looping over a string and operating on each character. // Creates a new String in all caps String str = "Hello!"; String newstr = ""; for (int i = 0; i < str.length(); i++) { char ch = str.charat(i); newstr = newstr + Character.toUpperCase(ch); } println(newstr); // HELLO! 26
27 Looping Over Strings A common String programming pattern is looping over a string and operating on each character. // Creates a new String in all caps String str = "Hello!"; String newstr = ""; for (int i = 0; i < str.length(); i++) { char ch = str.charat(i); newstr += Character.toUpperCase(ch); } println(newstr); // HELLO! 27
28 Building Up New Strings Another common String programming pattern is building up a new string by adding characters to it over time. // Creates a new String in all caps String str = ""; for (int i = 0; i < 5; i++) { str += i; } println(str); //
29 Plan For Today Announcements Recap: Characters and Strings More Strings Practice: Reversing a String Practice: Palindromes Practice: Caesar Cipher 29
30 Exercise: Reversing a String Let s write a method called reversestring that takes one String parameter, and returns a new String with the characters in the opposite order. reversestring("hello!") -> "!olleh" 30
31 Reversing a String H e l l o! 31
32 Reversing a String H e l l o!! 32
33 Reversing a String H e l l o!! 33
34 Reversing a String H e l l o!! o 34
35 Reversing a String H e l l o!! o 35
36 Reversing a String H e l l o!! o l 36
37 Reversing a String H e l l o!! o l 37
38 Reversing a String H e l l o!! o l l 38
39 Reversing a String H e l l o!! o l l 39
40 Reversing a String H e l l o!! o l l e 40
41 Reversing a String H e l l o!! o l l e 41
42 Reversing a String H e l l o!! o l l e H 42
43 Reversing a String H e l l o!! o l l e H 43
44 Reversing a String String str = "Hello!"; String newstr = ""; for (??? ;??? ;???) {... } 44
45 Reversing a String String str = "Hello!"; String newstr = ""; for (int i = str.length() - 1;??? ;???) {... } H e l l o!! 45
46 Reversing a String String str = "Hello!"; String newstr = ""; for (int i = str.length() - 1; i >= 0;???) {... } H e l l o!! o l l e H 46
47 Reversing a String String str = "Hello!"; String newstr = ""; for (int i = str.length() - 1; i >= 0; i--) {... } H e l l o!! o l l e H 47
48 Reversing a String String str = "Hello!"; String newstr = ""; for (int i = str.length() - 1; i >= 0; i--) { newstr += str.charat(i); } 48
49 Reversing a String H e l l o! 49
50 Reversing a String H e l l o! H 50
51 Reversing a String H e l l o! e H 51
52 Reversing a String H e l l o! l e H 52
53 Reversing a String H e l l o! l l e H 53
54 Reversing a String H e l l o! o l l e H 54
55 Reversing a String H e l l o!! o l l e H 55
56 Reversing a String public void run() { private println("this String reversestring(string program reverses a string."); str) { String String str result = readline("enter = ""; a string: "); String for ( rev int = i reversestring(str); = 0; i < str.length(); i++ ) { println(str result = + str.charat(i) " spelled backwards + result; is " + rev); } } return result; rev } DESSERTS str STRESSED result str i STS SERTS DESSERTS STRESSED ReverseString This program reverses a string. Enter a string: STRESSED STRESSED spelled backwards is DESSERTS Using portions of slides by Eric Roberts 56
57 Plan For Today Announcements Recap: Characters and Strings More Strings Practice: Reversing a String Practice: Palindromes Practice: Caesar Cipher 57
58 Exercise: Palindromes Let s write a method called ispalindrome that takes one String parameter, and returns whether or not that String is a palindrome (the same forwards and backwards). ispalindrome("racecar") -> true ispalindrome("hi there") -> false ispalindrome("kayak") -> true 58
59 Plan For Today Announcements Recap: Characters and Strings More Strings Practice: Reversing a String Practice: Palindromes Practice: Caesar Cipher 59
60 Exercise: Caesar Cipher Rotate alphabet by n letters (n = 3 in below) n is called the key Wrap-around at the end Substitute letters based on this mapping original encrypt A B C D E F G H I J K L M N O P Q R S T U V W X Y Z D E F G H I J K L M N O P Q R S T U V W X Y Z A B C 60
61 Exercise: Caesar Cipher Rotate alphabet by a certain key, with wrapping original encrypt A B C D E F G H I J K L M N O P Q R S T U V W X Y Z D E F G H I J K L M N O P Q R S T U V W X Y Z A B C 61
62 Recap Recap: Characters and Strings More Strings Practice: Reversing a String Practice: Palindromes Practice: Caesar Cipher Next time: reading text files 62
CS 106A, Lecture 9 Problem-Solving with Strings
CS 106A, Lecture 9 Problem-Solving with Strings suggested reading: Java Ch. 8.5 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5
More informationCS107 Spring 2019, Lecture 4 C Strings
CS107 Spring 2019, Lecture 4 C Strings Reading: K&R (1.9, 5.5, Appendix B3) or Essential C section 3 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative
More informationCS107, Lecture 4 C Strings
CS107, Lecture 4 C Strings Reading: K&R (1.9, 5.5, Appendix B3) or Essential C section 3 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution
More informationCS106A Review Session
CS106A Review Session Nick Troccoli This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides
More informationchar ch = astring; //where astring is a String..illegal char ch = A ; //illegal
13-1 Character type and types can t be stored into each other. The following lines of code are : char ch = astring; //where astring is a String..illegal char ch = A ; //illegal String x = xchar; //where
More informationAn Interesting Article. How Revolutionary Tools Cracked a 1700s Code.
Manipulating Text An Interesting Article How Revolutionary Tools Cracked a 1700s Code http://www.nytimes.com/2011/10/25/science/25code.html Announcements Breakout! due this Friday at 3:15PM. Stop by the
More informationProgramming Lecture 9
Programming Lecture 9 Strings and Characters (Chapter 8) Enumeration ASCII / Unicode, special characters Character class Character arithmetic Strings vs. characters String methods Top-Down design StringTokenizer
More informationCS 106A, Lecture 19 ArrayLists
CS 106A, Lecture 19 ArrayLists suggested reading: Java Ch. 11.8 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights
More informationCS 106A, Lecture 27 Final Exam Review 1
CS 106A, Lecture 27 Final Exam Review 1 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on
More informationCS 106A, Lecture 27 Final Exam Review 1
CS 106A, Lecture 27 Final Exam Review 1 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on
More informationProgramming Lecture 9
Programming Lecture 9 Strings and Characters (Chapter 8) Enumeration ASCII / Unicode, special characters Character class Character arithmetic Strings vs. characters String methods Splitting a string into
More informationBuilding Java Programs
Building Java Programs Chapter 4 Lecture 4-2: Strings reading: 3.3, 4.3-4.4 self-check: Ch. 4 #12, 15 exercises: Ch. 4 #15, 16 videos: Ch. 3 #3 1 Objects and classes object: An entity that contains: data
More informationCS 106A, Lecture 7 Parameters and Return
CS 106A, Lecture 7 Parameters and Return suggested reading: Java Ch. 5.1-5.4 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License.
More informationCS 106A, Lecture 16 Arrays
CS 106A, Lecture 16 Arrays suggested reading: Java Ch. 11.1-11.5 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights
More informationAP Computer Science A
AP Computer Science A 1st Quarter Notes Table of Contents - section links Click on the date or topic below to jump to that section Date : 9/8/2017 Aim : Java Basics Objects and Classes Data types: Primitive
More informationCS 106X, Lecture 16 More Linked Lists
CS 106X, Lecture 16 More Linked Lists reading: Programming Abstractions in C++, Chapters 11-12, 14.1-14.2 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative
More informationCS 106A, Lecture 5 Booleans and Control Flow
CS 106A, Lecture 5 Booleans and Control Flow suggested reading: Java Ch. 3.4-4.6 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5
More informationCS106A Review Session. Monday Oct. 31, 2016 Nick Troccoli
CS106A Review Session Monday Oct. 31, 2016 Nick Troccoli 1 Topic List Karel Java constructs Graphics + Animation Classes and Interfaces Memory (Pass-by-reference vs. pass by value) Event-driven programming
More informationCS 106A, Lecture 20 ArrayLists and HashMaps
CS 106A, Lecture 20 ArrayLists and HashMaps suggested reading: Java Ch. 13.2 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License.
More information"Hello" " This " + "is String " + "concatenation"
Strings About Strings Strings are objects, but there is a special syntax for writing String literals: "Hello" Strings, unlike most other objects, have a defined operation (as opposed to a method): " This
More informationCS 106A, Lecture 14 Events and Instance Variables
CS 106A, Lecture 14 Events and Instance Variables Reading: Art & Science of Java, Ch. 10.1-10.4 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons
More informationCS107, Lecture 3 Bits and Bytes; Bitwise Operators
CS107, Lecture 3 Bits and Bytes; Bitwise Operators reading: Bryant & O Hallaron, Ch. 2.1 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution
More informationOctober 27, 7:30-9:30 PM Taylor Bacon and Kashif Nazir (Based on slides by Nick Troccoli)
October 27, 7:30-9:30 PM Taylor Bacon and Kashif Nazir (Based on slides by Nick Troccoli) Topic Date Time Location Assignment 4 Today! Now! Here! Midterm 11/1 7-9PM MemAud Assignment 5 11/9 7:30-9:30PM
More informationThe second statement selects character number 1 from and assigns it to.
Chapter 8 Strings 8.1 A string is a sequence A string is a sequence of characters. You can access the characters one at a time with the bracket operator: The second statement selects character number 1
More informationMemory Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University
Memory Chris Piech CS106A, Stanford University Learning Goals 1. Be able to trace memory with references Write this program Who thinks this prints true? Who thinks this prints true? public void run() {
More informationEng. Mohammed Abdualal
Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2124) Lab 2 String & Character Eng. Mohammed Abdualal String Class In this lab, you have
More informationDiscussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:
Discussion 1H Notes (Week 4, April 22) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs31 Passing Arguments By Value and By Reference So far, we have been passing in
More informationCS 106A, Lecture 3 Problem-solving with Karel
CS 106A, Lecture 3 Problem-solving with Karel suggested reading: Karel, Ch. 5-6 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5
More informationLab #7 Library Classes and JUnit Testing. Daniel Amyot, Diana Inkpen, Alan. Agenda. In this lab, you are going to create your own
ITI 1120 Lab #7 Library Classes and JUnit Testing Daniel Amyot, Diana Inkpen, Alan Williams Topics in this lab: Strings vs. char[] Methods Library classes Testing Agenda In this lab, you are going to create
More informationCS 106X, Lecture 10 Recursive Backtracking
CS 106X, Lecture 10 Recursive Backtracking reading: Programming Abstractions in C++, Chapter 9 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons
More informationCS 106A, Lecture 3 Problem-solving with Karel
CS 106A, Lecture 3 Problem-solving with Karel suggested reading: Karel, Ch. 5-6 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5
More informationLecture 8: The String Class and Boolean Zen
Lecture 8: The String Class and Boolean Zen Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Strings string: An object
More informationStrings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics
Strings Strings and their methods Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list Primitive Types: char Object Types: String Primitive
More informationSolution to Section #3 Portions of this handout by Eric Roberts, Mehran Sahami, Marty Stepp, Patrick Young and Jeremy Keeshin
Nick Troccoli Section #3 CS 106A July 10, 2017 Solution to Section #3 Portions of this handout by Eric Roberts, Mehran Sahami, Marty Stepp, Patrick Young and Jeremy Keeshin 1. Adding commas to numeric
More informationLecture 19: Recursion
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved. Recursion recursion: The definition of an operation
More informationCS 106A, Lecture 14 Events and Instance Variables
CS 106A, Lecture 14 Events and Instance Variables Reading: Art & Science of Java, Ch. 10.1-10.4 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons
More informationCreating Strings. String Length
Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and
More informationCS 106A, Lecture 25 Life After CS 106A, Part 1
CS 106A, Lecture 25 Life After CS 106A, Part 1 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based
More informationCS 106B, Lecture 1 Introduction to C++
CS 106B, Lecture 1 Introduction to C++ reading: Programming Abstractions in C++, Chapters 1 & 2 This document is copyright (C) Stanford Computer Science and Ashley Marty Stepp, Taylor, licensed under Creative
More informationStrings. Steven R. Bagley
Strings Steven R. Bagley Recap Programs are a series of statements Defined in functions Functions, loops and conditionals can alter program flow Data stored in variables or arrays Or pointed at by pointers
More informationCSE 2123 Recursion. Jeremy Morris
CSE 2123 Recursion Jeremy Morris 1 Past Few Weeks For the past few weeks we have been focusing on data structures Classes & Object-oriented programming Collections Lists, Sets, Maps, etc. Now we turn our
More informationCSc 110, Spring Lecture 14: Booleans and Strings. Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 14: Booleans and Strings Adapted from slides by Marty Stepp and Stuart Reges Exercise: Logical questions What is the result of each of the following expressions? x = 42 y =
More informationCS 1301 Ch 8, Part A
CS 1301 Ch 8, Part A Sections Pages Review Questions Programming Exercises 8.1 8.8 264 291 1 30 2,4,6,8,10,12,14,16,18,24,28 This section of notes discusses the String class. The String Class 1. A String
More informationSection Handout #3: Strings and Files
Nick Troccoli Section #3 CS 106A July 10, 2017 Section Handout #3: Strings and Files Portions of this handout by Eric Roberts, Mehran Sahami, Marty Stepp, Patrick Young and Jeremy Keeshin 1. Adding commas
More informationStrings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics
Strings Strings and their methods Produced by: Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list Primitive Types: char Object Types: String Primitive vs Object Types
More informationAssoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.
Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All This chapter discusses class String, from the java.lang package. These classes provide the foundation for string and character manipulation
More informationCS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017
+ CS 106A Midterm Review Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017 Details n Only the textbook is allowed n n n The Art and Science of Java Karel Course Reader You will
More informationCS106A Final Exam Review Session. Saturday Dec. 10, 2016 Nick Troccoli
CS106A Final Exam Review Session Saturday Dec. 10, 2016 Nick Troccoli 1 Today s Topic List Primitives, Objects, and Heap/Stack Graphics + Animation Event-Driven Programs Strings + chars Classes + Interfaces
More informationCSc 110, Autumn Lecture 14: Strings. Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 14: Strings Adapted from slides by Marty Stepp and Stuart Reges "Boolean Zen", part 1 Students new to boolean often test if a result is True: if is_prime(57) == True: # bad...
More informationCS107, Lecture 3 Bits and Bytes; Bitwise Operators
CS107, Lecture 3 Bits and Bytes; Bitwise Operators reading: Bryant & O Hallaron, Ch. 2.1 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution
More informationStrings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.
Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and
More informationCS 106A, Lecture 11 Graphics
CS 106A, Lecture 11 Graphics reading: Art & Science of Java, 9.1-9.3 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All
More informationChapter 4. Mathematical Functions, Characters, and Strings
Chapter 4 Mathematical Functions, Characters, and Strings 1 Outline 1. Java Class Library 2. Class Math 3. Character Data Type 4. Class String 5. printf Statement 2 1. Java Class Library A class library
More informationAssoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.
Assoc. Prof. Marenglen Biba (C) 2010 Pearson Education, Inc. All Advanced Java This chapter discusses class String, class StringBuilder and class Character from the java.lang package. These classes provide
More informationCS 106X, Lecture 7 Introduction to Recursion
CS 106X, Lecture 7 Introduction to Recursion reading: Programming Abstractions in C++, Chapter 7 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons
More informationCOSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand
COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand COSC 236 Web Site You will always find the course material at: http://www.class-notes.us or http://www.class-notes.info or http://www.lecture-notes.tripod.com
More informationCS 106A, Lecture 23 Interactors and GCanvas
CS 106A, Lecture 23 Interactors and GCanvas suggested reading: Java Ch. 10.5-10.6 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5
More informationt 1 =2 t n =3t n 1 1,n 1
Recursive Calculations Recursion in Java Implementing Recursive Algorithms Suppose we are given the following recursive definition of a sequence: t 1 =2 t n =3t n 1 1, n 1 We can express this sequence
More informationHangman YEAH Hours. Thursday, February 14, 7:30 9:00PM Andrew Tierno
Hangman YEAH Hours Thursday, February 14, 7:30 9:00PM Andrew Tierno Overview Review Lecture Material Characters Strings Assignment Overview Milestones/breakdown of tasks Some useful upcoming topics General
More informationJava s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"
1 Java s String Class in simplest form, just quoted text "This is a string" "So is this" "hi" used as parameters to Text constructor System.out.println 2 The Empty String smallest possible string made
More informationSequence of Characters. Non-printing Characters. And Then There Is """ """ Subset of UTF-8. String Representation 6/5/2018.
Chapter 4 Working with Strings Sequence of Characters we've talked about strings being a sequence of characters. a string is indicated between ' ' or " " the exact sequence of characters is maintained
More informationCOMP-202: Foundations of Programming. Lecture 9: Arrays and Practice Jackie Cheung, Winter 2016
COMP-202: Foundations of Programming Lecture 9: Arrays and Practice Jackie Cheung, Winter 2016 Review: for Loops for (initialization; condition; update) { Happens once per loop only, before the first check
More informationIntroducing arrays. Week 4: arrays and strings. Creating arrays. Declaring arrays. Initialising arrays. Declaring and creating in one step
School of Computer Science, University of Birmingham Java Lecture notes. M. D. Ryan. September 2001. Introducing arrays Week 4: arrays and strings Arrays: a data structure used to store multiple data,
More informationChapter 7 User-Defined Methods. Chapter Objectives
Chapter 7 User-Defined Methods Chapter Objectives Understand how methods are used in Java programming Learn about standard (predefined) methods and discover how to use them in a program Learn about user-defined
More informationVariables, Constants, and Data Types
Variables, Constants, and Data Types Strings and Escape Characters Primitive Data Types Variables, Initialization, and Assignment Constants Reading for this lecture: Dawson, Chapter 2 http://introcs.cs.princeton.edu/python/12types
More informationHomework Assignment 2: Java Console and Graphics
SSEA August 2016 Cynthia Lee CS106A Homework Assignment 2: Java Console and Graphics Based on past assignments created by Marty Stepp, Mehran Sahami, Keith Schwarz, Eric Roberts, Stuart Reges, and others.
More informationIntro to Strings. CSE 231 Rich Enbody. String: a sequence of characters. Indicated with quotes: or " " 9/11/13
CSE 231 Rich Enbody String: a sequence of characters. Indicated with quotes: or " " 2 1 Triple quotes: preserve both the vertical and horizontal formatting of the string. Allows you to type tables, paragraphs,
More informationCS 106 Introduction to Computer Science I
CS 106 Introduction to Computer Science I 05 / 31 / 2017 Instructor: Michael Eckmann Today s Topics Questions / Comments? recap and some more details about variables, and if / else statements do lab work
More informationStrings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.
The only operator that can be applied to String objects is concatenation (+) for combining one or more strings. Java also provides many methods with the String class to allow us to manipulate text. These
More informationCSC 222: Object-Oriented Programming. Spring 2017
CSC 222: Object-Oriented Programming Spring 2017 Simulations & library classes HW3: RouletteWheel, RouletteGame, RouletteTester javadoc java.lang classes: String, Character, Integer java.util.random for
More informationUsing System.out.println()
Programming Assignments Read instructions carefully Many deduction on Program 3 for items in instructions Comment your code Coding conventions 20% of program grade going forward Class #23: Characters,
More informationLecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8
Today... Java basics S. Bowers 1 of 8 Java main method (cont.) In Java, main looks like this: public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World!"); Q: How
More informationYou ll be reading more about tools for branching in Sections 3.4, and We are skipping Sections 3.5, 3.11 and 3.13-end of chapter.
CS 1050 - Preview for Session 6 You ll be reading more about tools for branching in Sections 3.4, 3.6-3.10 and 3.12. We are skipping Sections 3.5, 3.11 and 3.13-end of chapter. Section 3.4 Logical Operators
More informationx++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x?
x++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x? x++ vs. ++x public class Plus{ public static void main(string []args){ int x=0; int a=++x; System.out.println(x); System.out.println(a);
More informationCSc 110, Autumn Lecture 11: Strings. Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 11: Strings Adapted from slides by Marty Stepp and Stuart Reges Strings string: a type that stores a sequence of text characters. name = "text" name = expression Examples:
More informationTopic 13 procedural design and Strings
Topic 13 procedural design and Strings Ugly programs are like ugly suspension bridges: they're much more liable to collapse than pretty ones, because the way humans (especially engineerhumans) perceive
More informationCS 106X, Lecture 14 Classes and Pointers
CS 106X, Lecture 14 Classes and Pointers reading: Programming Abstractions in C++, Chapter 6, 11 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons
More informationString related classes
Java Strings String related classes Java provides three String related classes java.lang package String class: Storing and processing Strings but Strings created using the String class cannot be modified
More informationCSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.
CSE1710 Click to edit Master Week text 12, styles Lecture 22 Second level Third level Fourth level Fifth level Fall 2014 Tuesday, Nov 25, 2014 1 Big Picture Tuesday, Dec 02 is designated as a study day.
More informationGetting started with Java
Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving
More informationBASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I
BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment
More informationWorking with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.
Working with Strings Husni "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc. Sequence of characters We've talked about strings being a sequence of characters.
More informationSlide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto
CS 170 Java Programming 1 More on Strings Working with the String class Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 What are Strings in Java? Immutable sequences of 0 n characters
More informationPython allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:
1 STRINGS Objectives: How text data is internally represented as a string Accessing individual characters by a positive or negative index String slices Operations on strings: concatenation, comparison,
More informationConverting a Lowercase Letter Character to Uppercase (Or Vice Versa)
Looping Forward Through the Characters of a C String A lot of C string algorithms require looping forward through all of the characters of the string. We can use a for loop to do that. The first character
More informationENGR/CS 101 CS Session Lecture 3
ENGR/CS 101 CS Session Lecture 3 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2010 Windows button -> All Programs -> 02 Programming -> Microsoft Visual Studio 2010 -> Microsoft
More informationString Objects: The string class library
String Objects: The string class library Lecture 23 COP 3014 Spring 2017 March 7, 2017 C-strings vs. string objects In C++ (and C), there is no built-in string type Basic strings (C-strings) are implemented
More informationNotes from the Boards Set BN19 Page
1 The Class, String There are five programs in the class code folder Set17. The first one, String1 is discussed below. The folder StringInput shows simple string input from the keyboard. Processing is
More informationLecture 2: Variables & Assignments
http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 2: Variables & Assignments (Sections 2.1-2.3,2.5) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner,
More informationIntro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16
Intro to Strings Lecture 7 CGS 3416 Spring 2017 February 13, 2017 Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, 2017 1 / 16 Strings in Java In Java, a string is an object. It is not a primitive
More informationSTRINGS AND STRINGBUILDERS. Spring 2019
STRINGS AND STRINGBUILDERS Spring 2019 STRING BASICS In Java, a string is an object. Three important pre-built classes used in string processing: the String class used to create and store immutable strings
More informationSimple Arrays. Eric Roberts CS 106A February 15, 2017
Simple Arrays Eric Roberts CS 106A February 15, 2017 Once upon a time... A Quick Review of Array Lists In Java, an array list is an abstract type used to store a linearly ordered collection of similar
More informationThe data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as
Control structures in Java are statements that contain statements. In particular, control structures can contain control structures. You've already seen several examples of if statements inside loops,
More informationCS 106X, Lecture 23 Dijkstra and A* Search
CS 106X, Lecture 23 Dijkstra and A* Search reading: Programming Abstractions in C++, Chapter 18 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons
More informationCS 2316 Pair 1: Homework 3 Enigma Fun Due: Wednesday, February 1st, before 11:55 PM Out of 100 points
CS 2316 Pair 1: Homework 3 Enigma Fun Due: Wednesday, February 1st, before 11:55 PM Out of 100 points Files to submit: HW3.py This is a PAIR PROGRAMMING assignment: Work with your partner! For pair programming
More informationCSC 222: Object-Oriented Programming. Spring 2013
CSC 222: Object-Oriented Programming Spring 2013 Simulations & library classes HW3: RouletteWheel, RouletteGame, RouletteTester javadoc java.lang classes: String, Character, Integer java.util.random for
More informationCMPT 125: Lecture 3 Data and Expressions
CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,
More informationUnit 2: The string class
: class Programming 2 2015-2016 Index 1 characters in C 2 Input / output 3 Conversion between arrays of characters and strings 4 Comparison with arrays of characters 5 characters in C characters in C have
More informationCS 115 Lecture 13. Strings. Neil Moore. Department of Computer Science University of Kentucky Lexington, Kentucky
CS 115 Lecture 13 Strings Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 29 October 2015 Strings We ve been using strings for a while. What can
More informationCSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI
CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are
More information