CISC124 Assignment 1 due this Friday at 7pm by submission to an onq dropbox. First onq quiz next week write in lab. More details in yesterday s lecture. Today Intro. to 2D Arrays (and iteration examples). Useful Java classes. Fall 2017 CISC124 - Prof. McLeod 1 Fall 2017 CISC124 - Prof. McLeod 2 Introduction to Multi-Dimensional Arrays We have discussed the declaration of 1D arrays. For example: double[] oned = new double[100]; To assign values, for example: for (int i = 0; i < oned.length; i++) oned[i] = i; For each dimension, just add another set of [ ]: int[][] twod = new int[4][20]; Has room for 80 values. To assign, for example: int row, col; for (row = 0; row < twod.length; row++) for (col = 0; col < twod[row].length; col++) twod[row][col] = row * col; Fall 2017 CISC124 - Prof. McLeod 3 Fall 2017 CISC124 - Prof. McLeod 4 It is helpful to think of a 2D array as storing tabular data, like a spreadsheet. For convenience (and to help you code consistently) you can think of the first dimension as the rows and the second dimension as the columns. To get the number of rows, use twod.length, and to get the number of columns, grab one of the rows and use twod[0].length. You can use three sets of [ ] to get a 3 dimensional array. Using the spreadsheet analogy, the third dimension could be the sheet number, where each sheet contains a table. For a good analogy for a 4D array you might need to look up a Star Trek script Fall 2017 CISC124 - Prof. McLeod 5 Fall 2017 CISC124 - Prof. McLeod 6 Prof. Alan McLeod 1
See TestLoops.java Iterating 2D Arrays Can a for/each loop be used to assign or alter values in an array? Fall 2017 CISC124 - Prof. McLeod 7 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 2017 CISC124 - Prof. McLeod 8 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. 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 2017 CISC124 - Prof. McLeod 9 Fall 2017 CISC124 - Prof. McLeod 10 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. 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 2017 CISC124 - Prof. McLeod 11 Fall 2017 CISC124 - Prof. McLeod 12 Prof. Alan McLeod 2
Wrapper Classes - Cont. Each primitive type has an associated wrapper class: char int long float double Character Integer Long Float Double 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. 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 2017 CISC124 - Prof. McLeod 13 Fall 2017 CISC124 - Prof. McLeod 14 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 2017 CISC124 - Prof. McLeod 15 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 2017 CISC124 - Prof. McLeod 16 System Class Other Useful System Class Methods We have used: System.out.println() System.out.print() System.out.printf() Also: System.err.println() 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 2017 CISC124 - Prof. McLeod 17 Fall 2017 CISC124 - Prof. McLeod 18 Prof. Alan McLeod 3
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! 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 2017 CISC124 - Prof. McLeod 19 Fall 2017 CISC124 - Prof. McLeod 20 Strings, so Far - Cont. For example, the code: System.out.println( Hello\nclass! ); prints the following to the screen: Hello class! 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 2017 CISC124 - Prof. McLeod 21 Fall 2017 CISC124 - Prof. McLeod 22 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. 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 Fall 2017 CISC124 - Prof. McLeod 23 abool = teststuff.equalsignorecase( A TESTING STRING. ); // abool is true Fall 2017 CISC124 - Prof. McLeod 24 Prof. Alan McLeod 4
char achar; achar = teststuff.charat(2); // achar is t i = teststuff.indexof( test ); // i is 2 Fall 2017 CISC124 - Prof. McLeod 25 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 2017 CISC124 - Prof. McLeod 26 Aside - Exercises You are now ready for exercises 4, 5 and 6: Exercise 4 on File I/O the content will not be discussed in class. Exercise 5 will give you practice with the String and Character classes. Exercise 6 will give you more practice with modular program design and get you thinking about Object Oriented design. 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 2017 CISC124 - Prof. McLeod 27 Fall 2017 CISC124 - Prof. McLeod 28 Prof. Alan McLeod 5