Software Development with UML and Java 2
SDJ I2, Spring 2010
Agenda – week 7, 2010
•
Pakages
•
Looking back
•
Looking forward
•
Packages
•
Interfaces
Page 2 – Spring 2010 Steffen Vissing Andersen
Download, Install/Setup
1.
Java SE SDK (
http://java.sun.com/javase/downloads
)
2.
Java SE SDK - Documentation
3.
Setup Environment Variables (e.g. in Windows)
4.
Eclipse IDE (
http://www.eclipse.org
)
5.
JUDE UML (
http://jude.change-vision.com/
)
•
All the above is on the USB stick
•
Video – How To Install:
Page 4 – Spring 2010 Steffen Vissing Andersen
Page 6 – Spring 2010 Steffen Vissing Andersen
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
Page 8 – Spring 2010 Steffen Vissing Andersen
Page 10 – Spring 2010 Steffen Vissing Andersen
Page 12 – Spring 2010 Steffen Vissing Andersen
Interface for a List of Strings
package collection.stringcollection;
public interface IStringList {
public void add(int index, String element); public void add(String element);
public void clear();
public boolean contains(String element); public String get(int index);
public int indexOf(String element); public boolean isEmpty();
public void remove(int index);
Page 14 – Spring 2010 Steffen Vissing Andersen
Page 16 – Spring 2010 Steffen Vissing Andersen
Page 18 – Spring 2010 Steffen Vissing Andersen
Page 20 – Spring 2010 Steffen Vissing Andersen
Page 22 – Spring 2010 Steffen Vissing Andersen
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 size by one
Example 2: Alternatively, calling add(int index, String element)
Page 24 – Spring 2010 Steffen Vissing Andersen
class StringListArrayBased
package collection.stringcollection;
public class StringListArrayBased implements IStringList
{
private String[] collection; private int size;
private static final int DEFAULT_CAPACITY = 20; public StringListArrayBased(int capacity)
{
//TODO – implement the constructor
}
public StringListArrayBased()
{
//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); // Make room for the element (has to be implemented)
collection[index] = element; size++;
Page 26 – Spring 2010 Steffen Vissing Andersen
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/SDJI2B/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);
Page 28 – Spring 2010 Steffen Vissing Andersen
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("}"); }
Page 30 – Spring 2010 Steffen Vissing Andersen
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("}"); } }