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
*
@return
A String of all the contacts in the Address Book
*/
public
String
toString
();
}
Java5/java5/src/assignment5/AddressBookTest.java
Java5/java5/src/assignment5/AddressBookTest.java
package
assignment5
;
import
static
org
.
junit
.
Assert
.
*
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Scanner
;
import
org
.
junit
.
After
;
import
org
.
junit
.
Before
;
import
org
.
junit
.
Test
;
public
class
AddressBookTest
{
private
AddressBook
addressBook
;
private
Contact
contact1
,
contact2
,
contact3
,
contact4
,
contact5
,
contact6
,
contact7
;
@
Before
public
void
setUp
()
throws
Exception
{
addressBook
=
new
AddressBook
();
contact1
=
new
Contact
(
“Amelia”
,
“Smith”
,
“Addr1”
,
“City1”
,
“State1”
,
“Zip1”
,
“Phone1”
);
contact2
=
new
Contact
(
“John”
,
“Donahue”
,
“Addr2”
,
“City2”
,
“State2”
,
“Zip2”
,
“Phone2”
);
contact3
=
new
Contact
(
“George”
,
“Amberly”
,
“Addr3”
,
“City3”
,
“State3”
,
“Zip3”
,
“Phone3”
);
contact4
=
new
Contact
(
“Penelope”
,
“Zuckerman”
,
“Addr4”
,
“City4”
,
“State4”
,
“Zip4”
,
“Phone4”
);
contact5
=
new
Contact
(
“Bob”
,
“Brown”
,
“AddrBob”
,
“CityBob”
,
“StateBob”
,
“ZipBob”
,
“PhoneBob”
);
contact6
=
new
Contact
(
“Tom”
,
“Brown”
,
“AddrTom”
,
“CityTom”
,
“StateTom”
,
“ZipTom”
,
“PhoneTom”
);
contact7
=
new
Contact
(
“Harry”
,
“Brown”
,
“AddrHarry”
,
“CityHarry”
,
“StateHarry”
,
“ZipHarry”
,
“PhoneHarry”
);
}
@
After
public
void
tearDown
()
throws
Exception
{
addressBook
=
null
;
contact1
=
contact2
=
contact3
=
contact4
=
contact5
=
contact6
=
contact7
=
null
;
}
@
Test
public
void
testAddContact
()
{
// add three contacts
addressBook
.
add
(
contact1
);
addressBook
.
add
(
contact2
);
addressBook
.
add
(
contact3
);
//check the number of contacts in the addressBook
assertEquals
(
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
);
assertEquals
(
scan
.
nextLine
(),
“George Amberly”
);
assertEquals
(
scan
.
nextLine
(),
“Phone3”
);
scan
.
nextLine
();
// gets the space in between
assertEquals
(
scan
.
nextLine
(),
“John Donahue”
);
assertEquals
(
scan
.
nextLine
(),
“Phone2”
);
scan
.
nextLine
();
// gets the space in between
assertEquals
(
scan
.
nextLine
(),
“Amelia Smith”
);
assertEquals
(
scan
.
nextLine
(),
“Phone1”
);
}
@
Test
public
void
testAddWithStrings
()
{
//add three contacts
addressBook
.
add
(
“Amelia”
,
“Smith”
,
“Addr1”
,
“City1”
,
“State1”
,
“Zip1”
,
“Phone1”
);
addressBook
.
add
(
“John”
,
“Donahue”
,
“Addr2”
,
“City2”
,
“State2”
,
“Zip2”
,
“Phone2”
);
addressBook
.
add
(
“George”
,
“Amberly”
,
“Addr3”
,
“City3”
,
“State3”
,
“Zip3”
,
“Phone3”
);
//check the number of contacts in the addressBook
assertEquals
(
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
);
assertEquals
(
scan
.
nextLine
(),
“George Amberly”
);
assertEquals
(
scan
.
nextLine
(),
“Phone3”
);
scan
.
nextLine
();
// gets the space in between
assertEquals
(
scan
.
nextLine
(),
“John Donahue”
);
assertEquals
(
scan
.
nextLine
(),
“Phone2”
);
scan
.
nextLine
();
// gets the space in between
assertEquals
(
scan
.
nextLine
(),
“Amelia Smith”
);
assertEquals
(
scan
.
nextLine
(),
“Phone1”
);
}
@
Test
public
void
testAddWithSameLastName
()
{
addressBook
.
add
(
“Bob”
,
“Brown”
,
“AddrBob”
,
“CityBob”
,
“StateBob”
,
“ZipBob”
,
“PhoneBob”
);
addressBook
.
add
(
“Tom”
,
“Brown”
,
“AddrTom”
,
“CityTom”
,
“StateTom”
,
“ZipTom”
,
“PhoneTom”
);
addressBook
.
add
(
“Harry”
,
“Brown”
,
“AddrHarry”
,
“CityHarry”
,
“StateHarry”
,
“ZipHarry”
,
“PhoneHarry”
);
//check the number of contacts in the addressBook
assertEquals
(
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
);
assertEquals
(
scan
.
nextLine
(),
“Bob Brown”
);
assertEquals
(
scan
.
nextLine
(),
“PhoneBob”
);
scan
.
nextLine
();
// gets the space in between
assertEquals
(
scan
.
nextLine
(),
“Harry Brown”
);
assertEquals
(
scan
.
nextLine
(),
“PhoneHarry”
);
scan
.
nextLine
();
// gets the space in between
assertEquals
(
scan
.
nextLine
(),
“Tom Brown”
);
assertEquals
(
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
fail
(
“Not yet implemented”
);
}
@
Test
public
void
testGet
()
{
// add three contacts
addressBook
.
add
(
contact1
);
addressBook
.
add
(
contact2
);
addressBook
.
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”
);
assertEquals
(
contact
.
get
(
0
),
“George”
);
assertEquals
(
contact
.
get
(
2
),
“Addr3”
);
assertEquals
(
contact
.
get
(
5
),
“Zip3”
);
contact
=
addressBook
.
get
(
“John”
,
“Donahue”
);
assertEquals
(
contact
.
get
(
0
),
“John”
);
assertEquals
(
contact
.
get
(
2
),
“Addr2”
);
assertEquals
(
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
fail
(
“Not yet implemented”
);
}
@
Test
public
void
testDeleteContact
()
{
//add contacts to the address book
addressBook
.
add
(
contact1
);
addressBook
.
add
(
contact2
);
addressBook
.
add
(
contact3
);
addressBook
.
add
(
contact4
);
addressBook
.
add
(
contact5
);
addressBook
.
add
(
contact6
);
addressBook
.
add
(
contact7
);
//show that Tom Brown is in the address book
ArrayList
<
String
>
contact
=
addressBook
.
get
(
“Tom”
,
“Brown”
);
assertEquals
(
contact
.
get
(
0
),
“Tom”
);
assertEquals
(
contact
.
get
(
2
),
“AddrTom”
);
assertEquals
(
contact
.
get
(
5
),
“ZipTom”
);
//delete the contact using a reference to the contact object
addressBook
.
delete
(
contact6
);
//show that Tom Brown is no longer in the address book
assertEquals
(
addressBook
.
get
(
“Tom”
,
“Brown”
),
null
);
}
@
Test
public
void
testDeleteWithStrings
()
{
//add contacts to the address book
addressBook
.
add
(
contact1
);
addressBook
.
add
(
contact2
);
addressBook
.
add
(
contact3
);
addressBook
.
add
(
contact4
);
addressBook
.
add
(
contact5
);
addressBook
.
add
(
contact6
);
addressBook
.
add
(
contact7
);
//show that Tom Brown is in the address book
ArrayList
<
String
>
contact
=
addressBook
.
get
(
“Tom”
,
“Brown”
);
assertEquals
(
contact
.
get
(
0
),
“Tom”
);
assertEquals
(
contact
.
get
(
2
),
“AddrTom”
);
assertEquals
(
contact
.
get
(
5
),
“ZipTom”
);
//delete the contact using the first and last name (which make up the key)
addressBook
.
delete
(
“Tom”
,
“Brown”
);
//delete the contact using a reference to the contact object
assertEquals
(
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
fail
(
“Not yet implemented”
);
}
@
Test
public
void
testEdit
()
{
//add contacts to the address book
addressBook
.
add
(
contact1
);
addressBook
.
add
(
contact2
);
addressBook
.
add
(
contact3
);
addressBook
.
add
(
contact4
);
addressBook
.
add
(
contact5
);
addressBook
.
add
(
contact6
);
addressBook
.
add
(
contact7
);
// Test change of details without a change of key
ArrayList
<
String
>
contact
=
addressBook
.
get
(
“Penelope”
,
“Zuckerman”
);
assertEquals
(
contact
.
get
(
0
),
“Penelope”
);
assertEquals
(
contact
.
get
(
2
),
“Addr4”
);
assertEquals
(
contact
.
get
(
5
),
“Zip4”
);
// edit the Address and Phone Number
addressBook
.
edit
(
“Penelope”
,
“Zuckerman”
,
“Penelope”
,
“Zuckerman”
,
“4587 Oak St.”
,
“City4”
,
“State4”
,
“24095”
,
“Phone4”
);
contact
=
addressBook
.
get
(
“Penelope”
,
“Zuckerman”
);
// check if the edits were made
assertEquals
(
contact
.
get
(
0
),
“Penelope”
);
assertEquals
(
contact
.
get
(
2
),
“4587 Oak St.”
);
assertEquals
(
contact
.
get
(
5
),
“24095”
);
// Test change of details with a change of key
contact
=
addressBook
.
get
(
“Amelia”
,
“Smith”
);
assertEquals
(
contact
.
get
(
0
),
“Amelia”
);
assertEquals
(
contact
.
get
(
2
),
“Addr1”
);
assertEquals
(
contact
.
get
(
5
),
“Zip1”
);
// edit the last name (part of the key) and the address
addressBook
.
edit
(
“Amelia”
,
“Smith”
,
“Amelia”
,
“Babble”
,
“1422 Turtle Bay Way”
,
“City1”
,
“State1”
,
“Zip1”
,
“Phone1”
);
// make sure the old contact was deleted from address book
assertEquals
(
addressBook
.
get
(
“Amelia”
,
“Smith”
),
null
);
// check if the edits were made
contact
=
addressBook
.
get
(
“Amelia”
,
“Babble”
);
assertEquals
(
contact
.
get
(
0
),
“Amelia”
);
assertEquals
(
contact
.
get
(
2
),
“1422 Turtle Bay Way”
);
assertEquals
(
contact
.
get
(
5
),
“Zip1”
);
}
@
Test
public
void
testToTable
()
{
//add contacts to the address book
addressBook
.
add
(
contact1
);
addressBook
.
add
(
contact2
);
addressBook
.
add
(
contact3
);
addressBook
.
add
(
contact4
);
addressBook
.
add
(
contact5
);
addressBook
.
add
(
contact6
);
addressBook
.
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
();
assertEquals
(
table
[
2
][
0
],
“Harry”
);
assertEquals
(
table
[
2
][
1
],
“Brown”
);
assertEquals
(
table
[
4
][
2
],
“Addr2”
);
assertEquals
(
table
[
4
][
3
],
“City2”
);
assertEquals
(
table
[
6
][
4
],
“State4”
);
assertEquals
(
table
[
6
][
5
],
“Zip4”
);
}
}
Java5/java5/src/assignment5/BinarySearchTreeInterface.java
Java5/java5/src/assignment5/BinarySearchTreeInterface.java
package
assignment5
;
import
java
.
util
.
ArrayList
;
/**
* 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
*
@author
Professor Myers
*
*
@param
*
@param
The object type of the data that is being stored in the Binary Search Tree
*/
interface
BinarySearchTreeInterface
<
T
,
S
>
{
/**
* Adds the data to the binary search tree. The key is used to determine where to place
* the data into the binary search tree.
*
@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
);
/**
* Deletes the object in the binary search tree that corresponds to the key
*
@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
);
/**
* Performs an inorder traversal of the binary search tree and returns an ArrayList of the data objects in order
*
@return
an ArrayList of the data objects in order
*/
public
ArrayList
<
S
>
inOrder
();
/**
* Determines if the key is in the binary search tree
*
@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
);
/**
* Returns the data object that corresponds the the 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
Java5/java5/src/assignment5/BinarySearchTreeTest.java
package
assignment5
;
import
static
org
.
junit
.
Assert
.
*
;
import
java
.
util
.
ArrayList
;
import
org
.
junit
.
After
;
import
org
.
junit
.
Before
;
import
org
.
junit
.
Test
;
//your Contact class must have a getFirstName method for these tests to work
public
class
BinarySearchTreeTest
{
BinarySearchTree
<
String
,
Contact
>
bst
;
Contact
contact1
,
contact2
,
contact3
,
contact4
,
contact5
,
contact6
,
contact7
;
@
Before
public
void
setUp
()
throws
Exception
{
bst
=
new
BinarySearchTree
<
String
,
Contact
>
();
contact1
=
new
Contact
(
“Amelia”
,
“Smith”
,
“Addr1”
,
“City1”
,
“State1”
,
“Zip1”
,
“Phone1”
);
contact2
=
new
Contact
(
“John”
,
“Donahue”
,
“Addr2”
,
“City2”
,
“State2”
,
“Zip2”
,
“Phone2”
);
contact3
=
new
Contact
(
“George”
,
“Amberly”
,
“Addr3”
,
“City3”
,
“State3”
,
“Zip3”
,
“Phone3”
);
contact4
=
new
Contact
(
“Penelope”
,
“Zuckerman”
,
“Addr4”
,
“City4”
,
“State4”
,
“Zip4”
,
“Phone4”
);
contact5
=
new
Contact
(
“Bob”
,
“Brown”
,
“AddrBob”
,
“CityBob”
,
“StateBob”
,
“ZipBob”
,
“PhoneBob”
);
contact6
=
new
Contact
(
“Tom”
,
“Brown”
,
“AddrTom”
,
“CityTom”
,
“StateTom”
,
“ZipTom”
,
“PhoneTom”
);
contact7
=
new
Contact
(
“Harry”
,
“Brown”
,
“AddrHarry”
,
“CityHarry”
,
“StateHarry”
,
“ZipHarry”
,
“PhoneHarry”
);
}
@
After
public
void
tearDown
()
throws
Exception
{
bst
=
null
;
contact1
=
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
bst
.
add
(
“Smith, Amelia”
,
contact1
);
bst
.
add
(
“Donahue, John”
,
contact2
);
bst
.
add
(
“Amberly, George”
,
contact3
);
bst
.
add
(
“Zuckerman, Penelope”
,
contact4
);
bst
.
add
(
“Brown, Bob”
,
contact5
);
bst
.
add
(
“Brown, Tom”
,
contact6
);
bst
.
add
(
“Brown, Harry”
,
contact7
);
ArrayList
<
Contact
>
list
=
bst
.
inOrder
();
assertEquals
(
list
.
get
(
0
).
getFirstName
(),
“George”
);
assertEquals
(
list
.
get
(
1
).
getFirstName
(),
“Bob”
);
assertEquals
(
list
.
get
(
3
).
getFirstName
(),
“Tom”
);
assertEquals
(
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
bst
.
add
(
“Smith, Amelia”
,
contact1
);
bst
.
add
(
“Donahue, John”
,
contact2
);
bst
.
add
(
“Amberly, George”
,
contact3
);
bst
.
add
(
“Zuckerman, Penelope”
,
contact4
);
bst
.
add
(
“Brown, Bob”
,
contact5
);
bst
.
add
(
“Brown, Tom”
,
contact6
);
bst
.
add
(
“Brown, Harry”
,
contact7
);
ArrayList
<
Contact
>
list
=
bst
.
inOrder
();
assertEquals
(
list
.
get
(
0
).
getFirstName
(),
“George”
);
assertEquals
(
list
.
get
(
1
).
getFirstName
(),
“Bob”
);
assertEquals
(
list
.
get
(
3
).
getFirstName
(),
“Tom”
);
assertEquals
(
list
.
get
(
5
).
getFirstName
(),
“Amelia”
);
//delete a contact
assertEquals
(
bst
.
delete
(
“Brown, Bob”
),
true
);
//check if deleted from tree
list
=
bst
.
inOrder
();
assertEquals
(
list
.
get
(
0
).
getFirstName
(),
“George”
);
assertEquals
(
list
.
get
(
1
).
getFirstName
(),
“Harry”
);
assertEquals
(
list
.
get
(
3
).
getFirstName
(),
“John”
);
assertEquals
(
list
.
get
(
5
).
getFirstName
(),
“Penelope”
);
//test if returns false, not in tree
assertEquals
(
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
fail
(
“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
bst
.
add
(
“Smith, Amelia”
,
contact1
);
bst
.
add
(
“Donahue, John”
,
contact2
);
bst
.
add
(
“Amberly, George”
,
contact3
);
bst
.
add
(
“Zuckerman, Penelope”
,
contact4
);
bst
.
add
(
“Brown, Bob”
,
contact5
);
bst
.
add
(
“Brown, Tom”
,
contact6
);
bst
.
add
(
“Brown, Harry”
,
contact7
);
//check if key if found in the tree
assertEquals
(
bst
.
hasKey
(
“Donahue, John”
),
true
);
assertEquals
(
bst
.
hasKey
(
“Brown, Tom”
),
true
);
assertEquals
(
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
bst
.
add
(
“Smith, Amelia”
,
contact1
);
bst
.
add
(
“Donahue, John”
,
contact2
);
bst
.
add
(
“Amberly, George”
,
contact3
);
bst
.
add
(
“Zuckerman, Penelope”
,
contact4
);
bst
.
add
(
“Brown, Bob”
,
contact5
);
bst
.
add
(
“Brown, Tom”
,
contact6
);
bst
.
add
(
“Brown, Harry”
,
contact7
);
//check if finds key
assertEquals
(
bst
.
get
(
“Brown, Bob”
).
getFirstName
(),
“Bob”
);
//check if finds key
assertEquals
(
bst
.
get
(
“Amberly, George”
).
getFirstName
(),
“George”
);
//check if finds key
assertEquals
(
bst
.
get
(
“Brown, Robert”
),
null
);
}
}
Java5/java5/src/assignment5/Tabelize.java
Java5/java5/src/assignment5/Tabelize.java
package
assignment5
;
/**
* This guarantees that what is returned from toTable can be placed in a JTable
*
@author
Professor Myers
*
*/
public
interface
Tabelize
{
/**
*
*
@return
the contents as a 2 dimensional array of Strings
*/
String
[][]
toTable
();
}
Java5/java5/src/assignment5/TableUtility.java
Java5/java5/src/assignment5/TableUtility.java
package
assignment5
;
import
javax
.
swing
.
JTable
;
import
javax
.
swing
.
table
.
DefaultTableModel
;
import
javax
.
swing
.
table
.
TableModel
;
public
class
TableUtility
{
public
static
void
updateTable
(
Tabelize
tableContents
,
JTable
table
)
{
clearTable
(
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
);
}
}