This lab is based after the November 24 mock exam. You will practice reviews these skills :
creating and using classes, objects, methods, attributes, parameters
referencing class objects inside other classes
avoiding null objects when looping through arrays
sorting
Concepts introduced in this lab. By the end of this lab you should be able to identify examples of each concept, and correctly use these terms, and use them in your code
The class Post has already been written for you. It is based off of the following UML.
classDiagram
class Post {
- textContent: String
- hashtag: String
- likes: int
- visibility: boolean
+ Post(String textContent, String hashtag, boolean visibility, int likes)
+ accessors and mutator methods ()
}
Polymorphism in toString()
Polymorphism is when we override the usual behavior of a method and define new behavior.
The usual behavior of toString() on any object would be to print out the memory location like this:
Postpost1=newPost("Check out this cool picture!","#coolpicture",false, 0);System.out.println(post1);-----------------model.Post@30f39991
This method of Post overrides toString(), so now it will print nicely to the console.
Postpost1=newPost("Check out this cool picture!","#coolpicture",false, 0);System.out.println(post1);--------------------------------------------------|Checkoutthiscoolpicture!|--------------------------------------------------|Hashtag:#coolpicture|--------------------------------------------------|Likes: 0 |--------------------------------------------------
In order to override a method, we need to make sure that we keep these the same:
Method name
Return type
Number and type of parameters
Additionally, we should include the @Override keyword above our child class method to indicate to the compiler that we want to override a method in the parent class.
In the Main class, there is a method readPostsFromFile() that reads all Post objects from a text file posts_database.txt into a large, unsorted array called allPosts.
A method is needed to show users a sample selection of posts from the platform.
This method should take the array allPosts as a parameter and select Post objects from allPosts so that every available hashtag is presented only once.
There are empty spaces at the end of the array allPosts.
You may assume that there are never more than 100 different hashtags on the platform (as identified by the variable hashtag).
💻 Construct the code for the method findSamplePosts() that will take the array allPosts as a parameter. It must return a Post array that contains one post for every hashtag that is used on the social media platform, without including any two Posts with the same hashtag.
The students want to know which topics are popular on their platform. Therefore, they want to view which hashtags were used in posts with the most likes. To be included, the hashtag must be on a publicly visible post that has at least 200 likes. Even if a hashtag is used on many popular posts, it should only be included once in the list of hashtags.
💻 Construct the code for the method findPopularHashtags() that will perform this query and return the results in the form of a String array.
The students now want to change the visibility feature. Instead of only two options for visibility (being viewable by friends or publicly viewable), posts will have a third option to be viewable only to the user themselves.
Because of this, visibility can no longer be a boolean. Decide which data type is more appropriate, and then make this change.
Note: you will need to make chages in may places, including:
💻 Import the Profile class into the Main class. Reference the way that Post was imported as an example.
💻 Construct a main method with code that creates an instance of a profile of a 16 year old named Rex whose chosen username is tyrannosaurus_rex.
Rex needs some posts on his profile. 💻 Create a new .txt file like posts_database.txt to create lots of posts for Rex, and read it in using readPostsFromFile().