Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1 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 2. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

2 What’s Next? Some Odds ‘n Ends before we can start OOP:
Classes in java.lang, including the String class. StringTokenizer class. Method overloading. Exceptions and catching them. Arrays in memory. Aliasing objects. Passing objects by reference. Fall 2018 CISC124 - Prof. McLeod

3 Today Useful Classes in the java.lang package. (All classes in java.lang are imported automatically in every program.) Use the Java API docs as we go! Fall 2018 CISC124 - Prof. McLeod

4 Some Useful Java Classes
The classes defined in the java.lang package are automatically imported for you, since they are used quite often. They include: The Wrapper classes Math Object String System Thread (How do I know all this?...) Fall 2018 CISC124 - Prof. McLeod

5 Fall 2013 CISC124 Aside - static Methods static attributes and methods are loaded once into memory and not garbage collected until main is finished. These methods will run faster the second time (and later) they are invoked. Generally, they are utility methods that do not depend on the values of a class’ attributes. static methods can be invoked without instantiation of the Object that owns them. Math.random(), for example. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

6 static Methods, Cont. static methods and attributes are shared by all instances of a class – there is only one copy of these methods in memory. A static method can only invoke other static methods in its own class – you can’t have pieces of code disappearing from a static method in memory... This is all done for reasons of ease of use and efficiency. Fall 2018 CISC124 - Prof. McLeod

7 Math Class As you would expect:
A collection of static constants and static mathematical methods. You cannot instantiate the Math class, but why would you want to? Let’s just look over the API. Fall 2018 CISC124 - Prof. McLeod

8 Fall 2013 CISC124 Wrapper Classes Sometimes it is necessary for a primitive type value to be an Object, rather than just a primitive type. Some data structures only store Objects. Some Java methods only work on Objects. Wrapper classes also contain some useful constants and a few handy methods. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

9 Wrapper Classes - Cont. char Character int Integer long Long float
Each primitive type has an associated wrapper class: Each wrapper class Object can hold the value that would normally be contained in the primitive type variable, but now has a number of useful static methods. char Character int Integer long Long float Float double Double Fall 2018 CISC124 - Prof. McLeod

10 Wrapper Classes - Cont. Integer number = new Integer(46);//”Wrapping”
Integer num = new Integer(“908”); Integer.MAX_VALUE // gives maximum integer Integer.MIN_VALUE // gives minimum integer Integer.parseInt(“453”) // returns 453 Integer.toString(653) // returns “653” number.equals(num) // returns false int aNumber = number.intValue(); // aNumber is 46 Fall 2018 CISC124 - Prof. McLeod

11 Wrapper Classes – Cont. The Double wrapper class has equivalent methods: Double.MAX_VALUE // gives maximum double value Double.MIN_VALUE // gives minimum double value Double.parseDouble(“0.45E-3”) // returns 0.45E-3 See the API for other methods and constants dealing with NaN and -Infinity and Infinity Fall 2018 CISC124 - Prof. McLeod

12 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() Fall 2018 CISC124 - Prof. McLeod

13 System Class We have used: System.out.println() System.out.print()
System.out.printf() Also: System.err.println() Fall 2018 CISC124 - Prof. McLeod

14 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 Fall 2018 CISC124 - Prof. McLeod

15 Strings, so Far String literals: “Press <enter> to continue.”
Fall 2013 CISC124 Strings, so Far 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! Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

16 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 Fall 2018 CISC124 - Prof. McLeod

17 Strings, so Far - Cont. For example, the code:
System.out.println(“Hello\nclass!”); prints the following to the screen: Hello class! Fall 2018 CISC124 - Prof. McLeod

18 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) Fall 2018 CISC124 - Prof. McLeod

19 String Class - Cont. String’s do not have any attributes.
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. Fall 2018 CISC124 - Prof. McLeod

20 String Class - Cont. 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 Fall 2018 CISC124 - Prof. McLeod

21 String Class - Cont. char aChar;
aChar = testStuff.charAt(2); // aChar is ‘t’ i = testStuff.indexOf(“test”); // i is 2 Fall 2018 CISC124 - Prof. McLeod

22 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. Fall 2018 CISC124 - Prof. McLeod

23 Aside – More Exercises Exericse 6 (Palindromes) is on strings.
Exercise 7 will give you more practice with modular program design and get you thinking about Object Oriented design. Fall 2018 CISC124 - Prof. McLeod

24 Other java.lang Classes
Fall 2013 CISC124 Other java.lang Classes Object is the base class for all objects in Java. We’ll need to learn about object hierarchies (Inheritance) 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. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

25 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. Fall 2018 CISC124 - Prof. McLeod

26 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) Fall 2018 CISC124 - Prof. McLeod

27 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. Fall 2018 CISC124 - Prof. McLeod

28 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. Fall 2018 CISC124 - Prof. McLeod

29 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 Fall 2018 CISC124 - Prof. McLeod

30 StringTokenizer Class - Cont.
Screen output: The String has 6 tokens. The tokens are: This is a String - Wow! Fall 2018 CISC124 - Prof. McLeod

31 StringTokenizer Class - Cont.
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. Fall 2018 CISC124 - Prof. McLeod

32 Scanner Class Tokenizer
The Scanner class has a tokenizer built into it. 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. Fall 2018 CISC124 - Prof. McLeod

33 Tokenizing Demo See SystemPropertiesDemo.java
Includes some old-fashioned string parsing code that uses String class methods only. Fall 2018 CISC124 - Prof. McLeod


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

Similar presentations


Ads by Google