1 Likeable Implement the interface Likeable It declares two

1- Likeable

Implement the interface Likeable. It declares two methods: like() and int getLikes().

2- Post

Write the implementation of the class Post. It implements the characteristics that are common to its sub-classes, here TextPost and PhotoPost.

Post implements the interface Likeable.

All the Post messages have a user name, a time stamp (of type java.util.Date), as well as a count for the number of likes.

The value of the time stamp is automatically assigned when an object is created. Usejava.util.Calendar.getInstance().getTime() to obtain a Date object representing the current time. A Dateobject has a method toString() that converts this date to a String

Each call to the method like() increases the number of likes for this message.

Post implements the interface Comparable. This interface allows you to compare two Post according to specific criteria. In this case the criteria will be the date of the post. For more information, refer to the documentation.

Add the method isPopular. This method returns true if the post is considered popular (more the 100 likes), false otherwise.

Do not forget the method toString()!

3- PhotoPost

Implement the class PhotoPost. A PhotoPost is a specialized Post. It stores a file name and a caption. Override the method toString() by using the keyword \"super\" in your implementation.

4- TextPost

Implement the class TextPost. A TextPost is a specialized Post. It stores a text message (String). Override the method toString using the keyword “super” in your implementation. A TextPost is considered popular if the post gets more than 50 likes.

5- NewsFeed

Write the implementation of the class NewsFeed. A NewsFeed object stores Post messages

It uses a fixed size array of some constant size MAX_SIZE to store Post messages . For this implementation will only accept up to 25 Post messages.

It has a method for adding a Post message. The message is added after the last message added.

It has a method sort in which the Post are sorted from the oldest to the most recent.

It has a method for returning the message found at a given index, Post get(int index).

It has a method size that returns the number of messages currently stored.

It has a method getPhotoPost that returns a new object of type NewsFeed containing only the PhotoPost

It has an instance method plus that has one formal parameter of type NewsFeed. This method returns a new object of type NewsFeed that represents the combination of the two NewsFeed. The Post of the new NewsFeed have to be sorted from the oldest to the most recent one.

This UML diagrams give you an overview of this application. Follow the instructions below: Comparable Diagramme UML de l\'application NewFeed News Feed Post D Likeable TextPost PhotoPost Figure 1: UML diagram for the application NewFeed

Solution

Hi,

Please see below the java classes and interfaces. Please comment for any queries/feedbacks.

Thanks

Likeable.java
public interface Likeable {
   public void like() ;
   public int getLikes();

}

Post.java

import java.util.Calendar;
import java.util.Date;


public class Post implements Likeable,Comparable<Post>{
  
   private String userName;
   private Date timeStamp;
   private int noOfLikes;

   public Post() {
       //The value of the time stamp is automatically assigned when an object is created.
       this.timeStamp = Calendar.getInstance().getTime();
   }

   public String getUserName() {
       return userName;
   }

   public void setUserName(String userName) {
       this.userName = userName;
   }

   public Date getTimeStamp() {
       return timeStamp;
   }

   public void setTimeStamp(Date timeStamp) {
       this.timeStamp = timeStamp;
   }

   public int getNoOfLikes() {
       return noOfLikes;
   }

   public void setNoOfLikes(int noOfLikes) {
       this.noOfLikes = noOfLikes;
   }

   public int compareTo(Post post) {
       return this.getTimeStamp().compareTo(post.getTimeStamp());
   }

   public void like() {
       this.noOfLikes =this.noOfLikes +1;
      
   }

   public int getLikes() {
       // TODO Auto-generated method stub
       return 0;
   }
  
   public boolean isPopular(){
       if(getNoOfLikes()>100){
           return true;
       }
       return false;
   }
  
   public String toString(){
       String s =\"Posted By : \"+this.getUserName() +\"Time : \"+this.getTimeStamp()+\" Likes : \"+this.getNoOfLikes() ;
       return s;
   }

}

PhotoPost.java


public class PhotoPost extends Post {
  
   private String fileName;
   private String caption;

  
  
   public String getFileName() {
       return fileName;
   }

   public void setFileName(String fileName) {
       this.fileName = fileName;
   }

   public String getCaption() {
       return caption;
   }

   public void setCaption(String caption) {
       this.caption = caption;
   }

   public String toString(){
       return super.toString();
   }

}

TextPost.java


public class TextPost extends Post {

   private String textMsg;

   public String getTextMsg() {
       return textMsg;
   }

   public void setTextMsg(String textMsg) {
       this.textMsg = textMsg;
   }
  
   public boolean isPopular(){
       if(getNoOfLikes()>50){
           return true;
       }
       return false;
   }
  
   public String toString(){
       return super.toString();
   }

}

NewsFeed.java

import java.util.Arrays;
import java.util.Collections;


public class NewsFeed {

   public static final int MAX_SIZE = 25;
  
   private Post[] postArray ;
   private int count ;
  
   public NewsFeed(){
      
       postArray = new Post[MAX_SIZE];
       count =0;
      
   }
   // method for adding a Post message. The message is added after the last message added.
   public void addPostMessage(Post post){
       postArray[count] = post;
       this.count++;
      
   }
   //method sort in which the Post are sorted from the oldest to the most recent.
   public void sortMessages(){
       Collections.sort( Arrays.asList(postArray));
   }
   //method for returning the message found at a given index
   public Post get(int index){
       return postArray[index];
   }
  
  
   //returns the number of messages currently stored
   public int size(){
      
       return this.count;
   }
   //method getPhotoPost that returns a new object of type NewsFeed containing only the PhotoPost
   public NewsFeed getPhotoPost(){
       NewsFeed newsFeedphot = new NewsFeed();
       Post [] postArrayTemp = new Post[MAX_SIZE];
       for(int i=0;i<postArray.length ;i++){
           if(postArray[i] instanceof PhotoPost){
               postArrayTemp[0] = postArray[i];
           }
       }
       newsFeedphot.setPostArray(postArrayTemp);
       return newsFeedphot;
   }
  
   //returns a new object of type NewsFeed that represents the combination of the two NewsFeed after sorting
   public NewsFeed plus (NewsFeed newsfeed){
       NewsFeed newsFeedphot = new NewsFeed();
       Post [] postArrayTemp = new Post[MAX_SIZE];
       for(int i=0;i<newsfeed.getPostArray().length ;i++){
           this.addPostMessage(newsfeed.getPostArray()[i]) ;
       }
       this.sortMessages();
       return newsFeedphot;
   }
  
   public Post[] getPostArray() {
       return postArray;
   }
   public void setPostArray(Post[] postArray) {
       this.postArray = postArray;
   }
   public int getCount() {
       return count;
   }
   public void setCount(int count) {
       this.count = count;
   }
   public static int getMaxSize() {
       return MAX_SIZE;
   }
  

}

1- Likeable Implement the interface Likeable. It declares two methods: like() and int getLikes(). 2- Post Write the implementation of the class Post. It impleme
1- Likeable Implement the interface Likeable. It declares two methods: like() and int getLikes(). 2- Post Write the implementation of the class Post. It impleme
1- Likeable Implement the interface Likeable. It declares two methods: like() and int getLikes(). 2- Post Write the implementation of the class Post. It impleme
1- Likeable Implement the interface Likeable. It declares two methods: like() and int getLikes(). 2- Post Write the implementation of the class Post. It impleme
1- Likeable Implement the interface Likeable. It declares two methods: like() and int getLikes(). 2- Post Write the implementation of the class Post. It impleme

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site