Today Quiz in the lab, this week. Assignment 1 due Friday, this week. Continue useful classes from the java.lang package. (We did Math and wrapper classes,

Slides:



Advertisements
Similar presentations
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Advertisements

Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Fundamental Programming Structures in Java: Strings.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Recitation 2 Main Method, API & Packages, Java Basics.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Unit 3: Java Data Types Math class and String class.
Chapter 2 Elementary Programming
String Processing Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have questions.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Strings JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Today… Quiz 1 Topics and “Rules”. Some useful classes from the java.lang package. Includes: –static methods discussion. –The concept of mutability. Winter.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Today… Continue Building Expressions: –Literals –Constants –Casting –Array Declaration –Operators and Operator Precedence –Keywords Winter 2016CMPE212.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
Today… Style hints for Assignment 1. (Nothing else you need from lecture for assn 1.) Designing and writing methods. Some useful classes from the java.lang.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Last Time (Midterm Exam!) Before that – methods. Spring 2006
Yanal Alahmad Java Workshop Yanal Alahmad
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
CISC101 Reminders Quiz 2 this week.
Winter 2018 CISC101 11/16/2018 CISC101 Reminders
MSIS 655 Advanced Business Applications Programming
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
CISC101 Reminders Assn 3 due tomorrow, 7pm.
16 Strings.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
Fall 2018 CISC124 2/17/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. You are ready for exercises 4 and 5, and assignment.
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
CISC101 Reminders All assignments are now posted.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
String methods 26-Apr-19.
CISC101 Reminders All assignments are now posted.
Winter 2019 CMPE212 5/4/2019 CMPE212 – Reminders
Winter 2019 CMPE212 5/25/2019 CMPE212 – Reminders
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

Today Quiz in the lab, this week. Assignment 1 due Friday, this week. Continue useful classes from the java.lang package. (We did Math and wrapper classes, last week.) Character, System, String classes. Mutability. Not in java.lang: StringTokenizer. Winter 2015CMPE212 - Prof. McLeod1

Quiz 1 This Week Sample quiz “0” is linked to the grading page in the course web site. Solution not provided! (not by me anyways…) Up to and including last Friday’s lecture. Exercises 1 to 3 are fair game. Emphasis on expressions, loops, conditionals, arrays and writing small methods. Written in first hour of lab. 60 minutes, on paper, no aids. Pen or pencil. Let TA know if you cannot write at the start of the lab. Winter 2015CMPE212 - Prof. McLeod2

Winter 2015CMPE212 - Prof. McLeod3 Wrapper Classes – Cont. The Character wrapper class: –has methods to convert between ASCII and Unicode numeric values and characters. –isDigit(character) returns true if character is a digit. –isLetter(character) –isLetterOrDigit(character) –isUpperCase(character) –isLowerCase(character) –isWhitespace(character) –toLowerCase() –toUpperCase() –… static non-static

System Class We have used: –System.out.println() –System.out.print() –System.out.printf() Also: –System.err.println() Winter 2015CMPE212 - Prof. McLeod4

Winter 2015CMPE212 - Prof. McLeod5 Other Useful System Class Methods System.currentTimeMillis() –Returns, as a long, the number of milliseconds elapsed since midnight Jan. 1, System.exit(0) –Immediate termination of your program. System.getProperties() –All kinds of system specific info - see the API. System.getProperty(string) –Displays single system property. System.nanoTime() –Time in nanoseconds

Winter 2015CMPE212 - Prof. McLeod6 String literals: “Press to continue.” String variable declaration: String testStuff; or: String testStuff = “A testing string.”; String concatenation (“addition”): String testStuff = “Hello”; System.out.println(testStuff + “ to me!”); Would print the following to the console window: Hello to me! String s, so Far

Winter 2015CMPE212 - Prof. McLeod7 String s - Cont. Escape sequences in Strings: These sequences can be used to put special characters into a String: \” a double quote \’ a single quote \\ a backslash \n a linefeed \r a carriage return \t a tab character

Winter 2015CMPE212 - Prof. McLeod8 String s, so Far - Cont. For example, the code: System.out.println(“Hello\nclass!”); prints the following to the screen: Hello class!

Winter 2015CMPE212 - Prof. McLeod9 String Class - Cont. Since String’ s are Objects they can have methods. String methods (67 of them!) include: length() equals(OtherString) equalsIgnoreCase(OtherString) toLowerCase() toUpperCase() trim() charAt(Position) substring(Start) substring(Start, End)

Winter 2015CMPE212 - Prof. McLeod10 String Class - Cont. indexOf(SearchString) replace(oldChar, newChar) startsWith(PrefixString) endsWith(SuffixString) valueOf(integer) String ’s do not have any attributes. See the API Docs for details on all the String class methods.

Winter 2015CMPE212 - Prof. McLeod11 A few examples: int i; boolean aBool; String testStuff = “A testing string.”; i = testStuff.length(); // i is 17 aBool = testStuff.equals(“a testing string.”); // aBool is false aBool = testStuff.equalsIgnoreCase(“A TESTING STRING.”); // aBool is true String Class - Cont.

Winter 2015CMPE212 - Prof. McLeod12 String Class - Cont. char aChar; aChar = testStuff.charAt(2); // aChar is ‘t’ i = testStuff.indexOf(“test”); // i is 2

Winter 2015CMPE212 - Prof. McLeod13 Aside - More about String ’s Is “Hello class” (a String literal) an Object? Yup, “Hello class!”.length() would return 12. Also, String’s are immutable – meaning that they cannot be altered, only re-assigned. There are no methods that can alter characters inside a string while leaving the rest alone. Arrays are mutable, in contrast – any element can be changed

Mutability, Expanded Arrays are mutable. You can access individual elements in an array using [ ] on the LHS or RHS of an assignment operator: int[] test = {1, 2, 3, 4, 5}; test[2] = 30; // array is now {1, 2, 30, 4, 5} int aVar = test[4]; // aVar holds 5 Winter 2015CMPE212 - Prof. McLeod14

Mutability, Cont. String objects are not mutable. You can obtain individual characters from a String : String testStr = "I am a string!"; char aChar = testStr.charAt(3); // aChar is ' m ' You cannot do: testStr.charAt(3) = ' M ' ; Winter 2015CMPE212 - Prof. McLeod15

Mutability, Cont. In fact, no method can be invoked on the LHS of an assignment operator. And, you have no other way to get at individual characters or substrings in a string. So, you can only re-assign strings. For example, how would I capitalize the 'm' ? Winter 2015CMPE212 - Prof. McLeod16

Mutability, Cont. Since there is only one 'm', I can use: String otherStr = testStr.replace('m', 'M'); Or to replace just the first 'm' : int mLoc = testStr.indexOf('m'); otherStr = testStr.substring(0, mLoc) + 'M' + testStr.substring(mLoc + 1); None of this changes testStr ! Winter 2015CMPE212 - Prof. McLeod17

Mutability, Cont. So, a String object can only be re-assigned if it is to be changed. In your classes you control mutability by having private attributes and deciding if you will supply mutators or not – more on this stuff soon! Winter 2015CMPE212 - Prof. McLeod18

Exercise 4 Practice with strings and characters. Winter 2015CMPE212 - Prof. McLeod19

Other java.lang Classes Object is the base class for all objects in Java. We’ll need to learn about object hierarchies for this to make more sense. Thread is a base class used to create threads in multi-threaded program. More about this topic near the end of the course. A class not in java.lang: Winter 2015CMPE212 - Prof. McLeod20

Winter 2015CMPE212 - Prof. McLeod21 StringTokenizer Class This useful class is in the “java.util” package, so you need to have an import java.util.*; or import.java.util.StringTokenizer; statement at the top of your program. This class provides an easy way of parsing strings up into pieces, called “tokens”. Tokens are separated by “delimiters”, that you can specify, or you can accept a list of default delimiters.

Winter 2015CMPE212 - Prof. McLeod22 StringTokenizer Class - Cont. The constructor method for this class is overloaded. So, when you create an Object of type StringTokenizer, you have three options: new StringTokenizer(String s) new StringTokenizer(String s, String delim) new StringTokenizer(String s, String delim, boolean returnTokens)

Winter 2015CMPE212 - Prof. McLeod23 StringTokenizer Class - Cont. s is the String you want to “tokenize”. delim is a list of delimiters, by default it is: “ \t\n\r” or space, tab, line feed, carriage return. You can specify your own list of delimiters if you provide a different String for the second parameter.

Winter 2015CMPE212 - Prof. McLeod24 StringTokenizer Class - Cont. If you supply a true for the final parameter, then delimiters will also be provided as tokens. The default is false - delimiters are not provided as tokens.

Winter 2015CMPE212 - Prof. McLeod25 StringTokenizer Class - Cont. Here is some example code: String aString = "This is a String - Wow!"; StringTokenizer st = new StringTokenizer(aString); System.out.println("The String has " + st.countTokens() + " tokens."); System.out.println("\nThe tokens are:"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } // end while

Winter 2015CMPE212 - Prof. McLeod26 StringTokenizer Class - Cont. Screen output: The String has 6 tokens. The tokens are: This is a String - Wow!

Winter 2015CMPE212 - Prof. McLeod27 Note that the StringTokenizer object is emptied out as tokens are removed from it. You will need to re-create the object in order to tokenize it again. StringTokenizer Class - Cont.

Winter 2015CMPE212 - Prof. McLeod28 The Scanner class has a tokenizer built into it. In the latest Java versions, Scanner uses a regular expression or “regex” instead of the (easier to understand, but less powerful!) delimiter list. The default regex is: “\p{javaWhitespace}+”, which means “any number of whitespace characters”. A whitespace character is a space, a tab, a linefeed, formfeed or a carriage return. “ \t\n\f\r” in other words. Scanner Class Tokenizer

Winter 2015CMPE212 - Prof. McLeod29 Tokenizing Demo See SystemPropertiesDemo.java Includes some old-fashioned string parsing code that uses String class methods only.