310 likes | 478 Views
Software Development with UML and Java 2 SDJ I2, Spring 2010. Agenda – week 7, 2010 Pakages Looking back Looking forward Packages Interfaces. Download, Install/Setup. Java SE SDK ( http://java.sun.com/javase/downloads ) Java SE SDK - Documentation
E N D
Software Development with UML and Java 2SDJ I2, Spring 2010 Agenda – week 7, 2010 • Pakages • Looking back • Looking forward • Packages • Interfaces
Download, Install/Setup • Java SE SDK (http://java.sun.com/javase/downloads) • Java SE SDK - Documentation • Setup Environment Variables (e.g. in Windows) • Eclipse IDE (http://www.eclipse.org) • JUDE UML (http://jude.change-vision.com/) • All the above is on the USB stick • Video – How To Install: (http://it-engineering.dk/Course/A09/SDJI1A/Tools/_Video)
Looking forward: What to implement in this course • We will create a lot of collection classes during this course • List containing Strings • array based • dynamic linked based • List with generic type • array based • dynamic linked based • Stack • … • Queue • … • Set • … • SortedSet • …
Interface for a List of Strings package collection.stringcollection; public interface IStringList { public voidadd(intindex, String element); public voidadd(String element); public void clear(); public booleancontains(String element); public Stringget(intindex); public intindexOf(String element); public booleanisEmpty(); public voidremove(intindex); public voidremove(String element); public void set(intindex, String element); public intsize(); }
StringListArrayBased • Example 1:Calling method add(String element)on a StringListArrayBased-object with the parameter element="G"will • Add the element "G"to collection[6] • Increment sizeby one • Example 2: Alternatively, calling add(int index, String element)with the parameters index=3 and element="G" will • Move "F" to index 6, "E" to index 5 and "D" to index 4 • Add the element "G" to index 3 (i.e. collection[3]) • Increment size by one
class StringListArrayBased packagecollection.stringcollection; publicclassStringListArrayBasedimplementsIStringList { private String[] collection; privateint size; privatestaticfinalintDEFAULT_CAPACITY = 20; publicStringListArrayBased(int capacity) { //TODO – implement the constructor } publicStringListArrayBased() { //TODO – implement the constructor } ... // TODO - implement all methods (including method toString()) }
A little help… ... public void add(int index, String element) { if (index > size || index < 0) throw new IndexOutOfBoundsException("index=" + index + " size=" + size); if (size >= collection.length) throw new IllegalStateException(); shiftUp(index); // Makeroom for the element (has to beimplemented) collection[index] = element; size++; } ...
Exercises • Implement the interface IStringList – using an array to store the elements (call the class: StringListArrayBased in package collection.stringcollection) • Find documentation in the SDJI2 javadoc IStringList, or here: • http://it-engineering.dk/Course/S10/SDJI2A/javadoc/IStringList.html
StringListTest (1/4) import collection.stringcollection.IStringList; public class StringListTest { public static void main(String[] args) { IStringList list = new StringListArrayBased(); list.add("Bob"); list.add("Dee"); printList("BD: ", list); list.add(1,"Carl"); // add in the middle printList("BCD: ", list); list.add(0,"Allan"); // add first printList("ABCD: ", list); list.add(4,"Eric"); // add last printList("ABCDE: ", list);
StringListTest (2/4) printListBackwards("EDCBA: ", list); list.remove(0); // remove first printList("BCDE: ", list); list.remove(list.size()-1); // remove last printList("BCD: ", list); list.remove("Carl"); // remove middle printList("BD: ", list); printListBackwards("DB: ", list); }
StringListTest (3/4) private static void printList(String prompt, IStringList list) { System.out.print(prompt + "{"); for (int i=0; i<list.size(); i++) { System.out.print(list.get(i)); if (i < list.size() - 1) System.out.print(", "); } System.out.println("}"); }
StringListTest (4/4) private static void printListBackwards(String prompt, IStringList list) { System.out.print(prompt + "{"); for (int i=list.size()-1; i>=0; i--) { System.out.print(list.get(i)); if (i > 0) System.out.print(", "); } System.out.println("}"); } }
StringListTest (output) BD: {Bob, Dee} BCD: {Bob, Carl, Dee} ABCD: {Allan, Bob, Carl, Dee} ABCDE: {Allan, Bob, Carl, Dee, Eric} EDCBA: {Eric, Dee, Carl, Bob, Allan} BCDE: {Bob, Carl, Dee, Eric} BCD: {Bob, Carl, Dee} BD: {Bob, Dee} DB: {Dee, Bob}