CS102 Spring 2008

Lab # 2

Implementing an ADT

In this laboratory exercise you will implement an ADT Set.  The ADT will be given and a Java interface based upon this ADT will be supplied to you in a partially completed NetBeans project called Lab2.  You will implement this ADT by creating a class ArraySet in the project.  Once you have created this class you will test your implementation using the unit tests provided in the project. When all of the tests pass, you will complete the commented code in the main method and run your program.


Download the starter project

  1. Click here to download the starter project: Lab2
  2. Save it in your cs102 directory.
  3. Unzip the file.
  4. You should be able to Open this as an existing project in NetBeans (described next)

Launch NetBeans and Open Project

  1. Navigate to your cs102 folder and select the folder named "lab2." 
  2. The "Open Project Folder" button should be enabled. Click it to open the starter project.
  3. You will need to create a class file named ArraySet:
    1. From the File Menu, click on the item New File (the second item in the drop-down menu).  
    2. In the next window, highlight Java Classes in the Categories text area and Java Class in the File Types area, then
    3. click on the button labeled Next. 
    4. From NetBeans, go to the File menu and select "Open Project" -- or click on the third button from the left on the button bar.
    5. When the next window appears change the class name from the highlighted NewClass to ArraySet.  The location for this class will be in Source Packages.  
    6. Click on the Finish button, and this class will be added to your project source files.
  4. If ArraySet is not the file currently shown in your edit window, click on ArraySet.java in your project window.  When it appears in the edit window, you will need to modify to the class header with implements Set.  Now proceed with the implementation described above. 
  5. The top left pane in NetBeans is a more sophisticated version of the top left pane in DrJava. To find your source files, you should be able to navigate down through "Source Packages" and "lab2" to reveal the source files that comprise this project:
      1. Set.java,
      2. Main.java,
      3. the newly created ArraySet.java, and
      4. Under "Test Packages" you will find SetTest.java
  6. Double-click on each of the Java source files to open them for editing. The upper right pane in NetBeans is tabbed, for selecting which open file you want to edit. 
  7. The only file you will need to modify for this project is the ArraySet.java that you just created.


ADT Set

+add(in element:int):boolean

//adds a new element to a set of integers

//precondition:  element is in the universe of input values

//returns true if the precondition is met and operation performed

 

+remove(in element:int):boolean

//removes the named element from the set

//precondition:  element is in the set

//returns true if the precondition is met and operation performed

 

+union(in otherSet:Set):Set

//returns a set that contains all of the elements of this set and otherSet

 

+intersection(in otherSet:Set):Set

//returns a set that contains all of the elements that are contained

//in both this set and other set

 

+contains(in element:int):boolean

//returns true if element is in the set

 

+isEmpty():boolean

//returns true if set is empty

 

+size():int

//returns the value of the number of elements in the set


Work to be done

The above ADT specifies and provides the syntax for the operations in a type Set.  Your project implements this specification in a java interface.  You are to create a class ArraySet to implement the interface.  ArraySet will use an array of MAX_SIZE = 16 of Booleans in this implementation.  The universe of integer values is the integers from 0 to 15.  The array will initially be empty (all values false).  When an integer in the universe is added to the set, the value at its index will be set to true.  For example, the set A will contain the elements {2, 5, 7, 8, 15} if its associated array is

 

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

false

false

true

false

false

true

false

true

true

false

false

false

false

false

false

true

 

In implementing ArraySet, you will follow the specification in the ADT (as further stated in the comments above each operation in the interface.  In addition to implementing each of these operations in ArraySet, you will add a constructor and a method:  public String toString( ) to use for displaying the contents of the set.

Unit Testing with JUnit

When you have finished implementing this class, test your implementation for correctness by running setTest in the TestPackages (right-click on setTest.java and select "Run File"). You should see the following output:

init:
deps-jar:
compile:
compile-test-single:
Testsuite: setTester.SetTest
Tests run: 4, Failures: 0, Errors: 0, Time elapsed: 0.07 sec

test-single:
BUILD SUCCESSFUL (total time: 3 seconds)

Program Execution

When your code has passed all of the tests, run the Main Project and submit your lab. You should see the following output:

init:
deps-jar:
compile:
run:
Set A: Set = {1, 5, 6, 7, 8, 9, 10, 11, 13, 14}
Set B: Set = {0, 1, 2, 3, 5, 6}
Set U: Set = {0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 13, 14}
Set I: Set = {1, 5, 6}
BUILD SUCCESSFUL (total time: 0 seconds)


Submitting your work

Show your working program to one of the coaches or your instructor to verify your program is correct. Then, from a terminal window, type the following commands:
 
        cd
        cd cs102
        submit102 Lab2

Log out

When you are done, close NetBeans, and then locate the logout button on the menu bar. Click on the logout button (red arrow pointing through an open door).  Choose "Logout..." and then click "Yes" when prompted.  Always remember to log out when you are done using the system, to ensure that no one else uses your account.