ISF DP Computer Science

Posts #

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

  • BufferedReader(FileReader) methods .ready .readLine
  • Polymorphism - (e.g. overriding toString to print a Post object)
  • String methods .equals(String) .toUpperCase() .toLowerCase()

[0] Setup #

Clone the Repository #

💻 select [+ New Project] > [Repository URL] #

💻 Paste your URL to clone the lab

Be sure to change yourgithubusername to your actual GitHub username.

https://github.com/isf-dp-cs/lab_posts_yourgithubusername

[1] Post #

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:

Post post1 = new Post("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.

Post post1 = new Post("Check out this cool picture!", "#coolpicture", false, 0);
System.out.println(post1);

--------------------------------------------------
| Check out this cool picture! |
--------------------------------------------------
| 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.

@Override
public String toString()

[a] Find Sample Posts #

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.


[c] Update Visibility #

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:

Post

  • constructor
  • accessor
  • mutator
  • toString

Main

  • readPostsFromFile()

posts_database.txt


[2] Profile #

The Profile creates profiles, which each contain an array of many Post objects.

classDiagram class Profile { - username: String - firstName: String - age: int - myPosts: Post[] + Profile(String username, String firstName, int age) + sortPosts() void + accessors and mutator methods () }

It is your job to finish the Profile class according to the specifications.


[a] Attributes #

💻 Construct code to declare the attributes of the Profile class


[b] Constructor #

💻 Construct code for the constructor of class Profile.

💻 Be sure to test out that Profile works before moving on!.


[c.1] Profile in Main #

💻 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().


[c.2] Sort Posts #

💻 In Profile, construct the code for the method sortPosts that will sort the array myPosts in descending order of number of likes.

After you’re finished, make sure to test it on Rex in Main!


[3] Deliverables #

⚡✨

Push to Github #

💻 Select Commit from the menu on the left.

Select all your updated files. Be sure to include a descriptive commit message.

💻 Click Commit and Push

💻 Click Push