IMPORTANT. Make sure you attend the lab session to which you registered, show the result(s) of each exercise to your TA, and submit the .java files that you wrote and AnswersLab4.txt through OWL by 11:55pm on the same day as your lab session to get your marks for the lab.
SocialNetworking
project that you created in Lab 1.SocialNetwork.java
.
In the
main
method of the test harness add this statement (this should be the only statement in
main
):add("John","doe","johndoe@uwo.ca");
new
operator. When Java creates a static object, the object is allocated memory for the
static instance variables and for references to the static methods. Java creates only
one static object per class. When a non-static object is created it is allocated memory
for all non-static instance variables and for references to all non-static methods; a non-static
object also has a
reference to the unique static object of its class.
When a Java program is executed, the Java virtual machine looks for a method called main
in the class containing the program; the execution of every Java program starts at the
main
method. Since code cannot be executed unless there is an object
in memory with a reference to that code, the first thing that the virtual machine will do is to
create a static object for the class. Since method main
is static, there is a
reference to it inside the static object and this allows the Java virtual machine to
execute the program.
If the program that you just wrote in class SocialNetwork
could be executed (it
cannot be executed because of the compilation errors),
the virtual machine would start execution at method main
where the first statement
is add("John","doe","johndoe@uwo.ca")
. The virtual machine would then look for a
reference to method add
inside the static object (remember that the only object
in memory at this time is the static object). Since the static object does not have references
to non-static methods, the virtual machine would not be able to execute method add
.
This is the reason why the compiler issues the error message. From method main
a reference to a non-static method is invalid because a reference to that method would not
be stored in the static object of class SocialNetwork
.
add
so that
it it static: public static void add
. As you will see, there are no more compilation
errors inside method main
. However, now there are several compilation errors in
method add
. Explain why you are getting these compilation errors. Write your explanation
in AnswersLab4.txt.
SocialNetwork
as static
. What is the disadvantage (or
disadvantages) of doing this?
Write your answer in AnswersLab4.txt.
If you are unsure about what to answer, read these hints. Please first
think about all you have learned about static objects and try to answer the question without
reading the hints.
printFriends()
that prints the first name, last name, and email of
all the friends in friendList
. Information about each friend must be printed in a
separate line.
SocialNetwork
to be static. Change the declarations of methods add
and expandCapacity
to be static. This will fix all compilation errors. Add the following class
CreateNetworks.java to your project. Open
CreateNetworks.java
and study the code. The main
method creates
two objects of class SocialNetwork
, one stores a list of two friends: joe doe and jan
doe; the second one stores a list with only one friend: cs 1027.
Run the program. Why does the list
for network 1 includes the 3 friends joe doe, jan doe, and cs 1027? Why does the list for network 2
also includes the same 3 friends? We would expect the first list to contain only 2 friends and the
second list
to contain only one friend; explain why this is not the case and write your answer in
AnswersLab4.txt.
add
and
expandCapacity
so they are not static. Now you get back the
compilation error inside method main
. Since from a static method we cannot invoke a
non-static method, what we should do is to create a non-static object of the class
SocialNetwork
, and using that object we can now invoke the non-static method
add
:network
of type SocialNetwork
inside method
main
.
network
using the constructor public SocialNetwork()
(remember
how to do that?).
add
from the object referenced by network
:
network.add("john","doe","jd@uwo.ca");
.
CreateNetworks
and run this
program again. This time you should see the correct list of friends for network 1 and for network 2.
SocialNetwork
can be created? Write your
answer in AnswersLab4.txt.
SocialNetwork
class:
getNumFriends()
: this method must return the number of friends in
friendList
.clearFriends()
: this method must remove all friends from the list friendList
. main
code to do the following.
network1
of type SocialNetwork
and initialize it.network1
.network
using method printFriends()
.network1
.clearFriends
method of network1
. Print the list
of friends in network1
and
the value returned by getNumFriends
. (It must be 0.)network
. Is this list also empty? Write your answer is
AnswersLab4.txt.
SocialNetwork
class in an application program. Run the application MyFriends.java
. This is a simple application program that creates and prints
a list of friends. remove()
method for the person whose name was entered by the used,
and print the value that the method returns in the following format (assuming that the user
entered "John" and "Doe" when prompted): "When trying to remove John Doe method remove
returned the value: false". This message must be printed in ONE line and it must contain the
first name, last name, and value returned by remove
.remove
when the
user enters "exit" as the first name.