On completion of this lab you should be able to:
use Interfaces when defining collections.
write your own bespoke Interface and implement it.
In this practical, you will use the Collections Framework Interfaces in the Network app.
Create a new project called Network-V6 in Eclipse.
Copy the following classes into the project:
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:
Test your code to make sure your refactoring did not break anything.
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).
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();
Implement the INewsFeed interface in the NewsFeed class.
You will notice that implementations have already been provided for two of the abstract methods:
You need to provide an implementation for this method:
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.
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?