Objectives

On completion of this lab you should be able to:

  • use Interfaces when defining collections.

  • write your own bespoke Interface and implement it.

Using Pre-defined Interfaces

In this practical, you will use the Collections Framework Interfaces in the Network app.

Creating Network-V6

Using the List Interface

  • In this section, you may need to refer to your lecture slides for support (i.e. the fourth slide deck).

  • In Post.java:

    • define the comments ArrayList to be a List.

    • create a getter and setter for the comments field.

  • In NewsFeed.java:

    • define the posts ArrayList to be a List.
  • Test your code to make sure your refactoring did not break anything.

Implementing INewsFeed

In this practical, you will create the INewsFeed interface and implement it in NewsFeed. In this section, you may need to refer to your lecture slides for support (i.e. the fourth slide deck).

Creating the New Project

  • In Eclipse, right click on your Network-V6 project and paste it as Network-V7. Note: the solution to V6 is in the next step if you need it.

Creating INewsFeed.java

  • Within the src folder, create a new Interface called INewsFeed.java.

  • Add the following methods into the Interface:

    void addPost(Post post);
    void deletePost(int index);
    String show();

Updating NewsFeed.java

  • Implement the INewsFeed interface in the NewsFeed class.

  • You will notice that implementations have already been provided for two of the abstract methods:

    • void addPost(Post post);
    • String show();
  • You need to provide an implementation for this method:

    • void deletePost(int index);
  • The implementation should remove the object stored in the posts collection at the specified index.

  • In the implementation of the show method, add the index number of the post to the output string.

Updating Driver.java

  • Add an option 4 to the menu.

  • When this option is selected, a new method called deletePost() should be called. This method should:

    • display all the stored posts

    • ask the user to enter the index number of the post they wish to delete

    • delete the post at the specified index

  • Note: don't worry about robustness in this lab e.g. exception handling on the input, verifying that the index is valid, etc. The purpose of this lab is just to introduce you to Interfaces in the simplest form possible.

  • Test the delete aspect of your code; is all working as expected?

Solutions

The solution for Network-V6 can be found here .

The solution for Network-V7 can be found here .