AP Computer Science A Project: Implementing a Basic Social Media Platform in Java

Java CS A projects

SHARE WITH FRIENDS >

Overview

In this project, we’ll tackle a more complex application that aligns with the concepts taught in the AP Computer Science A course. Students will create a basic version of a social media platform, using Java, that allows users to create accounts, post messages, and follow other users. This project will encompass a broad range of Computer Science concepts, including Object-Oriented Programming (OOP), data structures, and algorithms.

Concepts Covered:

  • Object-Oriented Programming: Utilizing classes and objects to represent users and posts.
  • Data Structures: Employing arrays or ArrayLists to manage collections of users and posts.
  • Control Structures: Implementing loops and conditionals to navigate through posts and users.
  • Algorithms: Developing simple algorithms for functionalities like searching and sorting.

Setting Up on Replit

  1. Create a New Repl: Log in to Replit and start a new Java Repl. Name it “BasicSocialMediaPlatform”.
  2. Environment Setup: Ensure the JDK is properly configured in Replit for running Java applications.

Project Structure

The project will consist of two main classes: User and Post, and a Main class to drive the application.

The User Class

This class represents a social media user.

import java.util.ArrayList;

public class User {
    private String username;
    private ArrayList<Post> posts;
    private ArrayList<User> following;

    public User(String username) {
        this.username = username;
        this.posts = new ArrayList<>();
        this.following = new ArrayList<>();
    }

    // Post a message
    public void postMessage(String message) {
        this.posts.add(new Post(message));
    }

    // Follow another user
    public void follow(User user) {
        if (!this.following.contains(user)) {
            this.following.add(user);
        }
    }

    // Display user's posts
    public void displayPosts() {
        for (Post post : posts) {
            System.out.println(post.getMessage());
        }
    }

    // Getters
    public String getUsername() {
        return username;
    }

    public ArrayList<User> getFollowing() {
        return new ArrayList<>(following);
    }
}

The Post Class

This class represents a post by a user.

public class Post {
    private String message;

    public Post(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}

The Main Class

This class is the entry point of the application, where users can create accounts, post messages, and follow others.

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    private static ArrayList<User> users = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        // Main loop for user input
        while (true) {
            System.out.println("1. Create User  2. Post Message  3. Follow User  4. Display Posts  5. Exit");
            int choice = scanner.nextInt();
            scanner.nextLine();  // Consume newline

            switch (choice) {
                case 1:
                    createUser();
                    break;
                case 2:
                    postMessage();
                    break;
                case 3:
                    followUser();
                    break;
                case 4:
                    displayPosts();
                    break;
                case 5:
                    System.exit(0);
                default:
                    System.out.println("Invalid choice.");
            }
        }
    }

    // Implement the methods for createUser(), postMessage(), followUser(), and displayPosts()
}

Implementing Functionalities

Within the Main class, you need to implement the methods createUser(), postMessage(), followUser(), and displayPosts() to handle the respective functionalities based on user input. These methods will interact with User and Post objects and perform actions like adding users to the system, creating new posts, managing followers, and displaying posts.

Conclusion

This project provides a practical application of key computer science concepts taught in the AP Computer Science A course. Through this hands-on project, students will deepen their understanding of Object-Oriented Programming, data structures, control structures, and basic algorithms. They’ll also gain valuable experience in problem-solving and application design. Students are encouraged to further enhance the platform by adding new features such as liking posts, commenting, or implementing a simple feed algorithm to display posts from followed users.

SHARE WITH FRIENDS >

virtual pet scratch coding

Hackathon Projects

8 May 2024

My Virtual Pet

flappy football game

Hackathon Projects

8 May 2024

Flappy Football

Animate a Character

Hackathon Projects

8 May 2024

Animate a Character Level 2

Animate a Character

Hackathon Projects

8 May 2024

Animate a Character

two player pong

Hackathon Projects

8 May 2024

Pong Two Player Game

Pong game

Hackathon Projects

8 May 2024

Pong Game

Scratch Platformer

Hackathon Projects

8 May 2024

Platformer Game

catch game

Hackathon Projects

8 May 2024

Pumpkin Catch Game