In this lab you will work with your own classes, and Javadoc. Upon completion of this lab, you should be able to do the following:
Make sure that you show the result(s) of each Exercise to your TA and that you submit the .java files your wrote through owl, to get your marks for the lab.
Preparation. Make sure you understand the parts of a class definition: instance variable declaration, constructor definition, method definition. Read this introduction to Javadoc.
To make it easier to test code that you have written for a Java class
you can add to that class a
main method that acts as a "test harness".
A test harness is a
main method that includes calls to methods that you
wish to test. For convention this
main method is the last method of the
SocialNetworkingproject that you created in Lab 1.
Personclass, in the file PersonTestHarness.txt. It creates a
Personobject (this tests the constructor) and prints the result of invoking most of the methods of the class. Add this main method to your
Person.java; it will execute the test harness (
equalsmethod: it should print a message like "same friend" if two objects of the class Person are the same according to the
equalsmethod documentation (i.e. if they have the same first name and last name), or print "not same friend" if they are not the same. (Hint: you can use friend1 and friend2 to compare two objects of class Person that are not the same, but for thorough testing, you should also create a third Person object who has the same name as one of the first two.)
Person.javaagain to execute the changed test harness.
getNumFriends(): it will return the number of friends in the list.
clearFriends(): it will remove all friends from the list. (Hint: this can be implemented with just one assignment statement. The
SocialNetworkclass has an instance variable called
numFriendsthat holds the current number of persons in the list of friends. If you set this to 0 (zero), you do not need to clear out the array
friendList- why not?)
SocialNetwork.javato test your new methods by doing the following. (Note that this is not a complete test of the class; it will just give you a start in using a test harness in the Lab. You can add more tests later if you wish.)
SocialNetworkobject using the constructor
getNumFriendsmethod by printing the value that this method returns
clearFriendsmethod (how will you show that this worked?)
SocialNetwork.javaare Javadoc comments. (You should have read the Introduction to Javadoc document before this Lab.) Add Javadoc comments for the new methods you added to
SocialNetwork.javain Exercise 2.
SocialNetworkclass in an application program. Run the application
MyFriends.javathat is part of your SocialNetworking project from Lab 1. It is a simple application program that creates and prints a list of friends.
remove()method to remove one of the friends whose name is already in the list. If the method returns the value
truethen print a message giving the name of the person that was removed from the list and indicating that the person was removed, for example: "Mickey Mouse was removed successfully." If the method returns the value
falsethen print a message indicating that the specified person could not be removed, for example "Donald Duck was not in the list of friends".
remove()method for a person whose name is not in the list of friends, and print a similar message as above.