Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2019 CMPE212 5/4/2019 CMPE212 – Reminders

Similar presentations


Presentation on theme: "Winter 2019 CMPE212 5/4/2019 CMPE212 – Reminders"— Presentation transcript:

1 Winter 2019 CMPE212 5/4/2019 CMPE212 – Reminders First quiz in the lab next week, starting Monday. More info in Tuesday’s lecture. You are in two groups in onQ – your Lab and your Grader. Assignment 1 due next Friday. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

2 Today Useful Classes in the java.lang package, Cont. Math
Wrapper Classes System String StringTokenizer in java.util Method Overloading (if we have time) Winter 2019 CMPE212 - Prof. McLeod

3 java.lang This is the only package (and all its interfaces, enums, classes, Exceptions, Errors and annotations) that is automatically imported into every Java program. If you need something that is in another package (and there are lots of them!), you will need to write a specific import statement for that class or for the entire package containing that class. Your IDE should be able to help you create the appropriate import statement – Eclipse does. Winter 2019 CMPE212 - Prof. McLeod

4 Fall 2013 CMPE212 Aside - static Methods Many of these java.lang class are utilitarian in nature – they contain many 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 next time(s) they are invoked. Generally, they are utility methods that do not depend on the values of a class’ attributes. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

5 static Methods, Cont. static methods can be invoked without instantiation of the Object that owns them. Math.random(), for example. 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. Winter 2019 CMPE212 - Prof. McLeod

6 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 look over the API Docs. Winter 2019 CMPE212 - Prof. McLeod

7 Fall 2013 CMPE212 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. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

8 Wrapper Classes - Cont. 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 Winter 2019 CMPE212 - Prof. McLeod

9 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 Winter 2019 CMPE212 - Prof. McLeod

10 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 Docs for other methods and constants dealing with NaN and -Infinity and Infinity Winter 2019 CMPE212 - Prof. McLeod

11 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() Winter 2019 CMPE212 - Prof. McLeod

12 System Class We have used: System.out.println() System.out.print()
System.out.printf() Also: System.err.println() Winter 2019 CMPE212 - Prof. McLeod

13 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 Winter 2019 CMPE212 - Prof. McLeod

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

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

16 Strings, so Far - Cont. For example, the code:
System.out.println(“Hello\nclass!”); prints the following to the screen: Hello class! Winter 2019 CMPE212 - Prof. McLeod

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

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

19 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 Winter 2019 CMPE212 - Prof. McLeod

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

21 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. Winter 2019 CMPE212 - Prof. McLeod

22 Aside – More Exercises Exericse 5 (Palindromes) is on strings.
Exercise 6 will give you more practice with modular program design and get you thinking about Object Oriented design. Winter 2019 CMPE212 - Prof. McLeod

23 Other java.lang Classes
Fall 2013 CMPE212 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. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

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

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

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

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

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

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

30 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. Winter 2019 CMPE212 - Prof. McLeod

31 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. Winter 2019 CMPE212 - Prof. McLeod

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

33 Fall 2013 CMPE212 Method Overloading A method can have the same name in many different classes (println(), for example). “Overloading” is when a method name is used more than once in method declarations within the same class. (also like println()…) The rule is that no two methods with the same name within a class can have the same number and/or types of parameters in the method declarations. (The “NOT” rule.) Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

34 Method Overloading - Cont.
Why bother? – Convenience! Java does not have default arguments. Allows the user to call a method without requiring him to supply values for all the parameters. One method name can be used with many different types and combinations of parameters. Allows the programmer to keep an old method definition in the class for “backwards compatibility”. Winter 2019 CMPE212 - Prof. McLeod

35 Method Overloading - Cont.
How does it work? Java looks through all methods until the parameter types match with the list of arguments supplied by the user. If none match, Java tries to cast types in order to get a match. (Only “widening” casting like int to double, however.) Winter 2019 CMPE212 - Prof. McLeod

36 Method Overloading - Cont.
Final notes on overloading: You can have as many overloaded method definitions as you want, as long as they are differentiated by the type and/or number of the parameters listed in the definition. Do not change the return type – that is tacky! See the getInt() and getDouble() overloaded methods in the Exercise 1 IOHelper class for example. Winter 2019 CMPE212 - Prof. McLeod


Download ppt "Winter 2019 CMPE212 5/4/2019 CMPE212 – Reminders"

Similar presentations


Ads by Google