design patterns - Pulling data from a class instance that may not yet exist in Java -
in data structures java university course, we've been tasked making simple social network.
one of profile methods defined given interface recommend, supposed go array of profile objects holding list of people. instance follows, pick one, go instance's array follow list , return profile there suggestion of follow.
i don't understand how go this, because i'm supposed anticipate instance of class doesn't exist yet.
i don't expect whoever responds homework me, guidance how start appreciated.
here (very) bare-bones implementation can follow guide.
your main method:
public static void main(string args[]) { list<profile> profiles; // you'll have figure out how load these yourself. // sounds biggest part of assignment. // might not done here - maybe it's done in profile // class itself. depends on how want implement it. profiles = loadprofiles(); // grab first profile test. testprofile = profiles.get(0); // whichever random profile recommended. profile recommendedprofile = testprofile.recommend(); // print them user. (you'll have figure out how best.) if (recommendedprofile != null) system.out.println("test profile: " + testprofile.tostring() + " recommended profile: " + recommendedprofile.tostring()); } and possible implementation of profile class:
public class profile implements profileinterface { // list of friends. private list<profile> friendprofiles; public profile() { friendprofiles = new list<profile>(); } public profile getrandomfriend() { if (friendprofiles.size() == 0) return null; else return friendprofiles.get((int)(math.random() * friendprofiles.size()) } public void addfriend(profile friend) { friendprofiles.add(friend); } public profile recommend() { profile friend = this.getrandomfriend(); return friend.getrandomfriend(); } }
Comments
Post a Comment