JAVA ASSIGNMENTS AND SOURCE CODE
| Examples of Cool Java Applets w/ Source
Code
requires WT "web driver" more here |
|
Group Project
JAVA Programming I -- Section 0663
Phil Porvaznik -- Initial design and final report
PhilVazMark Lundemann -- Package, Class, Object
Zoot@Tampabay.rr.comTracey Morgan -- Driver and Testing
TMorgan72000@yahoo.comSwee-boon Wong -- Driver and Testing
Sweeboon23@yahoo.com
PROBLEM: Design and implement an address book object that contains a person's name, home address and phone number, business address and phone number, and numbers for their fax machine, cellular phone, and pager.
Write a test plan for the object and implement a driver that tests it. (Chapter 7, page 336, Problem #1)
INITIAL DESIGN AND SPECIFICATION
Package Name: addressbook
Class Name: AddressBook
To design and implement an address book object and package, we need the following field names:
first = First Name middle = Middle Name last = Last Name |
homeadd = Home Address homeph = Home Phone Number busadd = Business Address |
busph = Business Phone Number faxnum = Fax Number cellnum = Cell Phone Number pagenum = Pager Number |
These are all a string of characters. For the class "AddressBook" the constructor needs to store each into its corresponding instance variable (or field). To complete the package we need a number of "observers" :
knowFirstName ( ), knowMiddleName ( ), knowLastName ( ), knowHomeAddress ( ), knowHomePhone ( ), knowBusinessAddress ( ), knowBusinessPhone ( ), knowFaxNumber ( ), knowCellNumber ( ), knowPagerNumber ( )
These return the corresponding "field values" for the address book. Additional observers are needed to return the proper format for first, middle, and last name or last, first, middle (with a comma separator).
firstMidLast ( ), lastFirstMid ( )
Also needed are comparison methods to compare our first, middle, last names:
public boolean equals (AddressBook otherName)
public int compareTo (AddressBook otherName)
Finally, the driver program implements the address book package/class and we would "hard code" values into different address book objects (testBook, secondBook, differentBook1, differentBook2, differentBook3) to test the comparison methods (equals and compareTo).
The following is the test plan (class method, reason, input, and expected output), followed by the source code for the address book package and the driver program that solves the problem.
TEST PLAN AND SCRIPT
|
Method |
Reason |
Input Values |
Expected Output |
|
AddressBook |
Constructor |
"Jane", "Ann", "Smith",
"1234 Bar Lane Tampa FL", "234-456-5678", "586 Berwin Blvd Tampa FL", "234-678-5436",
"234-666-8769", "234-528-4763", "234-528-2154" |
AddressBook object with 10
Fields |
|
knowFirstName |
Observer |
instance from constructor test |
"Jane" |
|
knowMiddleName |
Observer |
instance from constructor test |
"Ann" |
|
knowLastName |
Observer |
instance from constructor test |
"Smith" |
|
knowHomeAddress |
Observer |
instance from constructor test |
" |
|
knowHomePhone |
Observer |
instance from constructor test |
"234-456-5678" |
|
knowBusinessAddress |
Observer |
instance from constructor test |
" |
|
knowBusinessPhone |
Observer |
instance from constructor test |
"234-678-5436" |
|
knowFaxNumber |
Observer |
instance from constructor test |
"234-666-8769" |
|
knowCellNumber |
Observer |
instance from constructor test |
"234-528-4763" |
|
knowPagerNumber |
Observer |
instance from constructor test |
"234-528-2154" |
|
firstMidLast |
Observer |
instance from constructor test |
"Jane Ann Smith" |
|
lastFirstMid |
Observer |
instance from constructor test |
"Smith, Jane Ann" |
|
Equals |
Equality Test |
instance, second object with same values |
TRUE |
|
Equals |
Inequality Test |
instance, second object with same first, different middle
and last |
FALSE |
|
Equals |
Inequality Test |
instance, second object with same first and middle,
different last |
FALSE |
|
Equals |
Inequality Test |
instance, second obect with
different first, middle, and last |
FALSE |
|
compareTo |
Equality Test |
instance, second object with same values |
0 |
|
compareTo |
Greater Than Test |
instance, second object with lesser value |
Positive int |
|
compareTo |
Less than Test |
instance, second object with greater value |
Negative int |
SOURCE CODE FOR ADDRESS BOOK PACKAGE
// // *************************************************************************** // // GROUP PROJECT Java Programming I Section 0663 // // MEMBERS: Phil Porvaznik PhilVaz -- Initial design/Final Report // Mark Lundemann zoot@tampabay.rr.com -- Package Design // Tracey Morgan TMorgan72000@yahoo.com -- Testing and Driver // Swee-Boon Wong Sweeboon23@yahoo.com -- Testing and Driver // // *************************************************************************** // // ADDRESS BOOK Chapter 7, page 336, Problem # 1 // // ADDRESS BOOK PACKAGE // // *************************************************************************** //
package addressbook;
public class AddressBook
// This class defines an address book, consisting of 10 parts
{
// Class constant
final String PUNCT = ", "; // Punctuation for formatting
// Instance variable
String first; String middle; String last; String homeadd; String homeph; String busadd; String busph; String faxnum; String cellnum; String pagenum;
// Constructors
public AddressBook(String firstName, String middleName, String lastName,
String homeAddress, String homePhone, String businessAddress, String businessPhone,
String faxNumber, String cellNumber, String pagerNumber)
// Init an Address Book object with all addressbook fields
{
first=firstName; middle=middleName; last=lastName; homeadd=homeAddress; homeph=homePhone; busadd=businessAddress; busph=businessPhone; faxnum=faxNumber; cellnum=cellNumber; pagenum=pagerNumber;
}
// Basic observers that return the value of each field
public String knowFirstName ()
{
return first;
}
public String knowMiddleName ()
{
return middle;
}
public String knowLastName ()
{
return last;
}
public String knowHomeAddress ()
{
return homeadd;
}
public String knowHomePhone ()
{
return homeph;
}
public String knowBusinessAddress ()
{
return busadd;
}
public String knowBusinessPhone ()
{
return busph;
}
public String knowFaxNumber ()
{
return faxnum;
}
public String knowCellNumber ()
{
return cellnum;
}
public String knowPagerNumber ()
{
return pagenum;
}
// Additional observers for first middle last format
public String firstMidLast ()
{
return first + " " + middle + " " + last;
}
public String lastFirstMid ()
{
return last + PUNCT + first + " " + middle;
}
// Equals and CompareTo methods
public boolean equals (AddressBook otherName)
{
return
first.equals (otherName.first) &&
middle.equals (otherName.middle) &&
last.equals (otherName.last);
}
public int compareTo (AddressBook otherName)
{
int result;
result = last.toUpperCase().compareTo(otherName.last.toUpperCase());
if (result != 0)
return result;
else
{
result = first.toUpperCase().compareTo(otherName.first.toUpperCase());
if (result != 0)
return result;
else
return
result = middle.toUpperCase().compareTo(otherName.middle.toUpperCase());
}
}
}
SOURCE CODE FOR DRIVER PROGRAM
// // *************************************************************************** // // GROUP PROJECT Java Programming I Section 0663 // // MEMBERS: Phil Porvaznik PhilVaz -- Initial design/Final Report // Mark Lundemann zoot@tampabay.rr.com -- Package Design // Tracey Morgan TMorgan72000@yahoo.com -- Testing and Driver // Swee-Boon Wong Sweeboon23@yahoo.com -- Testing and Driver // // *************************************************************************** // // ADDRESS BOOK Chapter 7, page 336, Problem # 1 // // DRIVER JAVA SOURCE CODE // // *************************************************************************** //
// // Import the Address Book Package //
import addressbook.*; import java.*;
public class driver
{
static AddressBook testBook, secondBook, differentBook1, differentBook2,
differentBook3;
public static void main(String[] arg)
{
//
// Heading1 for Test Results
//
System.out.println("Test results for Package addressbook");
System.out.println();
// // Heading2 for Test Results //
System.out.println("Class " + "Method " +
"Test# " + "Expected Output " +
"Observed Output");
System.out.println("--------------------------------------------"+
"------------------------------------");
//
// Constructor Test
//
testBook = new AddressBook("Jane", "Ann", "Smith", "1234 Bar Lane Tampa FL",
"234-456-5678", "586 Berwin Blvd Tampa FL", "234-678-5436", "234-666-8769",
"234-528-4763", "234-528-2154");
if (testBook.knowFirstName().equals("Jane") &&
testBook.knowMiddleName().equals("Ann") &&
testBook.knowLastName().equals("Smith") &&
testBook.knowHomeAddress().equals("1234 Bar Lane Tampa FL") &&
testBook.knowHomePhone().equals("234-456-5678") &&
testBook.knowBusinessAddress().equals("586 Berwin Blvd Tampa FL") &&
testBook.knowBusinessPhone().equals("234-678-5436") &&
testBook.knowFaxNumber().equals("234-666-8769") &&
testBook.knowCellNumber().equals("234-528-4763") &&
testBook.knowPagerNumber().equals("234-528-2154")
)
//
// If Fields equal then Output Header
//
{
System.out.println("AddressBook " + "AddressBook " +
" 1 " + "10 Field Entries " + "10 Field Entries");
}
//
// If Fields not equal then we have a Mismatch Error
//
else
{
System.out.println("Mismatch on Field/s\n");
}
//
// Observer Test
//
System.out.println("AddressBook " + "knowFirstName " +
" 1 " + "Jane " + testBook.knowFirstName());
System.out.println("AddressBook " + "knowMiddleName " +
" 1 " + "Ann " + testBook.knowMiddleName());
System.out.println("AddressBook " + "knowLastName " +
" 1 " + "Smith " + testBook.knowLastName());
System.out.println("AddressBook " + "knowHomeAddress " +
" 1 " + "1234 Bar Lane Tampa FL " + testBook.knowHomeAddress());
System.out.println("AddressBook " + "knowHomePhone " +
" 1 " + "234-456-5678 " + testBook.knowHomePhone());
System.out.println("AddressBook " + "knowBusinessAddress" +
" 1 " + "586 Berwin Blvd Tampa FL " + testBook.knowBusinessAddress());
System.out.println("AddressBook " + "knowBusinessPhone " +
" 1 " + "234-678-5436 " + testBook.knowBusinessPhone());
System.out.println("AddressBook " + "knowFaxNumber " +
" 1 " + "234-666-8769 " + testBook.knowFaxNumber());
System.out.println("AddressBook " + "knowCellNumber " +
" 1 " + "234-528-4763 " + testBook.knowCellNumber());
System.out.println("AddressBook " + "knowPagerNumber " +
" 1 " + "234-528-2154 " + testBook.knowPagerNumber());
System.out.println("AddressBook " + "firstMidLast " +
" 1 " + "Jane Ann Smith " + testBook.firstMidLast());
System.out.println("AddressBook " + "lastFirstMid " +
" 1 " + "Smith, Jane Ann " + testBook.lastFirstMid());
System.out.println("AddressBook " + "lastFirstMid " +
" 1 " + "Smith, Jane Ann " + testBook.lastFirstMid());
//
// Heading2 for Test Results
//
System.out.println("\n\nClass " + "Method " +
"Test# " + "Expected Output " + "Observed Output");
System.out.println("--------------------------------------------"+
"------------------------------------");
//
// Create second object with the same values as testBook
//
secondBook = new AddressBook("Jane", "Ann", "Smith", "1234 Bar Lane Tampa FL",
"234-456-5678", "586 Berwin Blvd Tampa FL", "234-678-5436", "234-666-8769",
"234-528-4763", "234-528-2154");
//
// Equality Test using second object with the same first, middle, and last
//
System.out.println("AddressBook " + "equals " +
" 1 " + "true " + testBook.equals(secondBook));
//
// Inequality Test using object with same first and different middle/last name
//
differentBook1 = new AddressBook("Jane", "Anne", "Smitt", "1234 Bar Lane Tampa FL",
"234-456-5678", "586 Berwin Blvd Tampa FL", "234-678-5436", "234-666-8769",
"234-528-4763", "234-528-2154");
System.out.println("AddressBook " + "equals " +
" 2 " + "false " + testBook.equals(differentBook1));
//
// Inequality Test using an object with same first/middle but different last name
//
differentBook2 = new AddressBook("Jane", "Ann", "Smitt", "1234 Bar Lane Tampa FL",
"234-456-5678", "586 Berwin Blvd Tampa FL", "234-678-5436", "234-666-8769",
"234-528-4763", "234-528-2154");
System.out.println("AddressBook " + "equals " +
" 3 " + "false " + testBook.equals(differentBook2));
//
// Inequality Test using object with different first, middle, last name
//
differentBook3 = new AddressBook("Janet", "Anne", "Adams", "1234 Bar Lane Tampa FL",
"234-456-5678", "586 Berwin Blvd Tampa FL", "234-678-5436", "234-666-8769",
"234-528-4763", "234-528-2154");
System.out.println("AddressBook " + "equals " +
" 4 " + "false " + testBook.equals(differentBook3));
//
// Equality test using compareTo method.
// Comparison is made using a second object with same values of the test object
//
System.out.println("AddressBook " + "compareTo " +
" 1 " + "0 " + testBook.compareTo(secondBook));
//
// Greater than test
// Comparison is made using a second object with same values of the test object
//
System.out.println("AddressBook " + "compareTo " +
" 2 " + "Greater Than Test " + testBook.compareTo(differentBook3));
//
// Less than test
// Comparison is made using a second object with same values of the test object
//
System.out.println("AddressBook " + "compareTo " +
" 3 " + "Less Than Test " + testBook.compareTo(differentBook1));
} // end main
} // end class driver
OUTPUT TEST RESULTS

END OF REPORT