Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations

Presentation on theme: "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,"— Presentation transcript:

1 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

2 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

3 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

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

5 Winter 2015CMPE212 - Prof. McLeod5 Other Useful System Class Methods System.currentTimeMillis() –Returns, as a long, the number of milliseconds elapsed since midnight Jan. 1, 1970. 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

6 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

7 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

8 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!

9 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)

10 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.

11 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.

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

13 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

14 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

15 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

16 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

17 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

18 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

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

20 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

21 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; 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.

22 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)

23 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.

24 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.

25 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

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

27 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.

28 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

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

Download ppt "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,"

Similar presentations

Ads by Google