Java5/Assign5-S2013 x
CS204
Myers
Assignment 5
Your assignment is to write a program that maintains a list of contacts. The program must be able to search for, make insertions, deletions and edit of contacts. The program must be able to edit characteristics for any contacts. The program will also print out mailing labels.
Data Element – Contact
You will be creating a class Contact which will hold all the information for a contact: first name, last name, street address, city, state, zip and phone number. It will implement the Comparable interface. Include getters and setters. You must have a getFirstName() which returns the first name for the BinarySearchTreeTest to work.
Data Structure – Binary Search Tree
You will be creating a generic class BinarySearchTree which implements the BinarySearchTreeInterface. You may create your own binary search tree methods or you may use methods from library classes such that it behaves as a binary search tree.
Data Managers – Address Book
Create a class AddressBook that implements the AddressBookInterface. This class will use a parameterized object of your BinarySearchTree class to hold the Contacts for the AddressBook (BinarySearchTree
GUI Driver
Use an object of your AddressBook. Use the FileChooser to ask the user for the location of the file to read stored contacts from, before the initial screen appears. The contacts are displayed in a JTable. I have provided a TableUtility class to help you with using a JTable. Provide a way for the user to select a row in the table. When a row is selected, display the details of this contact. Provide a way for the user to add, delete and edit contacts in the address book. Print Phone List in order by the key. When the user selects Exit, the contacts in the address book are stored back to the original file.
Input File
The contacts are stored in a file in the following format (fields separated by a ;):
Exception Handling
Display appropriate messages when user asks to delete or edit and a row hasn’t been selected.
(
Insert a contact
)
(
Select a row
in the table
)
(
Delete the contact
)
(
Edit the contact
No change in key
N
)
(
Edit Contact
Change in key
)
(
Edit the contact
Change in the key
)
(
Print Phone List
)
Program Grade Sheet
Assignment #5
CS204
Name _________________________________ Date/Time _________________
DOCUMENTATION
Javadoc for user created classes: 4 pt _____
Test Cases 4 pt _____
JUnit Test Class
Implement the student tests in AddressBookTest and
BinarySearchTreeTest
UML Diagram 2 pt _____
PROGRAMMING
Programming Style
Internal class documentation (within source code) 3 pt _____
Class description using Javadoc
Author’s Name, Class, Class Time
Methods commented using Javadoc
Program user interface
Clear to user how data is to be entered 1 pt _____
Output is easy to understand 1 pt _____
Accuracy
Public Tests – provided for you and the ones you wrote 5 pt _____
Private Tests 5 pt _____
Program Details
1. Data Element – Contact class 5 pt _____
a. Implements Comparable
b. toString, getters & setters methods
2. Data Structure – BinarySearchTree class 7 pts _____
a. Implements BinarySearchTreeInterface
b. Generic class
c. Method that does inorder traversal
3. Data Manager – AddressBook 7 pts _____
a. Implements the AddressBookInterface
b. Uses an object of BinarySearchTree
c. Methods that insert, delete and edit
d. Method toTable from Tabelize interface
4. GUI 6 pts _____
a. Reads from a file
b. Uses fileChooser
c. Displays contacts in a table
d. Allows user to select a row in the table
e. Uses methods of AddressBook
f. Write to a file
g. Display appropriate messages when user asks to delete or edit
and a row hasn’t been selected.
h. Prints out phone list
Total 50 pt _____
Java5/DemoContacts.txt
Helga;Hufflepuff;55 Redding Ave;Gaithersburg;MD;20877;240-555-3434
Neville;Longbottom;1414 Oak Dr.;Darnestown;MD;20878;240-666-9855
Nymphadora;Tonks;925 Cedar Ct.;Silver Spring;MD;20857;301-777-3873
Harry;Potter;234 Surley Ave;Anthem;MD;20877;240-888-3434
Ronald;Weasley;886 Maple Dr.;Gaithersburg;MD;20878;240-444-9855
Hermione;Granger;534 Willowby Ct.;Poolesville;MD;20857;301-999-3876
Sirius;Black;963 Victory Ave;Germantown;MD;20877;240-567-3434
Lavender;Brown;737 Spruce Dr.;Derwood;MD;20878;240-987-9855
Cho;Chang;934 Falconhurst Ct.;Silver Spring;MD;20857;301-432-3876
Albus;Dumbledore;3433 Bethany Ave;Rockville;MD;20877;240-572-3434
Draco;Malfoy;35553 Longhorn Dr.;Gaithersburg;MD;20878;240-765-9855
Bellatrix;Lestrange;2654 Killney Ct.;College Park;MD;20857;301-387-3876
Java5/java5/.classpath
Java5/java5/.project
java5
org.eclipse.jdt.core.javabuilder
org.eclipse.jdt.core.javanature
Java5/java5/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
Java5/java5/bin/assignment5/AddressBookInterface.class
package assignment5;
abstract interface AddressBookInterface extends Tabelize {
static void
public abstract void add(Contact);
public abstract void add(String, String, String, String, String, String, String);
public abstract java.util.ArrayList get(String, String);
public abstract boolean delete(Contact);
public abstract boolean delete(String, String);
public abstract boolean edit(String, String, String, String, String, String, String, String, String);
public abstract String[][] toTable();
public abstract int getNumContacts();
public abstract String toString();
}
Java5/java5/bin/assignment5/AddressBookTest.class
package assignment5;
public synchronized class AddressBookTest {
private AddressBook addressBook;
private Contact contact1;
private Contact contact2;
private Contact contact3;
private Contact contact4;
private Contact contact5;
private Contact contact6;
private Contact contact7;
public void AddressBookTest();
public void setUp() throws Exception;
public void tearDown() throws Exception;
public void testAddContact();
public void testAddWithStrings();
public void testAddWithSameLastName();
public void testAddWithStringsSTUDENT();
public void testGet();
public void testGetSTUDENT();
public void testDeleteContact();
public void testDeleteWithStrings();
public void testDeleteWithStringsSTUDENT();
public void testEdit();
public void testToTable();
}
Java5/java5/bin/assignment5/BinarySearchTreeInterface.class
package assignment5;
abstract interface BinarySearchTreeInterface {
public abstract void add(Object, Object);
public abstract boolean delete(Object);
public abstract java.util.ArrayList inOrder();
public abstract boolean hasKey(Object);
public abstract Object get(Object);
}
Java5/java5/bin/assignment5/BinarySearchTreeTest.class
package assignment5;
public synchronized class BinarySearchTreeTest {
BinarySearchTree bst;
Contact contact1;
Contact contact2;
Contact contact3;
Contact contact4;
Contact contact5;
Contact contact6;
Contact contact7;
public void BinarySearchTreeTest();
public void setUp() throws Exception;
public void tearDown() throws Exception;
public void testAdd();
public void testDelete();
public void testInOrderSTUDENT();
public void testHasKey();
public void testGet();
}
Java5/java5/bin/assignment5/Tabelize.class
package assignment5;
public abstract interface Tabelize {
public abstract String[][] toTable();
}
Java5/java5/bin/assignment5/TableUtility.class
package assignment5;
public synchronized class TableUtility {
public void TableUtility();
public static void updateTable(Tabelize, javax.swing.JTable);
public static javax.swing.JTable createTable(String[], int);
public static void clearTable(javax.swing.JTable);
}
Java5/java5/src/assignment5/AddressBookInterface.java
Java5/java5/src/assignment5/AddressBookInterface.java
package
assignment5
;
import
java
.
util
.
*
;
/**
* Data Manager for an Address Book
*
@author
Professor Myers
*
*/
interface
AddressBookInterface
extends
Tabelize
{
/**
* Use a TreeMap
* The key will be “
*/
/**
* Add a new Contact to the Address Book
* The key will be “
* Used for testing
*
@param
newContact the Contact object to be added to the TreeMap
*/
public
void
add
(
Contact
newContact
);
/**
* Add a new Contact to the Address Book
* The key will be “
*
@param
first first name of Contact
*
@param
last last name of Contact
*
@param
addr street address of Contact
*
@param
city city of Contact
*
@param
state state of Contact
*
@param
zip zip of Contact
*
@param
phoneNum phone number of Contact
*/
public
void
add
(
String
first
,
String
last
,
String
addr
,
String
city
,
String
state
,
String
zip
,
String
phoneNum
);
/**
* Retrieves the information for a specific Contact
*
@param
firstName first name of Contact
*
@param
lastName last name of Contact
*
@return
An ArrayList of Strings in the following order:
* first name
* last name
* street address
* city
* state
* zipcode
* phone number
*/
public
ArrayList
<
String
>
get
(
String
firstName
,
String
lastName
);
/**
* Delete a Contact from the Address Book
*
@param
contact the Contact object to delete
*
@return
true if the contact was deleted, false if the contact was not found
*/
public
boolean
delete
(
Contact
contact
);
/**
* Delete a Contact from the Address Book
*
@param
firstName the first name of the Contact to delete
*
@param
lastName the last name of the Contact to delete
*
@return
true if the contact was deleted, false if the contact was not found
*/
public
boolean
delete
(
String
firstName
,
String
lastName
);
/**
* Edit a Contact from the Address Book
* If the first or last name is changed, the contact must be deleted from the TreeMap
* and then added with the new key
* If the first or last name has not changed, just change the data within the Contact
*
@param
oldFirst the original first name
*
@param
oldLast the original last name
*
@param
keyFirst the current first name
*
@param
keyLast the current last name
*
@param
addr the current street address
*
@param
city the current city
*
@param
state the current state
*
@param
zip the current zip
*
@param
phoneNum the current phone number
*
@return
true if the Contact was found and edited, false if the Contact was not found
*/
public
boolean
edit
(
String
oldFirst
,
String
oldLast
,
String
keyFirst
,
String
keyLast
,
String
addr
,
String
city
,
String
state
,
String
zip
,
String
phoneNum
);
/**
* The overriden method from the Tabelize interface
* Takes all the Contacts and puts the information into a two dimensional array of Strings
* that can be used to place in a JTable
* The information for each Contact is put in the following columns:
* [0] – first name
* [1] – last name
* [2] – street address
* [3] – city
* [4] – state
* [5] – zipcode
* [6] – phone number
*/
public
String
[][]
toTable
();
/**
* Returns the number of contacts in the Address Book
* Used for testing
*
@return
the number of contacts in the Address Book
*/
public
int
getNumContacts
();
/**
* Returns a String of all the contacts in the Address Book in order by key
* (uses the inOrder traversal)
* The String for each Contact will be in the following format:
*
* * * use the toString method of the Contact class * */ } Java5/java5/src/assignment5/AddressBookTest.java import import import import import import public @ addressBook contact1 contact2 contact3 contact4 contact5 contact6 contact7 @ addressBook contact1 @ addressBook addressBook addressBook assertEquals assertEquals assertEquals scan assertEquals assertEquals scan assertEquals assertEquals @ addressBook addressBook addressBook assertEquals assertEquals assertEquals scan assertEquals assertEquals scan assertEquals assertEquals @ addressBook addressBook addressBook assertEquals assertEquals assertEquals scan assertEquals assertEquals scan assertEquals assertEquals @ fail @ addressBook addressBook addressBook assertEquals assertEquals assertEquals contact assertEquals assertEquals assertEquals @ fail @ addressBook addressBook addressBook addressBook addressBook addressBook addressBook assertEquals assertEquals assertEquals addressBook assertEquals @ addressBook addressBook addressBook addressBook addressBook addressBook addressBook assertEquals assertEquals assertEquals addressBook assertEquals @ fail @ addressBook addressBook addressBook addressBook addressBook addressBook addressBook assertEquals assertEquals assertEquals addressBook contact assertEquals assertEquals assertEquals contact assertEquals assertEquals assertEquals addressBook assertEquals contact assertEquals assertEquals assertEquals @ addressBook addressBook addressBook addressBook addressBook addressBook addressBook assertEquals assertEquals assertEquals assertEquals assertEquals assertEquals } Java5/java5/src/assignment5/BinarySearchTreeInterface.java import /** * This is a generic interface for a Binary Search Tree * You may build your own Binary Search Tree methods or use library class methods in a way that * is consistent with a binary search tree * * * * */ interface { * Adds the data to the binary search tree. The key is used to determine where to place * the data into the binary search tree. * * */ * Deletes the object in the binary search tree that corresponds to the key * * */ * Performs an inorder traversal of the binary search tree and returns an ArrayList of the data objects in order * */ * Determines if the key is in the binary search tree * * */ * Returns the data object that corresponds the the key * * */ } Java5/java5/src/assignment5/BinarySearchTreeTest.java import import import import import //your Contact class must have a getFirstName method for these tests to work public @ bst contact1 contact2 contact3 contact4 contact5 contact6 contact7 @ bst contact1 @ bst bst bst bst bst bst bst assertEquals assertEquals assertEquals assertEquals @ bst bst bst bst bst bst bst assertEquals assertEquals assertEquals assertEquals assertEquals list assertEquals assertEquals assertEquals assertEquals assertEquals @ fail @ bst bst bst bst bst bst bst assertEquals assertEquals assertEquals @ bst bst bst bst bst bst bst assertEquals assertEquals assertEquals } Java5/java5/src/assignment5/Tabelize.java /** * This guarantees that what is returned from toTable can be placed in a JTable * * */ public * * */ } Java5/java5/src/assignment5/TableUtility.java import import import public clearTable
@return
A String of all the contacts in the Address Book
public
String
toString
();
Java5/java5/src/assignment5/AddressBookTest.java
package
assignment5
;
static
org
.
junit
.
Assert
.
*
;
java
.
util
.
ArrayList
;
java
.
util
.
Scanner
;
org
.
junit
.
After
;
org
.
junit
.
Before
;
org
.
junit
.
Test
;
class
AddressBookTest
{
private
AddressBook
addressBook
;
private
Contact
contact1
,
contact2
,
contact3
,
contact4
,
contact5
,
contact6
,
contact7
;
Before
public
void
setUp
()
throws
Exception
{
=
new
AddressBook
();
=
new
Contact
(
“Amelia”
,
“Smith”
,
“Addr1”
,
“City1”
,
“State1”
,
“Zip1”
,
“Phone1”
);
=
new
Contact
(
“John”
,
“Donahue”
,
“Addr2”
,
“City2”
,
“State2”
,
“Zip2”
,
“Phone2”
);
=
new
Contact
(
“George”
,
“Amberly”
,
“Addr3”
,
“City3”
,
“State3”
,
“Zip3”
,
“Phone3”
);
=
new
Contact
(
“Penelope”
,
“Zuckerman”
,
“Addr4”
,
“City4”
,
“State4”
,
“Zip4”
,
“Phone4”
);
=
new
Contact
(
“Bob”
,
“Brown”
,
“AddrBob”
,
“CityBob”
,
“StateBob”
,
“ZipBob”
,
“PhoneBob”
);
=
new
Contact
(
“Tom”
,
“Brown”
,
“AddrTom”
,
“CityTom”
,
“StateTom”
,
“ZipTom”
,
“PhoneTom”
);
=
new
Contact
(
“Harry”
,
“Brown”
,
“AddrHarry”
,
“CityHarry”
,
“StateHarry”
,
“ZipHarry”
,
“PhoneHarry”
);
}
After
public
void
tearDown
()
throws
Exception
{
=
null
;
=
contact2
=
contact3
=
contact4
=
contact5
=
contact6
=
contact7
=
null
;
}
Test
public
void
testAddContact
()
{
// add three contacts
.
add
(
contact1
);
.
add
(
contact2
);
.
add
(
contact3
);
//check the number of contacts in the addressBook
(
addressBook
.
getNumContacts
(),
3
);
//the toString method uses the inOrder traversal to return a string of the contacts in order
//check if the contacts were added to the binary search tree correctly
String
result
=
addressBook
.
toString
();
Scanner
scan
=
new
Scanner
(
result
);
(
scan
.
nextLine
(),
“George Amberly”
);
(
scan
.
nextLine
(),
“Phone3”
);
.
nextLine
();
// gets the space in between
(
scan
.
nextLine
(),
“John Donahue”
);
(
scan
.
nextLine
(),
“Phone2”
);
.
nextLine
();
// gets the space in between
(
scan
.
nextLine
(),
“Amelia Smith”
);
(
scan
.
nextLine
(),
“Phone1”
);
}
Test
public
void
testAddWithStrings
()
{
//add three contacts
.
add
(
“Amelia”
,
“Smith”
,
“Addr1”
,
“City1”
,
“State1”
,
“Zip1”
,
“Phone1”
);
.
add
(
“John”
,
“Donahue”
,
“Addr2”
,
“City2”
,
“State2”
,
“Zip2”
,
“Phone2”
);
.
add
(
“George”
,
“Amberly”
,
“Addr3”
,
“City3”
,
“State3”
,
“Zip3”
,
“Phone3”
);
//check the number of contacts in the addressBook
(
addressBook
.
getNumContacts
(),
3
);
//the toString method uses the inOrder traversal to return a string of the contacts in order
String
result
=
addressBook
.
toString
();
Scanner
scan
=
new
Scanner
(
result
);
(
scan
.
nextLine
(),
“George Amberly”
);
(
scan
.
nextLine
(),
“Phone3”
);
.
nextLine
();
// gets the space in between
(
scan
.
nextLine
(),
“John Donahue”
);
(
scan
.
nextLine
(),
“Phone2”
);
.
nextLine
();
// gets the space in between
(
scan
.
nextLine
(),
“Amelia Smith”
);
(
scan
.
nextLine
(),
“Phone1”
);
}
Test
public
void
testAddWithSameLastName
()
{
.
add
(
“Bob”
,
“Brown”
,
“AddrBob”
,
“CityBob”
,
“StateBob”
,
“ZipBob”
,
“PhoneBob”
);
.
add
(
“Tom”
,
“Brown”
,
“AddrTom”
,
“CityTom”
,
“StateTom”
,
“ZipTom”
,
“PhoneTom”
);
.
add
(
“Harry”
,
“Brown”
,
“AddrHarry”
,
“CityHarry”
,
“StateHarry”
,
“ZipHarry”
,
“PhoneHarry”
);
//check the number of contacts in the addressBook
(
addressBook
.
getNumContacts
(),
3
);
//the toString method uses the inOrder traversal to return a string of the contacts in order
String
result
=
addressBook
.
toString
();
Scanner
scan
=
new
Scanner
(
result
);
(
scan
.
nextLine
(),
“Bob Brown”
);
(
scan
.
nextLine
(),
“PhoneBob”
);
.
nextLine
();
// gets the space in between
(
scan
.
nextLine
(),
“Harry Brown”
);
(
scan
.
nextLine
(),
“PhoneHarry”
);
.
nextLine
();
// gets the space in between
(
scan
.
nextLine
(),
“Tom Brown”
);
(
scan
.
nextLine
(),
“PhoneTom”
);
}
Test
public
void
testAddWithStringsSTUDENT
()
{
// Add a different set of contacts than is used in testAddWithStrings
// or testAddWithSameLastName
// show that the contacts were added correctly
(
“Not yet implemented”
);
}
Test
public
void
testGet
()
{
// add three contacts
.
add
(
contact1
);
.
add
(
contact2
);
.
add
(
contact3
);
//retrieve the details of the contact with the first and last name (which make up the key)
ArrayList
<
String
>
contact
=
addressBook
.
get
(
“George”
,
“Amberly”
);
(
contact
.
get
(
0
),
“George”
);
(
contact
.
get
(
2
),
“Addr3”
);
(
contact
.
get
(
5
),
“Zip3”
);
=
addressBook
.
get
(
“John”
,
“Donahue”
);
(
contact
.
get
(
0
),
“John”
);
(
contact
.
get
(
2
),
“Addr2”
);
(
contact
.
get
(
5
),
“Zip2”
);
}
Test
public
void
testGetSTUDENT
()
{
// Add a different set of contacts than is used in testGet
// show that the get works correctly
(
“Not yet implemented”
);
}
Test
public
void
testDeleteContact
()
{
//add contacts to the address book
.
add
(
contact1
);
.
add
(
contact2
);
.
add
(
contact3
);
.
add
(
contact4
);
.
add
(
contact5
);
.
add
(
contact6
);
.
add
(
contact7
);
//show that Tom Brown is in the address book
ArrayList
<
String
>
contact
=
addressBook
.
get
(
“Tom”
,
“Brown”
);
(
contact
.
get
(
0
),
“Tom”
);
(
contact
.
get
(
2
),
“AddrTom”
);
(
contact
.
get
(
5
),
“ZipTom”
);
//delete the contact using a reference to the contact object
.
delete
(
contact6
);
//show that Tom Brown is no longer in the address book
(
addressBook
.
get
(
“Tom”
,
“Brown”
),
null
);
}
Test
public
void
testDeleteWithStrings
()
{
//add contacts to the address book
.
add
(
contact1
);
.
add
(
contact2
);
.
add
(
contact3
);
.
add
(
contact4
);
.
add
(
contact5
);
.
add
(
contact6
);
.
add
(
contact7
);
//show that Tom Brown is in the address book
ArrayList
<
String
>
contact
=
addressBook
.
get
(
“Tom”
,
“Brown”
);
(
contact
.
get
(
0
),
“Tom”
);
(
contact
.
get
(
2
),
“AddrTom”
);
(
contact
.
get
(
5
),
“ZipTom”
);
//delete the contact using the first and last name (which make up the key)
.
delete
(
“Tom”
,
“Brown”
);
//delete the contact using a reference to the contact object
(
addressBook
.
get
(
“Tom”
,
“Brown”
),
null
);
}
Test
public
void
testDeleteWithStringsSTUDENT
()
{
// Add a different set of contacts than is used in testDeleteWithStrings
// show that the delete works correctly
(
“Not yet implemented”
);
}
Test
public
void
testEdit
()
{
//add contacts to the address book
.
add
(
contact1
);
.
add
(
contact2
);
.
add
(
contact3
);
.
add
(
contact4
);
.
add
(
contact5
);
.
add
(
contact6
);
.
add
(
contact7
);
// Test change of details without a change of key
ArrayList
<
String
>
contact
=
addressBook
.
get
(
“Penelope”
,
“Zuckerman”
);
(
contact
.
get
(
0
),
“Penelope”
);
(
contact
.
get
(
2
),
“Addr4”
);
(
contact
.
get
(
5
),
“Zip4”
);
// edit the Address and Phone Number
.
edit
(
“Penelope”
,
“Zuckerman”
,
“Penelope”
,
“Zuckerman”
,
“4587 Oak St.”
,
“City4”
,
“State4”
,
“24095”
,
“Phone4”
);
=
addressBook
.
get
(
“Penelope”
,
“Zuckerman”
);
// check if the edits were made
(
contact
.
get
(
0
),
“Penelope”
);
(
contact
.
get
(
2
),
“4587 Oak St.”
);
(
contact
.
get
(
5
),
“24095”
);
// Test change of details with a change of key
=
addressBook
.
get
(
“Amelia”
,
“Smith”
);
(
contact
.
get
(
0
),
“Amelia”
);
(
contact
.
get
(
2
),
“Addr1”
);
(
contact
.
get
(
5
),
“Zip1”
);
// edit the last name (part of the key) and the address
.
edit
(
“Amelia”
,
“Smith”
,
“Amelia”
,
“Babble”
,
“1422 Turtle Bay Way”
,
“City1”
,
“State1”
,
“Zip1”
,
“Phone1”
);
// make sure the old contact was deleted from address book
(
addressBook
.
get
(
“Amelia”
,
“Smith”
),
null
);
// check if the edits were made
=
addressBook
.
get
(
“Amelia”
,
“Babble”
);
(
contact
.
get
(
0
),
“Amelia”
);
(
contact
.
get
(
2
),
“1422 Turtle Bay Way”
);
(
contact
.
get
(
5
),
“Zip1”
);
}
Test
public
void
testToTable
()
{
//add contacts to the address book
.
add
(
contact1
);
.
add
(
contact2
);
.
add
(
contact3
);
.
add
(
contact4
);
.
add
(
contact5
);
.
add
(
contact6
);
.
add
(
contact7
);
//all contacts are put in a 2 dim array of String which can displayed in a JTable
//the contacts are in order
String
[][]
table
=
addressBook
.
toTable
();
(
table
[
2
][
0
],
“Harry”
);
(
table
[
2
][
1
],
“Brown”
);
(
table
[
4
][
2
],
“Addr2”
);
(
table
[
4
][
3
],
“City2”
);
(
table
[
6
][
4
],
“State4”
);
(
table
[
6
][
5
],
“Zip4”
);
}
Java5/java5/src/assignment5/BinarySearchTreeInterface.java
package
assignment5
;
java
.
util
.
ArrayList
;
@author
Professor Myers
@param
@param
The object type of the data that is being stored in the Binary Search Tree
BinarySearchTreeInterface
<
T
,
S
>
/**
@param
key determines how data is stored in the binary search tree
@param
data the object that is being stored in the binary search tree
public
void
add
(
T key
,
S data
);
/**
@param
key determines how data is stored in the binary search tree
@return
true if the key was found and the data object was deleted, false if the key was not found
public
boolean
delete
(
T key
);
/**
@return
an ArrayList of the data objects in order
public
ArrayList
<
S
>
inOrder
();
/**
@param
key determines how data is stored in the binary search tree
@return
true if the key is found in the binary search tree, false if the key is not found
public
boolean
hasKey
(
T key
);
/**
@param
key key determines how data is stored in the binary search tree
@return
the data object that corresponds the the key
public
S get
(
T key
);
Java5/java5/src/assignment5/BinarySearchTreeTest.java
package
assignment5
;
static
org
.
junit
.
Assert
.
*
;
java
.
util
.
ArrayList
;
org
.
junit
.
After
;
org
.
junit
.
Before
;
org
.
junit
.
Test
;
class
BinarySearchTreeTest
{
BinarySearchTree
<
String
,
Contact
>
bst
;
Contact
contact1
,
contact2
,
contact3
,
contact4
,
contact5
,
contact6
,
contact7
;
Before
public
void
setUp
()
throws
Exception
{
=
new
BinarySearchTree
<
String
,
Contact
>
();
=
new
Contact
(
“Amelia”
,
“Smith”
,
“Addr1”
,
“City1”
,
“State1”
,
“Zip1”
,
“Phone1”
);
=
new
Contact
(
“John”
,
“Donahue”
,
“Addr2”
,
“City2”
,
“State2”
,
“Zip2”
,
“Phone2”
);
=
new
Contact
(
“George”
,
“Amberly”
,
“Addr3”
,
“City3”
,
“State3”
,
“Zip3”
,
“Phone3”
);
=
new
Contact
(
“Penelope”
,
“Zuckerman”
,
“Addr4”
,
“City4”
,
“State4”
,
“Zip4”
,
“Phone4”
);
=
new
Contact
(
“Bob”
,
“Brown”
,
“AddrBob”
,
“CityBob”
,
“StateBob”
,
“ZipBob”
,
“PhoneBob”
);
=
new
Contact
(
“Tom”
,
“Brown”
,
“AddrTom”
,
“CityTom”
,
“StateTom”
,
“ZipTom”
,
“PhoneTom”
);
=
new
Contact
(
“Harry”
,
“Brown”
,
“AddrHarry”
,
“CityHarry”
,
“StateHarry”
,
“ZipHarry”
,
“PhoneHarry”
);
}
After
public
void
tearDown
()
throws
Exception
{
=
null
;
=
contact2
=
contact3
=
contact4
=
contact5
=
contact6
=
contact7
=
null
;
}
Test
public
void
testAdd
()
{
//Your Contact class must have a getFirstName method for this test to work
//add to the binary search tree
.
add
(
“Smith, Amelia”
,
contact1
);
.
add
(
“Donahue, John”
,
contact2
);
.
add
(
“Amberly, George”
,
contact3
);
.
add
(
“Zuckerman, Penelope”
,
contact4
);
.
add
(
“Brown, Bob”
,
contact5
);
.
add
(
“Brown, Tom”
,
contact6
);
.
add
(
“Brown, Harry”
,
contact7
);
ArrayList
<
Contact
>
list
=
bst
.
inOrder
();
(
list
.
get
(
0
).
getFirstName
(),
“George”
);
(
list
.
get
(
1
).
getFirstName
(),
“Bob”
);
(
list
.
get
(
3
).
getFirstName
(),
“Tom”
);
(
list
.
get
(
5
).
getFirstName
(),
“Amelia”
);
}
Test
public
void
testDelete
()
{
//Your Contact class must have a getFirstName method for this test to work
//add to the binary search tree
.
add
(
“Smith, Amelia”
,
contact1
);
.
add
(
“Donahue, John”
,
contact2
);
.
add
(
“Amberly, George”
,
contact3
);
.
add
(
“Zuckerman, Penelope”
,
contact4
);
.
add
(
“Brown, Bob”
,
contact5
);
.
add
(
“Brown, Tom”
,
contact6
);
.
add
(
“Brown, Harry”
,
contact7
);
ArrayList
<
Contact
>
list
=
bst
.
inOrder
();
(
list
.
get
(
0
).
getFirstName
(),
“George”
);
(
list
.
get
(
1
).
getFirstName
(),
“Bob”
);
(
list
.
get
(
3
).
getFirstName
(),
“Tom”
);
(
list
.
get
(
5
).
getFirstName
(),
“Amelia”
);
//delete a contact
(
bst
.
delete
(
“Brown, Bob”
),
true
);
//check if deleted from tree
=
bst
.
inOrder
();
(
list
.
get
(
0
).
getFirstName
(),
“George”
);
(
list
.
get
(
1
).
getFirstName
(),
“Harry”
);
(
list
.
get
(
3
).
getFirstName
(),
“John”
);
(
list
.
get
(
5
).
getFirstName
(),
“Penelope”
);
//test if returns false, not in tree
(
bst
.
delete
(
“Brown, George”
),
false
);
}
Test
public
void
testInOrderSTUDENT
()
{
//add contacts to the tree
//check if they are in order
//use different data than in testAdd and testDelete
(
“Not yet implemented”
);
}
Test
public
void
testHasKey
()
{
//Your Contact class must have a getFirstName method for this test to work
//add to the binary search tree
.
add
(
“Smith, Amelia”
,
contact1
);
.
add
(
“Donahue, John”
,
contact2
);
.
add
(
“Amberly, George”
,
contact3
);
.
add
(
“Zuckerman, Penelope”
,
contact4
);
.
add
(
“Brown, Bob”
,
contact5
);
.
add
(
“Brown, Tom”
,
contact6
);
.
add
(
“Brown, Harry”
,
contact7
);
//check if key if found in the tree
(
bst
.
hasKey
(
“Donahue, John”
),
true
);
(
bst
.
hasKey
(
“Brown, Tom”
),
true
);
(
bst
.
hasKey
(
“Brown, George”
),
false
);
}
Test
public
void
testGet
()
{
//Your Contact class must have a getFirstName method for this test to work
//add to the binary search tree
.
add
(
“Smith, Amelia”
,
contact1
);
.
add
(
“Donahue, John”
,
contact2
);
.
add
(
“Amberly, George”
,
contact3
);
.
add
(
“Zuckerman, Penelope”
,
contact4
);
.
add
(
“Brown, Bob”
,
contact5
);
.
add
(
“Brown, Tom”
,
contact6
);
.
add
(
“Brown, Harry”
,
contact7
);
//check if finds key
(
bst
.
get
(
“Brown, Bob”
).
getFirstName
(),
“Bob”
);
//check if finds key
(
bst
.
get
(
“Amberly, George”
).
getFirstName
(),
“George”
);
//check if finds key
(
bst
.
get
(
“Brown, Robert”
),
null
);
}
Java5/java5/src/assignment5/Tabelize.java
package
assignment5
;
@author
Professor Myers
interface
Tabelize
{
/**
@return
the contents as a 2 dimensional array of Strings
String
[][]
toTable
();
Java5/java5/src/assignment5/TableUtility.java
package
assignment5
;
javax
.
swing
.
JTable
;
javax
.
swing
.
table
.
DefaultTableModel
;
javax
.
swing
.
table
.
TableModel
;
class
TableUtility
{
public
static
void
updateTable
(
Tabelize
tableContents
,
JTable
table
)
{
(
table
);
String
[][]
temp
=
tableContents
.
toTable
();
for
(
int
row
=
0
;
row
<
temp
.
length
;
row
++
)
for
(
int
col
=
0
;
col
<
temp
[
row
].
length
;
col
++
)
table
.
setValueAt
(
temp
[
row
][
col
],
row
,
col
);
}
public
static
JTable
createTable
(
String
[]
headings
,
int
rows
)
{
DefaultTableModel
model
=
new
DefaultTableModel
(
rows
,
headings
.
length
);
model
.
setColumnIdentifiers
(
headings
);
return
(
new
JTable
(
model
));
}
public
static
void
clearTable
(
JTable
table
)
{
TableModel
model
=
table
.
getModel
();
int
colCount
=
model
.
getColumnCount
();
int
rowCount
=
model
.
getRowCount
();
for
(
int
row
=
0
;
row
<
rowCount
;
row
++
)
for
(
int
col
=
0
;
col
<
colCount
;
col
++
)
table
.
setValueAt
(
""
,
row
,
col
);
}
}