Question: 1- Likeable Implement the interface Likeable. It declares two methods: like() and int getLikes() . 2- Post Write the implementation of the class Post. It
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. Use java.util.Calendar.getInstance().getTime() to obtain a Date object representing the current time. A Date object has a method toString() that converts this date to a String
Date rightNow = Calendar.getInstance().getTime(); System.out.println(rightNow);
- 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 than 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.
Bonus
- Write the implementation of the class NewsFeed using a dynamic array (array that automatically changes size) to store Post messages, instead of a fixed size array.
- The constructor has two parameters, the initial capacity of the array and the capacity increment
- Each time the array is full, the implementation should create a new array larger by the capacity increment.
Skeleton Code Provided:
Post.java
import java.util.Calendar;
import java.util.Date;
public class Post implements Likeable, Comparable
protected int likes;
private Date timeStamp;
private String userName;
public Post(String userName) {
// Your code here
}
public String getUserName() {
return userName;
}
public Date getTimeStamp() {
return timeStamp;
}
// Implement the methods required by the interface Likeable.
// This file will not compile unless they are present with the correct name and signature.
public String toString() {
String str = new String();
str = getClass().getName() + ": " + timeStamp + ", " + userName + ", likes = " + likes;
return str;
}
public int compareTo(Post other){
// Your code here.
}
public boolean isPopular(){
// Your code here.
}
}
==========================================================
PhotoPost.java
public class PhotoPost extends Post {
private String fileName;
private String caption;
public PhotoPost(String userName, String fileName, String caption) {
// Your code here.
}
public String getCaption() {
return caption;
}
public String getFileName() {
return fileName;
}
public String toString() {
String str = new String();
str = super.toString() + ", " + fileName + ", " + caption;
return str;
}
}
=============================================================================================
TextPost.java
public class TextPost extends Post {
private String message;
public TextPost(String userName, String message) {
// Your code here.
}
public String getMessage() {
return message;
}
public String toString() {
String str = new String();
str = super.toString() + ", " + message;
return str;
}
public boolean isPopular() {
// Your code here.
}
}
===============================================================================================
NewsFeed.java
/* *
* Use static array for NewsFeed
* with constant MAX_SIZE
* */
public class NewsFeed {
private Post[] messages;
private int size;
public static final int MAX_SIZE = 25;
public NewsFeed() {
// Your code here.
}
public void add(Post message) {
// Your code here.
}
public Post get(int index) {
return messages[index];
}
public int size() {
return size;
}
public void sort(){
int i, j, argMin;
Post tmp;
for (i = 0; i < size - 1; i++) {
argMin = i;
for (j = i + 1; j < size(); j++) {
if (messages[j].compareTo(messages[argMin]) < 0) {
argMin = j;
}
}
tmp = messages[argMin];
messages[argMin] = messages[i];
messages[i] = tmp;
}
}
public NewsFeed getPhotoPost(){
// Your code here
}
public NewsFeed plus(NewsFeed other){
// Your code here
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
