CodeBlanks Logo Icon CodeBlanks

Model design

Design the Post and Comment class skeleton.

Project: Blog app

In this five-lesson project you'll build a blog app. Each lesson adds one piece, and by the final lesson you'll complete a working mini command-line application.

You'll use what you learned in previous groups: classes, inheritance and List. First you'll design a Post class with title and author fields. Then you'll add a Comment class and link comments to posts.

The Comment class

Every blog post should accept comments. The Comment class is also a simple data holder like Post: it carries author and text fields. Both classes are written as POJOs (Plain Old Java Objects); fields are package-private and filled through a constructor.

For now we keep Comment separate from Post; the next lesson links them via a List<Comment>. Defining the two classes side by side is enough for now.

Practice Define the Post and Comment classes

Define class Post with String title and String author fields and a Post(String title, String author) constructor that assigns them. Define class Comment the same way with String author and String text fields and a Comment(String author, String text) constructor. In main, create a post with Post p = new Post("First post", "Ayşe"); and print its title with System.out.println(p.title);.

Starter code

Main.java
// Define the Post class: title, author fields and constructor

// Define the Comment class: author, text fields and constructor

// main: create a Post instance and print its title

Sample solution

Main.java
class Post {
    String title;
    String author;
    Post(String title, String author) {
        this.title = title;
        this.author = author;
    }
}

class Comment {
    String author;
    String text;
    Comment(String author, String text) {
        this.author = author;
        this.text = text;
    }
}

public class Main {
    public static void main(String[] args) {
        Post p = new Post("First post", "Ayşe");
        System.out.println(p.title);
    }
}

Expected output

First post

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the role of the Post class?

  • Read user input
  • Delete comments
  • Hold post data Correct answer
  • Write to a file

Explanation: Post holds the data of a blog entry such as title and author.

2 Complete the constructor

Question
class Post {
    String title;
    String author;
    Post(String title, String author) {
        this.title = title;
        this.____ = author;
    }
}
Answer
class Post {
    String title;
    String author;
    Post(String title, String author) {
        this.title = title;
        this.author = author;
    }
}

Explanation: this.author = author copies the parameter into the field.

3 Match the blog project concepts

Term Meaning
Post Holds post data
Comment Holds comment data
title Post title
author Post or comment writer

All lessons in Intermediate