today quiz in the lab, this week. assignment 1 due friday, this week. continue useful classes from...

29
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 2015 CMPE212 - Prof. McLeod 1

Upload: jamal-plowden

Post on 15-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 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,

CMPE212 - Prof. McLeod 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 2015

Page 2: 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,

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 2015 CMPE212 - Prof. McLeod 2

Page 3: 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,

Winter 2015 CMPE212 - Prof. McLeod 3

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

Page 4: 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,

System Class

• We have used:– System.out.println()– System.out.print()– System.out.printf()

• Also:– System.err.println()

Winter 2015 CMPE212 - Prof. McLeod 4

Page 5: 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,

Winter 2015 CMPE212 - Prof. McLeod 5

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

Page 6: 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,

Winter 2015 CMPE212 - Prof. McLeod 6

• String literals:

“Press <enter> 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!

Strings, so Far

Page 7: 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,

Winter 2015 CMPE212 - Prof. McLeod 7

Strings - 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

Page 8: 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,

Winter 2015 CMPE212 - Prof. McLeod 8

Strings, so Far - Cont.

• For example, the code:

System.out.println(“Hello\nclass!”);

prints the following to the screen:

Helloclass!

Page 9: 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,

Winter 2015 CMPE212 - Prof. McLeod 9

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)

Page 10: 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,

Winter 2015 CMPE212 - Prof. McLeod 10

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.

Page 11: 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,

Winter 2015 CMPE212 - Prof. McLeod 11

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

Page 12: 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,

Winter 2015 CMPE212 - Prof. McLeod 12

String Class - Cont.

char aChar;

aChar = testStuff.charAt(2); // aChar is ‘t’

i = testStuff.indexOf(“test”); // i is 2

Page 13: 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,

Winter 2015 CMPE212 - Prof. McLeod 13

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

Page 14: 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,

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 2015 CMPE212 - Prof. McLeod 14

Page 15: 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,

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 2015 CMPE212 - Prof. McLeod 15

Page 16: 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,

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 2015 CMPE212 - Prof. McLeod 16

Page 17: 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,

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 2015 CMPE212 - Prof. McLeod 17

Page 18: 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,

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 2015 CMPE212 - Prof. McLeod 18

Page 19: 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,

Exercise 4

• Practice with strings and characters.

Winter 2015 CMPE212 - Prof. McLeod 19

Page 20: 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,

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 2015 CMPE212 - Prof. McLeod 20

Page 21: 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,

Winter 2015 CMPE212 - Prof. McLeod 21

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.

Page 22: 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,

Winter 2015 CMPE212 - Prof. McLeod 22

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)

Page 23: 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,

Winter 2015 CMPE212 - Prof. McLeod 23

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.

Page 24: 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,

Winter 2015 CMPE212 - Prof. McLeod 24

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.

Page 25: 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,

Winter 2015 CMPE212 - Prof. McLeod 25

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

Page 26: 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,

Winter 2015 CMPE212 - Prof. McLeod 26

StringTokenizer Class - Cont.

• Screen output:

The String has 6 tokens.

The tokens are:ThisisaString-Wow!

Page 27: 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,

Winter 2015 CMPE212 - Prof. McLeod 27

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

Page 28: 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,

Winter 2015 CMPE212 - Prof. McLeod 28

• 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

Page 29: 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,

Winter 2015 CMPE212 - Prof. McLeod 29

Tokenizing Demo

• See SystemPropertiesDemo.java

• Includes some old-fashioned string parsing code that uses String class methods only.