Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***FILL IN THE PROVIDED CODE, WITH ONLY FILE 5-7 NEEDING TO BE FILLED *** Objective: The popular social network Facebook was founded by Mark Zuckerberg

***FILL IN THE PROVIDED CODE, WITH ONLY FILE 5-7 NEEDING TO BE FILLED ***

Objective: The popular social network Facebook was founded by Mark Zuckerberg and his classmates at Harvard University in 2004. At the time, he was a sophomore studying computer science. Design and implement an application that maintains the data for a simple social network. Each person in the network should have a profile that contains the persons name, an optional image, current status, and a list of friends. Your application should allow a user to join the network, leave the network, create a profile, modify the profile, search for other profiles, and add friends. The code and the output for this part are in the ZIP archive. Your programs output must be identical to the output of the sample run, _OUTPUT.txt.

File 1: _OUTPUT.txt

run:

Creating profiles and adding to database.

Name: John Doe

Status: My name is John.

Picture: BufferedImage@52e922

# of friends: 0

Friends:

Name: Jane Doe

Status: My name is Jane.

Picture: BufferedImage@25154f

# of friends: 0

Friends:

Name: Billy Bob

Status: My name is Billy Bob!

Picture: BufferedImage@10dea4

# of friends: 0

Friends:

Name: John Smith

Status: My name is also John.

Picture: BufferedImage@647e05

# of friends: 0

Friends:

-------------------------------------

Creating friendships.

Name: John Doe

Status: My name is John.

Picture: BufferedImage@52e922

# of friends: 2

Friends:

Jane Doe

John Smith

Name: Jane Doe

Status: My name is Jane.

Picture: BufferedImage@25154f

# of friends: 3

Friends:

John Doe

Billy Bob

John Smith

Name: Billy Bob

Status: My name is Billy Bob!

Picture: BufferedImage@10dea4

# of friends: 2

Friends:

Jane Doe

John Smith

Name: John Smith

Status: My name is also John.

Picture: BufferedImage@647e05

# of friends: 3

Friends:

John Doe

Jane Doe

Billy Bob

-------------------------------------

Changing statuses / names.

Name: John Doe

Status: My name is John.

Picture: BufferedImage@52e922

# of friends: 2

Friends:

Jane Smith

John Smith

Name: Jane Smith

Status: Now Mrs. Smith!

Picture: BufferedImage@25154f

# of friends: 3

Friends:

John Doe

Billy Bob

John Smith

Name: Billy Bob

Status: My name is Billy Bob!

Picture: BufferedImage@10dea4

# of friends: 2

Friends:

Jane Smith

John Smith

Name: John Smith

Status: Just got married!

Picture: BufferedImage@647e05

# of friends: 3

Friends:

John Doe

Jane Smith

Billy Bob

-------------------------------------

Ending a friendship.

Name: John Doe

Status: My name is John.

Picture: BufferedImage@52e922

# of friends: 2

Friends:

Jane Smith

John Smith

Name: Jane Smith

Status: Now Mrs. Smith!

Picture: BufferedImage@25154f

# of friends: 3

Friends:

John Doe

Billy Bob

John Smith

Name: Billy Bob

Status: My name is Billy Bob!

Picture: BufferedImage@10dea4

# of friends: 2

Friends:

Jane Smith

John Smith

Name: John Smith

Status: Just got married!

Picture: BufferedImage@647e05

# of friends: 3

Friends:

John Doe

Jane Smith

Billy Bob

-------------------------------------

Removing John.

Name: Jane Smith

Status: Now Mrs. Smith!

Picture: BufferedImage@25154f

# of friends: 2

Friends:

Billy Bob

John Smith

Name: Billy Bob

Status: My name is Billy Bob!

Picture: BufferedImage@10dea4

# of friends: 2

Friends:

Jane Smith

John Smith

Name: John Smith

Status: Just got married!

Picture: BufferedImage@647e05

# of friends: 2

Friends:

Jane Smith

Billy Bob

BUILD SUCCESSFUL (total time: 0 seconds)

File 2: AList

import java.util.Arrays; /**  *  * DO NOT CHANGE  *  * ADT list by using a re sizable array.  * @param <T> */ public class AList implements ListInterface { private T[] list; // Array of list entries; ignore list[0] private int numberOfEntries; private boolean initialized = false; private static final int DEFAULT_CAPACITY = 25; private static final int MAX_CAPACITY = 10000; public AList() { this(DEFAULT_CAPACITY); } // end default constructor public AList(int initialCapacity) { // Is initialCapacity too small? if (initialCapacity < DEFAULT_CAPACITY) { initialCapacity = DEFAULT_CAPACITY; } else // Is initialCapacity too big? { checkCapacity(initialCapacity); } // The cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] tempList = (T[]) new Object[initialCapacity + 1]; list = tempList; numberOfEntries = 0; initialized = true; } // end constructor @Override public void add(T newEntry) { add(numberOfEntries + 1, newEntry); } // end add // Precondition: The array list has room for another entry. @Override public void add(int newPosition, T newEntry) { checkInitialization(); if ((newPosition >= 1) && (newPosition <= numberOfEntries + 1)) { if (newPosition <= numberOfEntries) { makeRoom(newPosition); } list[newPosition] = newEntry; numberOfEntries++; ensureCapacity(); // Ensure enough room for next add } // end if else { throw new IndexOutOfBoundsException( "Given position of add's new entry is out of bounds."); } } // end add @Override public T remove(int givenPosition) { checkInitialization(); if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) { assert !isEmpty(); T result = list[givenPosition]; // Get entry to be removed // Move subsequent entries towards entry to be removed, // unless it is last in list if (givenPosition < numberOfEntries) { removeGap(givenPosition); } numberOfEntries--; return result; } else { throw new IndexOutOfBoundsException( "Illegal position given to remove operation."); } } // end remove @Override public void clear() { checkInitialization(); // Clear entries but retain array; no need to create a new array for (int index = 1; index <= numberOfEntries; index++) // Loop is part of Q4 { list[index] = null; } numberOfEntries = 0; } // end clear @Override public T replace(int givenPosition, T newEntry) { checkInitialization(); if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) { assert !isEmpty(); T originalEntry = list[givenPosition]; list[givenPosition] = newEntry; return originalEntry; } else { throw new IndexOutOfBoundsException( "Illegal position given to replace operation."); } } // end replace @Override public T getEntry(int givenPosition) { checkInitialization(); if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) { assert !isEmpty(); return list[givenPosition]; } else { throw new IndexOutOfBoundsException( "Illegal position given to getEntry operation."); } } // end getEntry @Override public T[] toArray() { checkInitialization(); // The cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] result = (T[]) new Object[numberOfEntries]; // Unchecked cast for (int index = 0; index < numberOfEntries; index++) { result[index] = list[index + 1]; } // end for return result; } // end toArray @Override public boolean contains(T anEntry) { checkInitialization(); boolean found = false; int index = 1; while (!found && (index <= numberOfEntries)) { if (anEntry.equals(list[index])) { found = true; } index++; } // end while return found; } // end contains @Override public int getLength() { return numberOfEntries; } // end getLength @Override public boolean isEmpty() { return numberOfEntries == 0; } // end isEmpty // Doubles the capacity of the array list if it is full. // Precondition: checkInitialization has been called. private void ensureCapacity() { int capacity = list.length - 1; if (numberOfEntries >= capacity) { int newCapacity = 2 * capacity; checkCapacity(newCapacity); // Is capacity too big? list = Arrays.copyOf(list, newCapacity + 1); } // end if } // end ensureCapacity // Makes room for a new entry at newPosition. // Precondition: 1 <= newPosition <= numberOfEntries + 1; // numberOfEntries is list's length before addition; // checkInitialization has been called. private void makeRoom(int newPosition) { assert (newPosition >= 1) && (newPosition <= numberOfEntries + 1); int newIndex = newPosition; int lastIndex = numberOfEntries; // Move each entry to next higher index, starting at end of // list and continuing until the entry at newIndex is moved for (int index = lastIndex; index >= newIndex; index--) { list[index + 1] = list[index]; } } // end makeRoom // Shifts entries that are beyond the entry to be removed to the // next lower position. // Precondition: 1 <= givenPosition < numberOfEntries; // numberOfEntries is list's length before removal; // checkInitialization has been called. private void removeGap(int givenPosition) { assert (givenPosition >= 1) && (givenPosition < numberOfEntries); int removedIndex = givenPosition; int lastIndex = numberOfEntries; for (int index = removedIndex; index < lastIndex; index++) { list[index] = list[index + 1]; } } // end removeGap // Throws an exception if this object is not initialized. private void checkInitialization() { if (!initialized) { throw new SecurityException("AList object is not initialized properly."); } } // end checkInitialization // Throws an exception if the client requests a capacity that is too large. private void checkCapacity(int capacity) { if (capacity > MAX_CAPACITY) { throw new IllegalStateException("Attempt to create a list " + "whose capacity exceeds " + "allowed maximum."); } } // end checkCapacity } // end AList 

File 3: Driver

/**  * DO NOT CHANGE  */ public class Driver { public static void main(String[] args) { System.out.println("Creating profiles and adding to database."); ProfileManager m = new ProfileManager(); Profile john = new Profile(); john.setName("John", "Doe"); john.setStatus("My name is John."); Profile jane = new Profile(); jane.setName("Jane", "Doe"); jane.setStatus("My name is Jane."); Profile billy = new Profile(); billy.setName(new Name("Billy", "Bob")); billy.setStatus("My name is Billy Bob!"); Profile smith = new Profile(); smith.setName(new Name("John", "Smith")); smith.setStatus("My name is also John."); m.addProfile(john); m.addProfile(jane); m.addProfile(billy); m.addProfile(smith); m.display(); System.out.println("------------------------------------- "); System.out.println("Creating friendships. "); m.createFriendship(john, jane); m.createFriendship(jane, billy); m.createFriendship(john, smith); m.createFriendship(smith, jane); m.createFriendship(billy, smith); m.display(); System.out.println("------------------------------------- "); System.out.println("Changing statuses / names. "); smith.setStatus("Just got married!"); jane.setStatus("Now Mrs. Smith!"); jane.setName("Jane", "Smith"); m.display(); System.out.println("------------------------------------- "); System.out.println("Ending a friendship. "); m.endFriendship(john, billy); m.display(); System.out.println("------------------------------------- "); System.out.println("Removing John. "); m.removeProfile(john); m.display(); } // end main } // end Driver 

File 4: ListInterface

/**  *  * DO NOT CHANGE  *  * An interface for the ADT list.  * @param <T> */ public interface ListInterface { /**  * Adds a new entry to the end of this list. Entries currently in the list  * are unaffected. The list's size is increased by 1.  *  * @param newEntry The object to be added as a new entry.  */  public void add(T newEntry); /**  * Adds a new entry at a specified position within this list. Entries  * originally at and above the specified position are at the next higher  * position within the list. The list's size is increased by 1.  *  * @param newPosition An integer that specifies the desired position of the  * new entry.  * @param newEntry The object to be added as a new entry.  * @throws IndexOutOfBoundsException if either newPosition < 1 or newPosition  * > getLength() + 1.  */  public void add(int newPosition, T newEntry); /**  * Removes the entry at a given position from this list. Entries originally  * at positions higher than the given position are at the next lower  * position within the list, and the list's size is decreased by 1.  *  * @param givenPosition An integer that indicates the position of the entry  * to be removed.  * @return A reference to the removed entry.  * @throws IndexOutOfBoundsException if either givenPosition < 1 or givenPosition  * > getLength().  */  public T remove(int givenPosition); /**  * Removes all entries from this list.  */  public void clear(); /**  * Replaces the entry at a given position in this list.  *  * @param givenPosition An integer that indicates the position of the entry  * to be replaced.  * @param newEntry The object that will replace the entry at the position  * givenPosition.  * @return The original entry that was replaced.  * @throws IndexOutOfBoundsException if either givenPosition < 1 or givenPosition  * > getLength().  */  public T replace(int givenPosition, T newEntry); /**  * Retrieves the entry at a given position in this list.  *  * @param givenPosition An integer that indicates the position of the  * desired entry.  * @return A reference to the indicated entry.  * @throws IndexOutOfBoundsException if either givenPosition < 1 or givenPosition  * > getLength().  */  public T getEntry(int givenPosition); /**  * Retrieves all entries that are in this list in the order in which they  * occur in the list.  *  * @return A newly allocated array of all the entries in the list. If the  * list is empty, the returned array is empty.  */  public T[] toArray(); /**  * Sees whether this list contains a given entry.  *  * @param anEntry The object that is the desired entry.  * @return True if the list contains anEntry, or false if not.  */  public boolean contains(T anEntry); /**  * Gets the length of this list.  *  * @return The integer number of entries currently in the list.  */  public int getLength(); /**  * Sees whether this list is empty.  *  * @return True if the list is empty, or false if not.  */  public boolean isEmpty(); } // end ListInterface 

File 5: Name

public class Name { private String first; // First name private String last; // Last name public Name() { } public Name(String firstName, String lastName) { } public void setName(String firstName, String lastName) { } public String getName() { } public void setFirst(String firstName) { } public String getFirst() { } public void setLast(String lastName) { } public String getLast() { } public void giveLastNameTo(Name aName) { } public String toString() { } } 

File 6: Profile

import java.awt.image.*; /**  * A profile on a simple social network.  */ public class Profile { private BufferedImage profilePicture; private Name profileName; private String status; private AList friends; /**  * DO NOT CHANGE  * Constructor for an instance of a profile.  */  public Profile() { /**  * DO NOT CHANGE  * Constructor for an instance of a profile.  */  profilePicture = new BufferedImage(150, 150, BufferedImage.TYPE_INT_RGB); profileName = new Name("", ""); status = ""; friends = new AList<>(); } public BufferedImage getProfilePicture() { } public void setProfilePicture(BufferedImage newPicture) { } public Name getName() { } /**  * Sets the name associated with the profile  * to the given first and last name.  */  public void setName(String first, String last) { } /**  * Sets the name associated with the profile  * to the given name.  */  public void setName(Name name) { } /**  * Sets the current status of the profile  * to the given string.  */  public void setStatus(String stat) { } /**  * Returns the status associated with the profile.  */  public String getStatus() { } /**  * Returns a list of all the profile's friends  */  public AList getFriends() { } /**  * Adds a friend to the profile's list of friends.  */  public void addFriend(Profile p) { } /**  * Removes a friend from the profile's list of friends.  */  public void removeFriend(Profile p) { } public String toString() { } /**  * Displays the profile's information and friends.  */  public void display() { } } // end Profile 

File 7: ProfileManager

/**  * A profile manager on a simple social network.  */  public class ProfileManager { private final AList allProfiles; /**  * DO NOT CHANGE  * Constructor for an instance of a profile manager.  */  public ProfileManager() { /**  * DO NOT CHANGE  */  allProfiles = new AList<>(); } /**  * Adds a profile onto the social network.  */  public void addProfile(Profile p) { } /**  * Removes a profile from the social network.  */  public void removeProfile(Profile p) { } /**  * Created a friendship between two profiles on the social network.  */  public void createFriendship(Profile a, Profile b) { } /**  * Ends a friendship between two profiles on the social network.  */  public void endFriendship(Profile a, Profile b) { } /**  * Displays each profile's information and friends.  */  public void display() { } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning PostgreSQL On The Cloud Simplifying Database As A Service On Cloud Platforms

Authors: Baji Shaik ,Avinash Vallarapu

1st Edition

1484234464, 978-1484234464

Students also viewed these Databases questions