Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Within a forum post, many components of data are required to represent the post.

ID: 3560869 • Letter: W

Question

Within a forum post, many components of data are required to represent the post. The ones we are concerned about are 1) the user who posted it, 2) when it was posted, 3) under what topic was it posted to, 4) the rating of the post, 5) the message itself.

A.Create a class, Post, that contains and handles the specified fields. You will need a default constructor, accessors and mutators, toString, upVote, downVote. Some fields can only be changed once, make sure to handle that. Use proper naming in all cases. [20%]

B.Add a main to class Post that allows us to test behavior, specifically, read in a set of input data and output the Posts. Assume that the first input to the program tells us how many Posts we are reading. Store these in an array, not an ArrayList. Do not prompt the user for the input in this portion. [20%]

Format: <username> <topic> <rating> <when> <message>

Explanation / Answer

This is going to be long post, Please bear with me. I have not attempted B part, as it seems very ambigous to me. But I have attempted C, which actually took a lot of time. I have tried to put everything in a single class - Post.class . This will be easy to run.

There are very little comments, as the naming of methods explains every thing. I have done testing from my side. After the class code, I am also printing some test cases from my side. There are lot more functionalities than the testing I have provided. Kindly test from your side also.

I have done very less testing as it is getting late in this side of the world. Feel free to revert back if you need more.

Post.java:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;

public class Post {
    private String user;
    private Date createdTime;
    private String topic;
    private float rating; //rating being upvotes - downvotes
    private String message;
    private int id; // randomly generated id

    private int upVotes;
    private int downVotes;
    private int totalVotes;

    public Post() {
        createdTime = new Date(); // this is equivalent to System.currentTimeMills
        upVotes = 1; //Assuming the user will up vote his post while creating it.
        downVotes = 0;
        rating = 1;
        id = new Random().nextInt(100); // Assigns a random number in range 0..99
    }

    // Accessors & Mutators
    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public float getRating() {
        return rating;
    }

    public void setRating(float rating) {
        this.rating = rating;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        DateFormat dateFormat = new SimpleDateFormat();
        return "{" +
                "id='" + id + ''' +
                "user='" + user + ''' +
                ", createdTime=" + dateFormat.format(createdTime) +
                ", topic='" + topic + ''' +
                ", rating=" + rating +
                ", message='" + message + ''' +
                '}';
    }

    public void upVote(){
        upVotes++;
        totalVotes++;
        rating = upVotes - downVotes;
    }

    public void downVote(){
        downVotes++;
        totalVotes++;
        rating = upVotes - downVotes;
    }

    public static void main(String[] args) {
        printUsage();
        int totalNumberOfPosts = 0;
        Post [] posts = new Post[totalNumberOfPosts];
        Scanner scanner = new Scanner(System.in).useDelimiter(" ");

        while(true){
            System.out.print("Command:>");
            String command = scanner.next();
            if(command.equals("create")){
                posts = createPost(scanner,posts,totalNumberOfPosts);
                totalNumberOfPosts++;
            }
            else if(command.equals("quit"))
                System.exit(-1);
            else if (command.equals("edit")){
                posts = editPost(scanner,posts,totalNumberOfPosts);
            }
            else if(command.equals("delete")){
                posts = deletePost(scanner,posts,totalNumberOfPosts);
                totalNumberOfPosts--;
            } else if (command.equals("upVote")){
                posts = upVotePost(scanner, posts,totalNumberOfPosts);
            } else if (command.equals("downVote")){
                posts = downVotePost(scanner,posts,totalNumberOfPosts);
            } else if (command.equals("print")){
                printAllPosts(posts,totalNumberOfPosts);
            } else printUsage();

        }

    }

    private static void printAllPosts(Post [] posts,int totalNumberOfPosts) {
        for(int i=0 ; i<totalNumberOfPosts;i++){
            System.out.println(posts[i].toString());
        }
    }

    private static Post [] downVotePost(Scanner scanner, Post[] posts, int totalNumberOfPosts) {
        System.out.println("Enter id: ");
        int id = Integer.parseInt(scanner.next());
        int actualIndex = getActualIndexForId(posts, id, totalNumberOfPosts);
        if (actualIndex == -1) {
            System.out.println("The given id does not exist");
            return posts;
        }
        posts[actualIndex].downVote();
        return posts;
    }

    private static Post [] upVotePost(Scanner scanner, Post[] posts, int totalNumberOfPosts) {
        System.out.println("Enter id: ");
        int id = Integer.parseInt(scanner.next());
        int actualIndex = getActualIndexForId(posts, id, totalNumberOfPosts);
        if (actualIndex == -1) {
            System.out.println("The given id does not exist");
            return posts;
        }
        posts[actualIndex].upVote();
        return posts;
    }

    private static Post [] deletePost(Scanner scanner, Post[] posts, int totalNumberOfPosts) {
        System.out.println("Enter id: ");
        int id = Integer.parseInt(scanner.next());
        int actualIndex = getActualIndexForId(posts, id, totalNumberOfPosts);
        if (actualIndex == -1) {
            System.out.println("The given id does not exist");
            return posts;
        }
        Post[] tempArray = new Post[posts.length-1];
        System.arraycopy(posts,0,tempArray,0,actualIndex);
        System.arraycopy(posts,actualIndex+1,tempArray,actualIndex,posts.length-actualIndex-1);
        posts=tempArray;
        return posts;
    }

    private static Post [] editPost(Scanner scanner, Post[] posts, int totalNumberOfPosts) {
        System.out.println("Enter id: ");
        int id = Integer.parseInt(scanner.next());
        int actualIndex = getActualIndexForId(posts, id,totalNumberOfPosts);
        if (actualIndex == -1) {
            System.out.println("The given id does not exist");
            return posts;
        }
        Post post = posts[actualIndex];
        System.out.println("Edit username: ");
        String username = scanner.next();
        System.out.println("Edit topic: ");
        String topic = scanner.next();
        System.out.println("Edit message:");
        String message = scanner.next();
        post.setUser(username);
        post.setTopic(topic);
        post.setMessage(message);

        posts[actualIndex]= post;
        return posts;
    }

    private static int getActualIndexForId(Post[] posts, int id, int totalNumberOfPosts) {
        int actualIndex = -1;
        for (int i =0; i< totalNumberOfPosts; i++){
            if (posts[i].getId() == id)
                actualIndex = i;
        }
        return actualIndex;
    }

    private static Post [] createPost(Scanner scanner, Post[] posts, int totalNumberOfPosts) {
        Post post = new Post();
        System.out.println("Enter username: ");
        String username = scanner.next();
        System.out.println("Enter topic: ");
        String topic = scanner.next();
        System.out.println("Enter message:");
        String message = scanner.next();
        post.setUser(username);
        post.setTopic(topic);
        post.setMessage(message);

        return addPost(posts, post, totalNumberOfPosts);
    }

    private static Post [] addPost(Post[] posts, Post post, int totalNumberOfPosts) {
        if (totalNumberOfPosts >= posts.length){
            Post [] tempArray = new Post[totalNumberOfPosts+3];
            System.arraycopy(posts,0,tempArray,0,posts.length);
            posts=tempArray;
        }
        posts[totalNumberOfPosts]= post;
        return posts;

    }

    private static void printUsage() {
        System.out.println("Available Commands: "+
                "create Prompts you to create a post " +
                "edit Allows you to edit the post with specific id " +
                "delete Deletes the post that id " +
                "upVote Up votes post with that id " +
                "downVote Down votes post with that id " +
                "");
    }
}


Testing from my side:

Available Commands:
create   Prompts you to create a post
edit   Allows you to edit the post with specific id
delete   Deletes the post that id
upVote   Up votes post with that id
downVote   Down votes post with that id

Command:>create
Enter username:
adfa
aEnter topic:
dfasdf
aEnter message:
dfafdadsf gadas gasdg
Command:>print
{id='5'user='adfa', createdTime=8/27/14 3:48 AM, topic='adfasdf', rating=1.0, message='adfafdadsf gadas gasdg'}
Command:>edit
Enter id:
5
Edit username:
adfagd
Edit topic:
blah
Edit message:
adfa adfad adgasdga adasga
Command:>print
{id='5'user='adfagd', createdTime=8/27/14 3:48 AM, topic='blah', rating=1.0, message='adfa adfad adgasdga adasga'}
Command:>upvote
Available Commands:
create   Prompts you to create a post
edit   Allows you to edit the post with specific id
delete   Deletes the post that id
upVote   Up votes post with that id
downVote   Down votes post with that id

Command:>upVote
Enter id:
5
Command:>print
{id='5'user='adfagd', createdTime=8/27/14 3:48 AM, topic='blah', rating=2.0, message='adfa adfad adgasdga adasga'}
Command:>downVote
Enter id:
5
Command:>create
Enter username:
user2
Enter topic:
topic2
Enter message:
mesaage 2
Command:>print
{id='5'user='adfagd', createdTime=8/27/14 3:48 AM, topic='blah', rating=1.0, message='adfa adfad adgasdga adasga'}
{id='33'user='user2', createdTime=8/27/14 3:49 AM, topic='topic2', rating=1.0, message='mesaage 2'}
Command:>create
Enter username:
user3
Enter topic:
topic3
Enter message:
message 3
Command:>print
{id='5'user='adfagd', createdTime=8/27/14 3:48 AM, topic='blah', rating=1.0, message='adfa adfad adgasdga adasga'}
{id='33'user='user2', createdTime=8/27/14 3:49 AM, topic='topic2', rating=1.0, message='mesaage 2'}
{id='77'user='user3', createdTime=8/27/14 3:49 AM, topic='topic3', rating=1.0, message='message 3'}
Command:>delete
Enter id:
5
Command:>print
{id='33'user='user2', createdTime=8/27/14 3:49 AM, topic='topic2', rating=1.0, message='mesaage 2'}
{id='77'user='user3', createdTime=8/27/14 3:49 AM, topic='topic3', rating=1.0, message='message 3'}
Command:>quit

EDIT:

This is the answer for B part. This will accept input from the user and print the posts array in the end.

Post.java:

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;

public class Post {
    private String user;
    private Date createdTime;
    private String topic;
    private float rating; //rating being upvotes - downvotes
    private String message;
    private int id; // randomly generated id

    private int upVotes;
    private int downVotes;
    private int totalVotes;

    public Post() {
        createdTime = new Date(); // this is equivalent to System.currentTimeMills
        upVotes = 1; //Assuming the user will up vote his post while creating it.
        downVotes = 0;
        rating = 1;
        id = new Random().nextInt(100); // Assigns a random number in range 0..99
    }

    // Accessors & Mutators
    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public float getRating() {
        return rating;
    }

    public void setRating(float rating) {
        this.rating = rating;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        DateFormat dateFormat = new SimpleDateFormat();
        return "{" +
                "id='" + id + ''' +
                "user='" + user + ''' +
                ", createdTime=" + dateFormat.format(createdTime) +
                ", topic='" + topic + ''' +
                ", rating=" + rating +
                ", message='" + message + ''' +
                '}';
    }

    public void upVote(){
        upVotes++;
        totalVotes++;
        rating = upVotes - downVotes;
    }

    public void downVote(){
        downVotes++;
        totalVotes++;
        rating = upVotes - downVotes;
    }

    public static void main(String[] args) throws IOException {


        Scanner scanner = new Scanner(System.in);
        int numberOfPosts = scanner.nextInt();
        Post [] posts = new Post[numberOfPosts];
        for(int i = 0; i<numberOfPosts;i++){
            String username = scanner.next();
            String topic = scanner.next();
            float rating = scanner.nextFloat();
            String message = scanner.nextLine();
            Post post = new Post();
            post.setMessage(message);
            post.setUser(username);
            post.setTopic(topic);
            post.setRating(rating);
            posts[i] = post;
        }
        printAllPosts(posts,numberOfPosts);


    }

}

Testing from my side:

2
shravan topic1 2.0 This is message1
shravan topic2 3.0 This is message2
{id='30'user='shravan', createdTime=8/27/14 8:01 PM, topic='topic1', rating=2.0, message=' This is message1'}
{id='29'user='shravan', createdTime=8/27/14 8:01 PM, topic='topic2', rating=3.0, message=' This is message2'}

Feel free to revert.

Thanks,

Shravan B (bettisra1@gmail.com)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote