CS226 -- Day 09 -- Spring 2013 USING ECLIPSE - configuring to work with checkstyle, auto-formatting Here are the steps you need to get this all working together: 1) Install the Eclipse checkstyle plug-in: Help -> Install Software -> http://eclipse-cs.sf.net/update/, restart Eclipse 2) Go to Preferences (Window menu on PC, Eclipse menu on Mac) -> Checkstyle, click new to add the jhu_checks.xml configuration file, as either an external file if saved to your computer, or a remote file (http://www.cs.jhu.edu/~joanne/cs226/notes/jhu_checks.xml) - check caching in this case. Then select the jhu_checks configuration and "Set as Default". 3) For each project that you want to configure to work with checkstyle: right click the project in the package list -> Checkstyle -> Activate Checkstyle. 4) Again from Preferences -> Java -> Code Style set Clean-up and Formatter to be the styles you need to satisfy checkstyle, and save as your own custom profile. Specifically, in Formatter, under the Indentation tab change the Tab Policy drop down to Spaces Only. These files are eclipse specific, not quite the same as the checkstyle configuration files. They are the configurations for the next step. 5) Once your project is configured, everytime you save a file it will update your checkstyle issues. In order to clean up your file using the settings above, go to the Source menu -> Clean up and Format and it will change things according to the settings from 4) so it's important to make those match the expected checkstyle. 6) Eclipse Preferences are workspace specific. So if you create a new workspace and want the same settings, you'll have to repeat steps 2-4. Eclipse and JUnit: from File -> New -> JUnit Test Case. Your JUnit files do not have to conform to Checkstyle. ------------------------------------------------------- ADT Bag: a collection of objects of the same type Examples: candy bag, Scrabble tiles, Santa Claus' sack of toys Assumptions: duplicates allowed, unordered, unlimited size, unindexed (not positional) Operations: add, create, clear, size, isEmpty, remove, list, contains Java Interface Option #1 - Using Object public interface Bag { public void add(Object o); public Object remove(); public Object remove(Object o); // overload option public int size(); public boolean contains(Object o); public void isEmpty(); } Java Interface Option #2 - Using Generics (file Bag.java) If all objects must be of the same base type in the Bag. public interface Bag { public void add(T o); public T remove(); public T remove(T o); // overload option public int size(); public boolean contains(T o); } Now, in order to really specify how these operations work, we need an algebraic specification. This is a little tricky because duplicate items are allowed. So we need an operation that's not part of the interface in order to count how many of each value are in the Bag. ADT Bag uses Any, Boolean, Integer defines Bag operations new: ---> Bag size: Bag ---> Integer add: Bag x T ---> Bag remove: Bag x T -/-> Bag remove: Bag -/-> Bag contains: Bag x T ---> Boolean count: Bag x T ---> Integer isEmpty: Bag ---> Boolean clear: Bag ---> Bag preconditions remove(b, t): contains(b,t) remove(b): not isEmpty(b) axioms size(new()) = 0 count(new(), t) = 0 size(add(b,t)) = size(b) + 1 size(remove(b,t)) = size(b) - 1 size(remove(b)) = size(b) - 1 count(add(b,t),v) = if t = v then count(b,t) + 1 else count(b,v) count(remove(b,t),v) = if t = v then count(b,t) - 1 else count(b,v) contains(b,t) = if count(b,t) = 0 then false else true isEmpty(b) = if size(b) = 0 then true else false size(clear(b)) = 0 count(clear(b),t) = 0 We could implement either Bag interface in many different ways. How will we know if an implementation is correct? We need to also design good test cases, and will do so using JUnit. Using Eclispe for jUnit testing: Create BagTest.java Create BagArray.java - an array implementation of interface #2.