Transcribed TextTranscribed Text

Data Structures Assignment 1 1 Motivation we often discuss the importance of data structure design and implementation to the wide variety of computing applications. Despite decades of study, organizations must still regularly develop custom data structures to fulfill their applications’ specific needs, and as such the field remains hugely relevant to both computer scientists and software engineers. As an example of the magnitude of impact that data structures can have on a large system, read the following news article: In this assignment, you will implement two data structures to satisfy their specifications, which are provided in the form of interfaces. Note: Your goal in this assignment is to write classes that faithfully implement the ADTs described in the interfaces. Sample client code is provided as one example of how the classes may be used, and may also provide hints for how to solve certain problems in your implementations (since one of your classes will act as a client of the other). However, this sample code may not test all corner cases, and thus your goal is not simply to make this sample code work. You are strongly encouraged to write your own test client as well, to further test that your classes work in all the corner cases described in the interfaces. 2 Provided code First, look over the provided code. You can find this code on Pitt Box in a folder named -a1- abc123, where abc123 is your Pitt username. The SetInterface<E> interface describes a set, a data structure similar to a bag except that it does not allow duplicates. It is a generic interface that declares abstract methods for adding an item, removing an item (specified or unspecified), checking if the set is empty, determining the number of items in the set, and fetching the items from the set into an array. You should not modify this interface. 1 The SetFullException class is included to allow potential implementations of SetInterface that have a fixed capacity. Your implementation should not be fixed capacity, and thus should not throw this exception. The ProfileInterface interface describes a social network user’s profile. It declares abstract methods for setting and getting the user’s name and “about me” blurb, following other profiles, returning an array of the profile’s followed profiles, and recommending a new user to follow based on this profile’s “followed by those I follow” set. You should not modify this interface. The SocialClient class is a sample client of both Set and Profile. It is a social networking simulator that allows the user to carry out following, unfollowing, etc. on a simple social network. This class maintains a set of profiles (stored as SetInterface<ProfileInterface>. It also stores its data in a file (SocialClientData.bin) when quitting from the menu so that it can restore this data when it is run again. As noted above, this code is provided only as an example, and may not test all functionality of the required classes. 3 Tasks 3.1 Implement Set, 50 points Develop the generic class, Set<E>, a dynamic-capacity array-based implementation of the Set ADT described in SetInterface<E>. Include this class in package c.a1. Read the interface carefully (including comments) to ensure you implement it properly; it will be graded using a client that assumes all of the functionality described in the SetInterface, not just the behavior demonstrated in SocialClient! You must include a constructor public Set(int capacity) that initializes the array to the specified initial capacity, and a constructor public Set() that uses a reasonable default initial capacity. Finally, you should provide a constructor public Set(E[] entries) that initializes the set with the provided entries. Note that this constructor should still create its own backing array, and not adopt the argument as a data member; it must also skip all duplicates and null values in the provided array. Whenever the capacity is reached, the array should resize, using the techniques discussed in lecture (i.e., you should never throw SetFullException). Method Set() Set(int) Set(E[]) int getCurrentSize() boolean isEmpty() boolean add(E) boolean remove(E) E remove() void clear() boolean contains(E) E[] toArray() 2 3.2 Implement Profile, 50 points Develop the Profile class, an implementation of the ADT described in ProfileInterface. Include this class in package.a1. Read the interface carefully (including comments) to ensure you implement it properly. As with Set, it will be graded using a client that expects the functionality described in its interface. The Profile class should be a client of the Set data structure. Use composition with your Set<E> class to store the followees (the profiles that this profile is following) as a data member of type Set<Profile> or Set<ProfileInterface>. You must include a constructor public Profile() that initializes the name and “about me” blurb to be empty strings, and a constructor public Profile(String name, String about) that initializes these data members with the specified values. In the latter, you must check for null values for both, and replace any null value with an empty string. Method Profile() Profile(String, String) void setName(String) String getName() void setAbout(String) String getAbout() boolean follow(ProfileInterface) boolean unfollow(ProfileInterface) ProfileInterface[] following(int) ProfileInterface recommend() 3.3 Testing SocialClient is provided as an example client of the Profile and Set classes. It does not exhaustively test the functionality of these classes. You are responsible for ensuring your implementations work properly in all cases, even those not tested by SocialClient, and follow the ADTs described in the provided interfaces. Thus, it is highly recommended that you write additional test client code to test all of the corner cases described in the interfaces. For help getting started, re-read the section of the textbook starting at Chapter 2.16. Note: For functionality that cannot be tested (e.g., methods that crash, cannot be compiled), up to 1/2 points will be awarded by inspection. At this level, turning in code that crashes or does not compile is not acceptable and will not yield success.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.NoSuchElementException;
import java.util.Scanner;

* SocialClient is a toy application for simulating the connections between profiles in an
* asymmetric (i.e., following) social network. It is a client of the Set and Profile classes.
public class SocialClient {
    // a set to store the profiles in the system
    private static SetInterface<ProfileInterface> profiles = new Set<ProfileInterface>();
    // a scanner to get input from the user
    private static Scanner input = new Scanner(;

    * Runs the toy social media application.
    * Attempts to restore data from previous runs, if possible.
    public static void main(String args[]) {
       int selection = -1;

       while (selection != 0) {
            System.out.println("Social Client Main Menu");
            System.out.println("1. List profiles");
            System.out.println("2. Create a profile");
            System.out.println("3. Show a profile");
            System.out.println("4. Edit a profile");
            System.out.println("5. Follow");
            System.out.println("6. Unfollow");
            System.out.println("7. Recommend someone to follow");
            System.out.println("0. Quit");
            System.out.print("Selection: ");

            try {
                selection = input.nextInt();
            } catch (NoSuchElementException e) {
                selection = -1;
            } catch (IllegalStateException e) {
                selection = -1;

            switch (selection) {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 0:
                   // Invalid, just ignore and let loop again

    * Attempts to restore the program state from SocialClientData.bin
static void restore() {
try {
ObjectInputStream restoreStream =
                   new ObjectInputStream(new FileInputStream("SocialClientData.bin"));
profiles = (SetInterface<ProfileInterface>)restoreStream.readObject();
catch(FileNotFoundException e) {
// File does not exist, no warning
catch(IOException e) {
// Couldn't read file properly
            System.out.println("Warning: Could not restore from existing data");
catch(ClassNotFoundException e) {
// Couldn't cast to correct type
            System.out.println("Warning: Could not restore from existing data");

    * Attempts to save the program s...

By purchasing this solution you'll be able to access the following files:

50% discount

$50.00 $25.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats