Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
CSE 8B: Introduction to Programming II
Programming Assignment 5
Objects and Classes
Due: Monday, February 24, 11:59 PM
Learning goals:
● Implement a simplified Reddit program using Java classes
● Use the Java ArrayList class
● Write unit tests for your classes
This assignment must be completed INDIVIDUALLY.
Coding Style [10 points]
For this programming assignment, we will be enforcing the CSE 8B and 11 Coding Style Guidelines. These guidelines can also be found on Canvas and the class website. Please ensure to have complete file headers, class headers and method headers. Avoid using magic numbers, use consistent indentation and keep your lines short.
Getting the Starter Code
1. You can download the starter code from Piazza → Resources → Homework → PA5.zip
2. After compiling Post.java and User.java, you should see the following in your terminal
$ javac Post.java
Post.java:65: error: cannot find symbol
P1.upvoteCount = 5;
^
symbol: variable upvoteCount
location: variable P1 of type Post
...
$ javac User.java
User.java:55: error: cannot find symbol
U1.karma = 10;
^
symbol: variable karma
location: variable U1 of type User
...
These compiler errors are expected, as the method unitTests is trying to reference things that have not yet been implemented in the starter code! Things should compile cleanly once you've added in the instance variables for each class.
You MUST NOT include any package statements in your code.
Overview
Reddit is a popular social news aggregation, content rating, and discussion website. Users submit content to the site such as links, text posts, images, and videos, which are then voted up or down by other members. In this programming assignment, we will create a simplified version of Reddit, where users can create, upvote, and downvote posts. To do so, you will be completing the implementation of two classes: Post.java and User.java
Part 1: Implementation [80 points]
Post.java
On Reddit, a post can exist as a stand-alone post in some community (subreddit) or as a comment on someone else's stand-alone post. In this write-up, we shall refer to the first type of post as an "original post" and the second type of post as a "comment". Both types of posts will be represented by the Post class (we'll use Post's instance variables to help us indicate whether a particular Post object is representing an original post or a comment)
In this part of the assignment, you will implement the Post class which represents the properties of a reddit post. The UML diagram for the Post class is:
To summarize, the Post class contains the following fields:
private String title
● The title of this Post. If this Post represents a comment, then title must be null. If this Post represents an original post, then title should be non-null.
private String content
● The content of this reddit Post.
private Post replyTo
● The Post object that this Post is replying to. If this Post is an original post, replyTo must be null. If this Post is a comment, then replyTo must be non-null.
private User author
● The author (represented as a User object) of this Post.
private int upvoteCount
● The number of upvotes that this Post has.
private int downvoteCount
● The number of downvotes that this Post has. and the following methods:
public Post(String title, String content, User author)
● The constructor for initializing an original post.
● This constructor must set the title, content, and author fields of this Post object with the values from the parameters (e.g., the title field must be assigned the contents of the title parameter).
● Set upvoteCount and downvoteCount to 0.
● Set replyTo to null.
public Post(String content, Post replyTo, User author)
● The constructor for initializing a comment.
● This constructor must set the content, replyTo, and author fields of this Post object with the values from the parameters (e.g., the content field must be assigned the contents of the content parameter).
● Set upvoteCount and downvoteCount to 0.
● Set title to null.
public String getTitle()
● Return the title of this Post.
public Post get ReplyTo()
● Return the Post object that this Post is replying to.
public User getAuthor()
● Return the author of this Post.
public int getUpvoteCount()
● Return the number of upvotes that this Post has.
public int getDownvoteCount()
● Return the number of downvotes that this Post has.
public void updateUpvoteCount(boolean is Increment)
● Increment the upvoteCount of this Post by 1 if is Increment is true. Otherwise, decrement upvoteCount by 1.
● You may assume that we will not call updateUpvoteCount in such a way that would result in a negative upvoteCount value in any of our tests.
public void updateDownvoteCount(boolean is Increment)
● Increment the downvoteCount of this Post by 1 if is Increment is true. Otherwise, decrement downvoteCount by 1.
● You may assume that we will not call updateDownvoteCount in such a way that would result in a negative downvoteCount value in any of our tests.
public ArrayList getThread()
● Return an ArrayList of Posts in the current thread, starting with the original post and ending with this Post.
● The original post should be the first item in the ArrayList, followed by one of its comments, followed by another one of its comments, and so on until this Post is reached.
● For example, suppose that P1 is an original post, P2 is a comment on P1, and P3 is a comment on P2. Then P3.getThread() must return [P1, P2, P3]. In other words, the "current thread" is the collection of posts that originate from this Post and "climbs up" through the replyTo fields to reach the original post.
○ If P1 were to have more than one comment, P3.getThread() must still return [P1, P2, P3].
● You may implement this method iteratively or recursively.
public String toString()
● Return a String representation of this Post.
● If this Post is an original post, its String representation must follow the format
[|]\t