Results 1 to 1 of 1
- 10-03-2011, 12:40 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
NEED HELP in IMPLEMENTING A GIVEN INTERFACE!
OBJECTIVE : Implement a given interface which is a network of people objects linked together with a friendship relation.
------------------------------------------------------------------------------------
An interface for SocialPerson and SocialNetwork are given below. An implementing class for each of these interfaces is given, but I need to complete these implementations.
A sample main method is given to illustrate the use of the classes and their methods.
Read the included code comment for each method of the interfaces to understand what each method should accomplish. There are also two methods in the SocialNetwork interface that are worth bonus marks, potentially earning you up to 12/10.
------------------------------------------------------------------------------------------
MainMethod.java
Java Code:public class MainMethod { /** Sample usage of the SocialPerson and SocialNetwork interfaces * * @param args * @throws PersonAlreadyExistsException * @throws PersonDoesNotExistException */ public static void main(String[] args) throws PersonAlreadyExistsException, PersonDoesNotExistException { // TODO Auto-generated method stub SocialNetwork mySocialNetwork = new UBCOBook(); // Creating a bunch of characters SocialPerson alice = new User("Alice Smith", "Kelowna", 20); SocialPerson bob = new User("Bob Jones", "Kelowna", 20); SocialPerson carl = new User("Carl Li", "Kelowna", 20); SocialPerson demetri = new User("Demetri Harper", "Kelowna", 20); SocialPerson earl = new User("Earl Martin", "Kelowna", 20); SocialPerson foo = new User("Foo Bar", "Kelowna", 20); SocialPerson guy = new User("Guy Lian", "Kelowna", 20); SocialPerson hoang = new User("Hoang Long", "Kelowna", 20); // Adding them to the network mySocialNetwork.add(alice); mySocialNetwork.add(bob); mySocialNetwork.add(carl); mySocialNetwork.add(demetri); mySocialNetwork.add(earl); mySocialNetwork.add(foo); mySocialNetwork.add(guy); mySocialNetwork.add(hoang); // Check mySocialNetwork.exists("BobJones"); // returns false mySocialNetwork.exists("bob jones"); // returns true System.out.println("\nThe members of the network are:\n" + mySocialNetwork.listMembers()); // Creating friend links alice.addFriend(bob.getName(),mySocialNetwork); // adding by name string alice.addFriend("Carl Li",mySocialNetwork); // adding by name, which should be a unique identifier alice.addFriend(demetri,mySocialNetwork); // adding by object earl.addFriend(alice,mySocialNetwork); alice.addFriend(foo,mySocialNetwork); guy.addFriend(alice,mySocialNetwork); // should function the same as alice.addFriend(guy); bob.addFriend(foo,mySocialNetwork); bob.addFriend(guy,mySocialNetwork); foo.addFriend(hoang,mySocialNetwork); foo.addFriend(hoang,mySocialNetwork); System.out.println("\n"+alice.getName()+" has " + alice.numFriends() + " friends, and they are:"); System.out.println(alice.getFriends()); // Should write "Alice Smith has 6 friends, and they are:" // ...(everyone except Hoang Long) ... foo.addFriend("Billy Bob",mySocialNetwork); // should create a user "Billy Bob" with no age or city and add him to the network // For Bar and Billy Bob should be friends // Error cases: guy.addFriend(guy,mySocialNetwork); // should not modify guy's friend list, and report "User Guy Lian cannot add him or her self as friend" SocialPerson extra = new User("Extra Person", "Kelowna", 20); extra.addFriend(alice, mySocialNetwork); // should not modify extra's friend list, and report "extra does not belong in mySocialNetwork" } }
PersonAlreadyExistsException.java
Java Code:/* This doesn't really need to be touched */ public class PersonAlreadyExistsException extends Exception { public PersonAlreadyExistsException() { super(); } public PersonAlreadyExistsException(String errorMessage) { super(errorMessage); } }
PersonDoesNotExistException.java
Java Code:public class PersonDoesNotExistException extends Exception { public PersonDoesNotExistException() { super(); } public PersonDoesNotExistException(String errorMessage) { super(errorMessage); } }
SocialNetwork.java
Java Code:/** Interface for a Social Network, which will just basically * be a collection of people and some basic functions on the * set of people. * * For now, people will be uniquely identified by their name (of * type String) ignoring upper/lower case. The same name should * not exist in this social network twice. * * Efficiency is not an issue at this point. * You do not need to modify this file. */ public interface SocialNetwork { /** Returns the number of people in the social network */ public int size(); /** Checks if a name already exists in the social network */ public boolean exists(String name); /** Add a person to the network. Throws an error if person.name already exists in the network. */ // Note that this can be implemented in any way you see fit... // They can be stored in a LinkedList or ArrayList or any // implementation of the java.util interface for Collection public void add(SocialPerson person) throws PersonAlreadyExistsException; /** Return a list of all names, separated by newline ( "\n" ) */ public String listMembers(); /** Return a more detailed summary of the people in the network: everyone's name and that person's number of friends */ public String listMembersDetailed(); /** Returns the SocialPerson object corresponding to the parameter name, if it exists. * Otherwise returns null */ public SocialPerson getUser(String name); //Bonus is available if everything else is implemented. // Completing this method will make your assignment worth up to 11/10 /** Return a list of all people's names which contain a search substring in their name, ignoring upper/lower case. * e.g. searching for "jo" could return the following names if they exist in the network: "John Smith" * "Steve Jobs" "Mojo Nixon" but not the name "Raj Ohm". Return as a list of names as in listMembers() */ public String searchName(String s); //Another +10% bonus /** Takes a name 's' in the Social Network and returns the names of any other users 'x' who are NOT friends * of 's' and have at least two common friends with 's' */ public String suggestFriends(String s); }
SocialPerson.java
Java Code:/** An interface for a node of data related to a user in a social network. * * You do not need to modify this file. * */ public interface SocialPerson { /** Getters and Setters ... */ public String getName(); public void setName(String name); public int getAge(); public void setAge(int age); public String getCity(); public void setCity(String city); /** Adds a friend with name 'newFriend' to this person's friend list in the SocialNetwork 'network'. * This should also add 'this' to 'newFriend's friend list. * * If 'newfriend' does not exist in 'network', then create a person with name 'newfriend' and * him or her to the network. * * Throws an exception if 'this' is not in 'network.' * * A person should not be able to add his or her self as a friend. */ public void addFriend(String newFriend, SocialNetwork network) throws PersonDoesNotExistException; /** Adds a friend object 'newFriend' to this person's friend list. This should * also add 'this' to 'newFriend's friend list. * * If 'newFriend' does not exist in 'network', then create a person with name 'newfriend' and * him or her to the network. * * Throws an exception if 'this' is not in 'network.' * * A person should not be able to add his or her self as a friend. */ public void addFriend(SocialPerson newFriend, SocialNetwork network) throws PersonDoesNotExistException; /** Returns the friends of this person as a String, names separated by \n */ public String getFriends(); /** Returns the number this's friends */ public int numFriends(); }
UBCOBook.java
Java Code:public class UBCOBook implements SocialNetwork { @Override public void add(SocialPerson person) throws PersonAlreadyExistsException { // TODO Auto-generated method stub } @Override public boolean exists(String name) { // TODO Auto-generated method stub return false; } @Override public String listMembers() { // TODO Auto-generated method stub return null; } @Override public String listMembersDetailed() { // TODO Auto-generated method stub return null; } @Override public SocialPerson getUser(String name) { // TODO Auto-generated method stub return null; } @Override public int size() { // TODO Auto-generated method stub return 0; } // This one is bonus @Override public String searchName(String s) { // TODO Auto-generated method stub return null; } // This one is bonus @Override public String suggestFriends(String s) { // TODO Auto-generated method stub return null; } }
User.java
--------------------------------------------------------------------Java Code:import java.util.ArrayList; public class User implements SocialPerson { ArrayList<User> friendList; String name; String city; int age; // Constructor public User() { friendList = new ArrayList<User>(); } // overloaded Constructor public User(String name) { friendList = new ArrayList<User>(); this.name = name; } // overloaded Constructor public User(String name, String city, int age) { friendList = new ArrayList<User>(); this.name = name; this.city = city; this.age = age; } @Override public void addFriend(String newFriend, SocialNetwork network) throws PersonDoesNotExistException { // TODO Auto-generated method stub } @Override public void addFriend(SocialPerson newFriend, SocialNetwork network) throws PersonDoesNotExistException { // TODO Auto-generated method stub } @Override public int getAge() { // TODO Auto-generated method stub return 0; } @Override public String getCity() { // TODO Auto-generated method stub return null; } @Override public String getFriends() { // TODO Auto-generated method stub return null; } @Override public String getName() { // TODO Auto-generated method stub return null; } @Override public int numFriends() { // TODO Auto-generated method stub return 0; } @Override public void setAge(int age) { // TODO Auto-generated method stub } @Override public void setCity(String city) { // TODO Auto-generated method stub } @Override public void setName(String name) { // TODO Auto-generated method stub } }
Help would be GREATLY APPRECIATED. THANKS!!!!!!
Similar Threads
-
Implementing Interface
By mew in forum New To JavaReplies: 4Last Post: 02-16-2010, 03:33 PM -
help! implementing an interface
By manda147 in forum New To JavaReplies: 28Last Post: 11-17-2008, 04:27 AM -
Implementing an interface
By bugger in forum Advanced JavaReplies: 1Last Post: 01-09-2008, 01:35 PM -
Implementing Serializable interface
By javaplus in forum Advanced JavaReplies: 4Last Post: 12-18-2007, 12:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks