CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

Similar documents
Strings! Today! CSE String Methods!

CSE1710. Big Picture. Today is last day covering Ch 6. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

The next three class meetings will focus on Chapter 6 concepts. There will be a labtest on Chapter 5 concepts on Thurs Nov 20/Fri Nov 21.

The next three class meetings will focus on Chapter 6 concepts. There will be a labtest on Chapter 5 concepts on Thurs Nov 28/Fri Nov 29.

"Hello" " This " + "is String " + "concatenation"

CST141 Thinking in Objects Page 1

H212 Introduction to Software Systems Honors

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

STUDENT LESSON A10 The String Class

String related classes

AP Computer Science. Strings. Credit: Slides are modified with permission from Barry Wittman at Elizabethtown College

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

More non-primitive types Lesson 06

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

JAVA Programming Fundamentals

A variable is a name for a location in memory A variable must be declared

- Thus there is a String class (a large class)

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

CS112 Lecture: Characters and Strings

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

CSE Lecture 24 Review and Recap. High-Level Overview of the Course!! L1-7: I. Programming Basics!

Intro to Strings. Lecture 7 COP 3252 Summer May 23, 2017

Intro to Strings. Lecture 7 CGS 3416 Spring February 13, Lecture 7 CGS 3416 Spring 2017 Intro to Strings February 13, / 16

Introduction to Programming Using Java (98-388)

Full file at

We now start exploring some key elements of the Java programming language and ways of performing I/O

More on variables and methods

Class. Chapter 6: Data Abstraction. Example. Class

String is a Class; Quoted Text is an Object

Welcome to the Using Objects lab!

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Java Programming Fundamentals. Visit for more.

Lesson 4 Utility classes: Math, String, I/O

Ans: Store s as an expandable array of chars. (Double its size whenever we run out of space.) Cast the final array to a String.

Character Strings COSC Yves Lespérance. Lecture Notes Week 6 Java Strings

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Lab 14 & 15: String Handling

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Comments in a Java Program. Java Overview. Identifiers. Identifier Conventions. Primitive Data Types and Declaring Variables

Lecture 4. Strings, Wrappers, & Containers

Java Fall 2018 Margaret Reid-Miller

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

c) And last but not least, there are javadoc comments. See Weiss.

CSC Java Programming, Fall Java Data Types and Control Constructs

Operators and Expressions

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Designing data types. Fundamentals of Computer Science I

Programming with Java

Using Java Classes Fall 2018 Margaret Reid-Miller

AP Computer Science A

Chapter 2: Using Data

More on Strings. String methods and equality. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Compu<ng and Mathema<cs h=p://

CIS192 Python Programming. Robert Rand. August 27, 2015

CS Week 5. Jim Williams, PhD

CS 1301 Ch 8, Part A

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

String. Other languages that implement strings as character arrays

Lesson:9 Working with Array and String

Fall 2017 CISC124 9/27/2017

Lesson 3: Accepting User Input and Using Different Methods for Output

Designing data types. Overview. Object Oriented Programming. Alan Kay. Object Oriented Programming (OOP) Data encapsulation. Checking for equality

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Strings

CIS 110: Introduction to Computer Programming

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Advanced Algorithms and Computational Models (module A)

String Objects: The string class library

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

Overview of List Syntax

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

5/23/2015. Core Java Syllabus. VikRam ShaRma

Computer Science 145 Midterm 1 Fall 2016

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Program Fundamentals

Lecture 5. Assignments. Arithmetic Operations. Arithmetic Operations -Summary 1/24/18. Lab 3: Variables and operations. Read Sections

Pointers (continued), arrays and strings

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Week 6: Review. Java is Case Sensitive

Pointers (continued), arrays and strings

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

CSE 142, Summer 2014

Programming Languages and Techniques (CIS120)

STRINGS AND STRINGBUILDERS. Spring 2019

Chapter 4: Control Structures I

Quiz 1. In class on Tuesday (April 14) Review learning goals (from lecture) Review lectures 1 4, PSA 1 You should be able to:

COMP6700/2140 Data and Types

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Variables, Constants, and Data Types

CS 106A, Lecture 9 Problem-Solving with Strings

Welcome to the Using Objects lab!

CSE 142, Summer 2015

Building Java Programs

Transcription:

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. No classes. Thursday, Dec 04 is last lecture. 2 1

String object vs char value String Character type: String char non-primitive primitive operators: + + - / * % (arithmetic operators) cast to int 3 4 Recap: Strings are objects with benefits Creating strings is not different from creating any other object A String object, like any other object, has a state the state of a string object: the sequence of characters that is encapsulated However, string objects have some bonus features they can masquerade as primitive value they are efficient (but in exchange they are immutable) masquerade aspect #1 string objects can be specified using literal-like syntax String s = hello ; (** creation of new objects only conditionally) System.out.println( hello world ); masquerade aspect #2 string objects can participate in expressions just like primitive-value operands hello + 89 4 2

REMEMBER Any string is represented by an object A variable of type String is used to store the address of the object. The String object has a state the state of an object is defined as the value of all its attributes the only attribute of a String object is the attribute that represents the sequence of characters the state of a String object basically boils down to what is its sequence of characters? 5 5 REMEMBER If the state of a String object is such that its sequence has no characters at all, how do we understand this? this is the empty string the string has length zero THIS IS NOT A NULL STRING 6 What is this null string? technically speaking, null string is not really a correctly-formed term, there is no such thing HOWEVER, it is often used to mean a string reference that is set to null. This means that a String reference has been declared, but that there is NO String object. 6 3

Can we modify the state of a String object? NO Once a string object is created, it cannot be changed. This is called immutability Strings are immutable This is an unusual property MOST other objects are mutable 7 7 How to get String object from anything any object has tostring() method this also includes String objects, in which case tostring() is redundant do primitive values have a tostring() method? no so how do we transform? concatenate primitive value to the empty string String str1 = + 9; String str2 = + x ; 8 8 4

9 How to get primitive values from String objects suppose we have a sequence of characters suppose that sequences happens to be the same as a literal value from a primitive type e.g., 897 875l false C Use any of these static methods Integer.parseInt(str) Short.parseShort(str) Byte.parseByte(str) Long.parseLong(str) Double.parseDouble(str) Float.parseFloat(str) Boolean.parseBoolean(str) look at API, note the contract re: parameter java.lang.numberformatexception: Value out of range. 9 How to get primitive values from String objects suppose we have a one-character String and we want the corresponding char e.g., C d 9 there is a wrapper class Character(just like the others) unfo, there is no Character.parseCharacter(str) or other such static method instead: char c = C.charAt(0) 10 10 5

String methods, recap assume str1, str2 are strings; idx1, idx2 are integers 11 str1.length() returns an int tells us the number of characters in the object s character sequence str1.charat(idx1) returns a char gives us the character at the specified index remember the first character of a string that is n characters long is at index 0 and the last character is at index n-1 str1.equals(str2) returns a boolean tells us whether str2 has the same state as str1 not whether str2 is the same object as str1 substring(idx1,idx2) returns a String gives a subset of the character sequence from the start index inclusive to the end index exclusive 11 String methods, some new ones assume str1, str2 are strings; idx1, idx2 are integers 12 str1.touppercase() returns a String str2.tolowercase() returns a String these are NOT mutators each returns a String obj, which is an entirely new object that is modified version of str1 str1 is not changed at all (in fact, it cannot be changed, since it is immutable) str1.substring(idx1) returns a String just like str1.substring(idx1, idx2), with the assumption that idx2 is the length of str1 anything you do using str1.substring(idx1), you could also do with str1.substring(idx1, idx2) CONVINCE YOURSELVES OF THIS 12 6

String methods str1.indexof(str2) returns an int if str2 does not occur within str1, the method gives us the value -1 if str2 does occur within str1, the method gives us a value which is the index at which str2 occurs in str1 s character sequence if str2 occurs more than once within str1, the method gives us a value which is the index at which str2 first occurs in str1 s character sequence str1.indexof(str2, idx1) returns an int just like str1.indexof(str2), but the methods looks at str1 s character sequence only starting at index position idx1 onwards 13 13 But what if we need to modify the state of a String object? Instead of modifying the sequence, we just create new strings that are modified verisons of the originals. It is fast and easy, thanks to the + operator Given this, is it correct to say that String has mutators? not technically; they are actually generators of new modified objects 14 14 7

char : charat(int) method remember the indexing of the character positions starts at 0 str1.charat(idx1) returns a char gives us the character at the specified index remember the first character of a string that is n characters long is at index 0 and the last character is at index n-1 15 15 String : substring(int, int) method String : substring(int) method what do each of these methods do? these methods must return a brand new string substring(idx1,idx2) returns a String gives a subset of the character sequence from the start index inclusive to the end index exclusive Can you live w/o substring(int) given the overloaded (int,int)? 16 16 8

int : indexof(char) method int : indexof(char, int) method what do each of these methods do? str1.indexof(str2) returns an int if str2 does not occur within str1, the method gives us the value -1 if str2 does occur within str1, the method gives us a value which is the index at which str2 occurs in str1 s character sequence if str2 occurs more than once within str1, the method gives us a value which is the index at which str2 first occurs in str1 s character sequence str1.indexof(str2, idx1) returns an int just like str1.indexof(str2), but the methods looks at str1 s character sequence only starting at index position idx1 onwards 17 str1.substring(idx1) [REVISITED] just like str1.substring(idx1, idx2), with the assumption that idx2 is the length of str1 17 int : indexof(char) method int : indexof(char, int) method How would use use indexof to detect all occurrences of a substring? str1.substring(idx1) returns a String just like str1.substring(idx1, idx2), with the assumption that idx2 is the length of str1 anything you do using str1.substring(idx1), you could also do with str1.substring(idx1, idx2) CONVINCE YOURSELVES OF THIS 18 18 9

String : tostring() method boolean : equals(string) method Do not underestimate what equals does str1.equals(str2) returns a boolean tells us whether str2 has the same state as str1 not whether str2 is the same object as str1 19 19 String methods, recap str1.compareto(str2) returns an int gives us an int that is a coded message 0 if if str1 and str2 are equal polarity (the sign, +ve or ve) tells us whether str2 comes before str1 in the dictionary. dictionary uses lexicographic ordering if str1 and str2 are not equal, then the value is Unicode difference of the first differing character if there is no index position at which they differ, then the value is the length difference 20 20 10

String matching/comparison (basic) Suppose c1, c2 are chars Suppose s1, s2 are Strings 21 what does the equality boolean operator == tell us? boolean ismatch = c1==c2; boolean ismatch = s1==s2; what does.equals(string) tell us? boolean ismatch = s1.equals(s2); what does.compareto(string) tell us? int differingindexpos = s1.compareto(s2); 21 22 Elaboration of compareto(string) (sort of) tell me whether the passed string comes before this string in the dictionary aardvark.compareto( anvil ) anvil does not come before aardvark in the dictionary, so the result is no (negative value) anvil.compareto( aardvark ) aardvark does come before anvil in the dictionary, so the result is yes (positive value) (better) tell me whether the passed string comes before this string in the dictionary and, for the first character that is the determining 22 factor, what is the distance 11

str1.compareto(str2) returns an int gives us an int that is a coded message 0 if if str1 and str2 are equal polarity (the sign, +ve or ve) tells us whether str2 comes before str1 in the dictionary. dictionary uses lexicographic ordering if str1 and str2 are not equal, then the value is Unicode difference of the first differing character if there is no index position at which they differ, then the value is the length difference 23 23 24 String : touppercase() method String : tolowercase() method these methods must return a brand new string str1.touppercase() returns a String str2.tolowercase() returns a String these are NOT mutators each returns a String obj, which is an entirely new object that is modified version of str1 str1 is not changed at all (in fact, it cannot be changed, since it is immutable) 24 12

Comparing strings: equals vs matches suppose we have two strings, str1 and str2 str1.equals(str2) returns true iff str1 has the same state as str2 str1.matches(str2) returns true iff str1 matches the pattern as stipulated by str2 FOR NOW, WE WILL DO DEAD SIMPLE PATTERNS 25 25 hello.matches( hello ) in the context of being a parameter to matches, str2 is interpreted as a regular expression (aka REGEX) the REGEX specifies 5 criteria: hello.matches( hello ) REGEX criteria Criterion satisfied? that the character h is in index position 0 yes that the character e is in index position 1 yes that the character l is in index position 2 yes that the character l is in index position 3 yes that the character o is in index position 4 yes (implied) no further characters in the sequence yes 26 26 13

Regular expressions: Simple classes a regular expression can also use special characters and syntax to specify more patterns more generally [abc] defines a simple class of characters REGEX criteria hello.matches( [Hh]ello ) the character H or h is in index position 0 the character e is in index position 1 the character l is in index position 2 the character l is in index position 3 the character o is in index position 4 no further characters in the sequence str1 satisfies? yes yes yes yes yes yes L17App2 27 27 Regular expressions: Simple classes using a range [a-d] defines a simple class using a range REGEX criteria hello.matches( [a-d]ello ) the character a or b or c or d is in index position 0 the character e is in index position 1 the character l is in index position 2 the character l is in index position 3 the character o is in index position 4 no further characters in the sequence str1 satisfies? yes yes yes yes yes yes L17App3 28 28 14

Regular Expressions [a-d[f-h]] matches any of a,b,c,d,f,g,h the union of a-d and f-h [^a-d] matches any character that is NOT a, b, c, d, \d matches any digit same as: [0-9] \s matches any whitespace character: same as: [ \t\n\x0b\f\r] vertical tab is \xob, aka \u000b \w matches any word character: same as: [a-za-z_0-9] L17App4 L17App5 L17App6 L17App7 29 29 Regular Expressions a* matches zero or more a s a+ matches 1 or more a s a? matches 0 or 1 a s a{n,m} matches at least n a s but not more than m a s 30 30 15

31 Regular Expressions suppose we prompt the user for a time, with the instructions that the time must be one of 3, 6, or 9 am or pm acceptable: 9 am, 3 pm not acceptable: 10 am, 3 um, 9am, 9:00 am construct a regex to match this [369] [ap]m suppose we want to allow the space to be optional acceptable: 9am, 12 am, 12pm not acceptable: 10am, 9:00am construct a regex to match this [369]?[ap]m or [369][ ]?[ap]m 31 Numeric Strings Using the Wrapper Classes String s = 1020; int n1 = Integer.parseInt(s); long n2 = Long.parseLong(s); double n2 = Double.parseDouble(s); float n3 = Float.parseFloat(s); number to string conversions? best handled using the + operator 32 Copyright 16

33 How to get primitive values from String objects suppose we have a sequence of characters suppose that sequences happens to be the same as a literal value from a primitive type e.g., 897 875l false C Use any of these static methods Integer.parseInt(str) Short.parseShort(str) Byte.parseByte(str) Long.parseLong(str) Double.parseDouble(str) Float.parseFloat(str) Boolean.parseBoolean(str) look at API, note the contract re: parameter L17App1b L17App1c java.lang.numberformatexception: Value 33 out of range. How to get primitive values from String objects suppose we have a one-character String and we want the corresponding char e.g., C d 9 there is a wrapper class Character(just like the others) unfo, there is no Character.parseCharacter(str) or other such static method instead: char c = C.charAt(0) 34 34 17