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 you show the result(s) of each Exercise to your TA, to get your marks for the lab. Please submit the .java files your wrote on owl.
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 class.If you have a test harness, you do not need to have a separate application program in order to test your class: the test harness is part of the class you are testing. In this part of the Lab, you will get some experience in writing a test harness.
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
numFriends- 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 (Hint: see page 40 of the Topic 1 lecture notes)
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. (For simplicity, there is no file I/O in this application at present.)
remove()method to remove one of the friends whose name is already in the list, and report whether or not the remove was successful. Make sure you include the name of the person you removed in the message to the user, for example: "Mickey Mouse was removed successfully."
remove()method for a person whose name is not in the list of friends, and report whether or not the remove was successful.