Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today… Quiz 1 Topics and “Rules”. Some useful classes from the java.lang package. Includes: –static methods discussion. –The concept of mutability. Winter.

Similar presentations


Presentation on theme: "Today… Quiz 1 Topics and “Rules”. Some useful classes from the java.lang package. Includes: –static methods discussion. –The concept of mutability. Winter."— Presentation transcript:

1 Today… Quiz 1 Topics and “Rules”. Some useful classes from the java.lang package. Includes: –static methods discussion. –The concept of mutability. Winter 2016CMPE212 - Prof. McLeod1

2 Quiz 1 Topics Everything up to and including Thursday’s (Jan. 21) lecture. Assignment 1. Exercises 1, 3 and 5 are fair game. Winter 2016CMPE212 - Prof. McLeod2

3 Quiz 1 Topics, Cont. Java history and operation. Primitive types, variable declaration and initialization, expressions, conditionals and loops. Type casting of primitive types. Screen (or "console") output using System.out.println() and System.out.printf(). Screen input using the Scanner class. Arrays – one and 2D. Methods and attributes – use, declaration and coding. String class. Method overloading. Catching Exceptions. Winter 2016CMPE212 - Prof. McLeod3

4 Quiz 1 Format Electronic – in Moodle. T/F, short answer. Short answer could be a code segment, a complete method, or possibly a complete program. Code writing will be towards the end of the quiz – leave yourself enough time!!! You will need to be able to read and write procedural Java code. Winter 2016CMPE212 - Prof. McLeod4

5 Quiz 1 Rules Written in the first part of the lab in week 4. One hour or less – time will be limited by Moodle and will count down from when you open the quiz. The quiz will be available for the entire lab time only. You must use a lab machine. Have your student card out beside you the entire time. You will sign beside your name on an attendance form circulated by your TA. If you do not sign, your quiz will not count. Winter 2016CMPE212 - Prof. McLeod5

6 Quiz 1 Rules, Cont. You will use Moodle and may use an IDE, but no other programs can be used. No other aids. Make sure you have enough time to use an IDE, if you choose to do so. (The final exam is on paper – no aids.) The TA must be able to see your screen. No talking to other students when writing. If you have written, please keep the noise down until all other students have finished writing the quiz. Winter 2016CMPE212 - Prof. McLeod6

7 Quiz 1 Rules, Cont. (sigh…) Do not discuss the quiz with students who have not written it. Do not provide quiz answers to students who have not written it. The TAs will note the names of any “violators” on the attendance sheet and their quizzes will become zero! The latter half of the lab is available for assignment help. Winter 2016CMPE212 - Prof. McLeod7

8 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 Winter 2016CMPE212 - Prof. McLeod8

9 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. Winter 2016CMPE212 - Prof. McLeod9

10 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. Winter 2016CMPE212 - Prof. McLeod10

11 Math Class As you would expect: A collection of static constants and static, mathematical methods. You cannot instantiate the Math class, BTW… Let’s just look over the API Docs. Winter 2016CMPE212 - Prof. McLeod11

12 Winter 2016CMPE212 - Prof. McLeod12 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.

13 Winter 2016CMPE212 - Prof. McLeod13 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. charCharacter intInteger longLong floatFloat doubleDouble

14 Winter 2016CMPE212 - Prof. McLeod14 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

15 Winter 2016CMPE212 - Prof. McLeod15 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

16 Winter 2016CMPE212 - Prof. McLeod16 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

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

18 Winter 2016CMPE212 - Prof. McLeod18 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

19 Winter 2016CMPE212 - Prof. McLeod19 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

20 Winter 2016CMPE212 - Prof. McLeod20 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

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

22 Winter 2016CMPE212 - Prof. McLeod22 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)

23 Winter 2016CMPE212 - Prof. McLeod23 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.

24 Winter 2016CMPE212 - Prof. McLeod24 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.

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

26 Winter 2016CMPE212 - Prof. McLeod26 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

27 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 2016CMPE212 - Prof. McLeod27

28 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 2016CMPE212 - Prof. McLeod28

29 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 2016CMPE212 - Prof. McLeod29

30 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 2016CMPE212 - Prof. McLeod30

31 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. Winter 2016CMPE212 - Prof. McLeod31


Download ppt "Today… Quiz 1 Topics and “Rules”. Some useful classes from the java.lang package. Includes: –static methods discussion. –The concept of mutability. Winter."

Similar presentations


Ads by Google