CS226 -- Day 06 - Spring 2013 GENERICS --------------------------------------------------- - static generic methods public static returnType method(T thing) - Generics can have bounds to qualify groups of appropriate types - - use ? as wildcard - can be - can be > (ie, for ordered collections) Comparable - java.lang http://docs.oracle.com/javase/tutorial/index.html - includes Generics and the Collections Framework - we'll come back to these ---------------------------------------------------------- Using Unix - We looked at using our Unix accounts, or using Terminal directly on a Mac. There are a bunch of links to resources for learning Unix and the emacs or vi editors on the course homepage - read them! From a windows machine, (download and) use puTTY to remotely log into your unix account. From your Mac terminal window, ssh for remote access: > ssh -l login ugrad#.cs.jhu.edu where login is your usernane, and pick a # for a ugrad machine. Common commands: ls - list files pwd - print working directory mkdir - make new directory cd - change directory . - current directory .. - parent directory mv - move (rename) file Downloading files from the internet to your CS unix account: > wget url Downloading files from the internet to your Mac - just right click and save as wherever you want them. The file system through the terminal window is the same as through the Mac finder. We downloaded these files: SearcherBetter.java, ArrayNotFullException.java checkstyle-5.6-all.jar, jhu_checks.xml timeSomething.java Students then worked to eliminate checkstyle errors from SearcherBetter.java. Here were the first two we dealt with: 1) Utility class error: This happens with classes that have main and other methods, but are not being instantiated to create objects. To eliminate the error, create an empty private Constructor() { } method. 2) Indentation errors: use 4 spaces (not tabs) for every indentation level. This way your code displays consistently no matter what program is used to view or edit it. Emacs - we tried some commands in emacs. Also, note that in order to scroll your file within emacs you use emacs commands, not the mouse window scroller (that only scrolls your unix commands). saving file: ^x s exiting: ^x ^c search-replace: esc-% page (scroll) down: ^v page (scroll) up: esc-v You can open two unix windows, keep you file open in emacs in one, and then do your compiling and checkstyle in the other. Checkstyle Basics ---------------------------------------------------- Enforcing coding standards: Many companies have specific coding standards to insure consistency and readability of code. The main standard for Java comes from the Sun Code Conventions. There is a much-used tool for checking the style of your java code that we will be using called Checkstyle. This is a program that evaluates your code with respect to many different coding conventions, such as whitespace, where to place {}, naming conventions, proper commenting, etc. The program is configurable with xml files in order to customize styles. You can read more about it here: http://checkstyle.sourceforge.net/ You will need to download the checkstyle-5.6-bin.zip archive from http://checkstyle.sourceforge.net/ in order to run checkstyle. Unzip the archive and locate the file checkstyle-5.6-all.jar within it. Put that file into the same directory all your code is in. (You can ignore all the other files in the archive.) You will also need our custom configuration file which is located here: http://www.cs.jhu.edu/~joanne/cs226/notes/jhu_checks.xml Copy that file into the same directory all your code is in as well. In order to run checkstyle on a Java source file you have to use the following command: > java -jar checkstyle-5.6-all.jar -c jhu_checks.xml SomeJavaFile.java This will execute the checkstyle program which is in a jar (java archive) using the specified configuration file on your java source code. You will then see a (most likely) long list of errors. In many cases the errors indicate what must be changed in order to fix things and bring your code up to standard. When in doubt, consult the checkstyle on-line documentation to find out what the expectations are. Note: Checkstyle is meant to be used with programs that are compiling. If you have compiler errors, checkstyle will probably only give you a limited set of errors because it can't parse your program properly. Checkstyle plug-ins for those of you using Eclipse are available on the download website as well. I'm not sure if there's support in jGrasp, but if you find some feel free to share with all on Piazza. ------------------------------------------------------------ So why did we use Unix? 1) I wanted you to have exposure to it. 2) Working in Unix and emacs/vim/nano can be very fast once you get the hang of it. Lots of coders actually prefer it. 3) We will use Eclipse with Checkstyle as well, but I have found Eclipse to be a big battery drain on my laptop. Working in unix is less of a drain.