Question

Define a toString method in Post and override it in EventPost. EventPost Class: /** * This...

Define a toString method in Post and override it in EventPost.

EventPost Class:

/**
* This class stores information about a post in a social network news feed.
* The main part of the post consists of events.
* Other data, such as author and type of event, are also stored.
*
* @author Matthieu Bourbeau
* @version (1.0) March 12, 2020
*/
public class EventPost extends Post
{
// instance variables - replace the example below with your own
private String eventType;

/**
* Constructor for objects of class EventPost
*/
public EventPost(String author, String typeOfEvent)
{
super(author);
this.eventType = typeOfEvent;
}

/**
* Sets the type of event.
*/
public void setEventType(String typeOfEvent)
{
this.eventType = typeOfEvent;
}
  
/**
* Returns the type of event.
*/
public String getEventType()
{
return eventType;
}
}

Post Class:

import java.util.ArrayList;

/**
* This class stores information about a news feed post in a
* social network. Posts can be stored and displayed. This class
* serves as a superclass for more specific post types.
*
* @author Michael Kölling and David J. Barnes
* @version 0.2
*/
public class Post
{
private String username; // username of the post's author
private long timestamp;
private int likes;
private ArrayList<String> comments;

/**
* Constructor for objects of class Post.
*
* @param author The username of the author of this post.
*/
public Post(String author)
{
username = author;
timestamp = System.currentTimeMillis();
likes = 0;
comments = new ArrayList<>();
}

/**
* Record one more 'Like' indication from a user.
*/
public void like()
{
likes++;
}

/**
* Record that a user has withdrawn his/her 'Like' vote.
*/
public void unlike()
{
if (likes > 0) {
likes--;
}
}

/**
* Add a comment to this post.
*
* @param text The new comment to add.
*/
public void addComment(String text)
{
comments.add(text);
}

/**
* Return the time of creation of this post.
*
* @return The post's creation time, as a system time value.
*/
public long getTimeStamp()
{
return timestamp;
}

/**
* Display the details of this post.
*
* (Currently: Print to the text terminal. This is simulating display
* in a web browser for now.)
*/
public void display()
{
System.out.println(username);
System.out.print(timeString(timestamp));
  
if(likes > 0) {
System.out.println(" - " + likes + " people like this.");
}
else {
System.out.println();
}
  
if(comments.isEmpty()) {
System.out.println(" No comments.");
}
else {
System.out.println(" " + comments.size() + " comment(s). Click here to view.");
}
}
  
/**
* Create a string describing a time point in the past in terms
* relative to current time, such as "30 seconds ago" or "7 minutes ago".
* Currently, only seconds and minutes are used for the string.
*
* @param time The time value to convert (in system milliseconds)
* @return A relative time string for the given time
*/
  
private String timeString(long time)
{
long current = System.currentTimeMillis();
long pastMillis = current - time; // time passed in milliseconds
long seconds = pastMillis/1000;
long minutes = seconds/60;
if(minutes > 0) {
return minutes + " minutes ago";
}
else {
return seconds + " seconds ago";
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

EventPost.java

/**

* This class stores information about a post in a social network news feed. The

* main part of the post consists of events. Other data, such as author and type

* of event, are also stored.

*

* @author Matthieu Bourbeau

* @version (1.0) March 12, 2020

*/

public class EventPost extends Post {

    // instance variables - replace the example below with your own

    private String eventType;

    /**

     * Constructor for objects of class EventPost

     */

    public EventPost(String author, String typeOfEvent) {

        super(author);

        this.eventType = typeOfEvent;

    }

    /**

     * Sets the type of event.

     */

    public void setEventType(String typeOfEvent) {

        this.eventType = typeOfEvent;

    }

    /**

     * Returns the type of event.

     */

    public String getEventType() {

        return eventType;

    }

    public String toString() {

        return "Username: " + getUsername() + "\nTime stamp: " + getTimeStamp() + "\n Likes: " + getLikes()

                + "\nComments: " + getComments() + "Event Type: " + this.eventType;

    }

}

Post.java

import java.util.ArrayList;

/**

* This class stores information about a news feed post in a social network.

* Posts can be stored and displayed. This class serves as a superclass for more

* specific post types.

*

* @author Michael Kölling and David J. Barnes

* @version 0.2

*/

public class Post {

    private String username;

    private long timestamp;

    private int likes;

    private ArrayList<String> comments;

    /**

     * Constructor for objects of class Post.

     *

     * @param author The username of the author of this post.

     */

    public Post(String author) {

        username = author;

        timestamp = System.currentTimeMillis();

        likes = 0;

        comments = new ArrayList<>();

    }

    /**

     * Record one more 'Like' indication from a user.

     */

    public void like() {

        likes++;

    }

    /**

     * Record that a user has withdrawn his/her 'Like' vote.

     */

    public void unlike() {

        if (likes > 0) {

            likes--;

        }

    }

    /**

     * Add a comment to this post.

     *

     * @param text The new comment to add.

     */

    public void addComment(String text) {

        comments.add(text);

    }

    /**

     * Return the time of creation of this post.

     *

     * @return The post's creation time, as a system time value.

     */

    public long getTimeStamp() {

        return timestamp;

    }

    /**

     * Display the details of this post.

     *

     * (Currently: Print to the text terminal. This is simulating display in a web

     * browser for now.)

     */

    public void display() {

        System.out.println(username);

        System.out.print(timeString(timestamp));

        if (likes > 0) {

            System.out.println(" - " + likes + " people like this.");

        } else {

            System.out.println();

        }

        if (comments.isEmpty()) {

            System.out.println(" No comments.");

        } else {

            System.out.println(" " + comments.size() + " comment(s). Click here to view.");

        }

    }

    /**

     * Create a string describing a time point in the past in terms relative to

     * current time, such as "30 seconds ago" or "7 minutes ago". Currently, only

     * seconds and minutes are used for the string.

     *

     * @param time The time value to convert (in system milliseconds)

     * @return A relative time string for the given time

     */

    private String timeString(long time) {

        long current = System.currentTimeMillis();

        long pastMillis = current - time; // time passed in milliseconds

        long seconds = pastMillis / 1000;

        long minutes = seconds / 60;

        if (minutes > 0) {

            return minutes + " minutes ago";

        } else {

            return seconds + " seconds ago";

        }

    }

    // getter

    public String getUsername() {

        return this.username;

    }

    public int getLikes() {

        return this.likes;

    }

    public ArrayList<String> getComments() {

        return this.comments;

    }

    // username of the post's author

    public String toString() {

        return "Username: " + this.username + "\nTime stamp: " + this.timestamp + "\n Likes: " + this.likes

                + "\nComments: " + this.comments;

    }

}

Please upvote if you like my answer and comment below if you have any queries.

Add a comment
Know the answer?
Add Answer to:
Define a toString method in Post and override it in EventPost. EventPost Class: /** * This...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Previous class code: 1) Employee.java ------------------------------------------------------------------------------- /** * */ package main.webapp; /** * @author sargade *...

    Previous class code: 1) Employee.java ------------------------------------------------------------------------------- /** * */ package main.webapp; /** * @author sargade * */ public class Employee { private int empId; private String empName; private Vehicle vehicle; public Double calculatePay() { return null; } /** * @return the empId */ public int getEmpId() { return empId; } /** * @param empId * the empId to set */ public void setEmpId(int empId) { this.empId = empId; } /** * @return the empName */ public String getEmpName() { return...

  • Given code: /** * Provides some methods to manipulate text * */ public class LoopyText {...

    Given code: /** * Provides some methods to manipulate text * */ public class LoopyText { private String text;    /** * Creates a LoopyText object with the given text * @param theText the text for this LoopyText */ public LoopyText(String theText) { text = theText; }    //Your methods here } Given tester code: /** * Tests the methods of LoopyText. * @author Kathleen O'Brien */ public class LoopyTextTester { public static void main(String[] args) { LoopyText loopy =...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • Modify the Auction class according to these exercises: 4.48: close method 4.49: getUnsold method 4.51: Rewrite getLot method 4.52: removeLot method For help watch Textbook Video Note 4.2 which sho...

    Modify the Auction class according to these exercises: 4.48: close method 4.49: getUnsold method 4.51: Rewrite getLot method 4.52: removeLot method For help watch Textbook Video Note 4.2 which shows you how to use and test the auction project, as well as solving the getUnsold exercise. Document with javadoc and use good style (Appendix J) as usual. Test your code thoroughly as you go. Create a jar file of your project. From BlueJ, choose Project->Create Jar File... Check the "Include...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • The method m() of class B overrides the m() method of class A, true or false?...

    The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...

  • Hi so I am currently working on a project for a java class and I am...

    Hi so I am currently working on a project for a java class and I am having trouble with a unit testing portion of the lab. This portion wants us to add three additional tests for three valid arguments and one test for an invalid argument. I tried coding it by myself but I am not well versed in unit testing so I am not sure if it is correct or if the invalid unit test will provide the desired...

  • COVERT TO PSEUDOCODE /****************************Vacation.java*****************************/ public abstract class Vacation {    /*    * private data field...

    COVERT TO PSEUDOCODE /****************************Vacation.java*****************************/ public abstract class Vacation {    /*    * private data field    */    private double cost;    private double budget;    private String destination;    /**    *    * @param cost    * @param budget    * @param destination    */    public Vacation(double cost, double budget, String destination) {        super();        this.cost = cost;        this.budget = budget;        this.destination = destination;    }    //getter and...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT