Skip to content

Instantly share code, notes, and snippets.

@guyhughes
Created February 18, 2016 18:36
Show Gist options
  • Save guyhughes/a6bbe1ebd24e02af5022 to your computer and use it in GitHub Desktop.
Save guyhughes/a6bbe1ebd24e02af5022 to your computer and use it in GitHub Desktop.
ITI1121 2013 exam question 1, code implementation
interface Likeable {
void like();
int getLikes();
}
.PHONY: tags default
default:
javac *.java
java Test
tags:
ctags -f .tags -R .
public class NewsFeed {
// privates
//--------------------------------------------------------------------------------
/**
* The number of Posts in this.posts.
*/
private int n = 0;
// privates to be initialized by constructor
//--------------------------------------------------------------------------------
private Post[] posts;
private int increment;
public NewsFeed(int capacity, int increment){
this.posts = new Post[capacity];
if (increment < 1){
System.err.println("Invalid increment passed to NewsFeed(..) constructor.");
System.exit(1);
}
this.increment = increment;
}
public int size() {
return this.n;
}
public Post get(int i){
if ( i >= this.n ){
System.err.printf("Post index %d is out of bounds of integers on [0,%d)\n",i,n);
System.exit(1);
}
return this.posts[i];
}
public void add(final Post post){
if (this.posts.length <= this.n){
// Here we must resize the array.
Post[] newarray = new Post[this.posts.length+this.increment];
System.arraycopy(posts, 0, newarray, 0, n);
this.posts = newarray;
}
// Here we add the post the array
this.posts[n++] = post;
}
}
public class PhotoPost extends Post {
protected String filename, caption;
public PhotoPost(String username, String filename, String caption){
super(username);
this.filename=filename;
this.caption=caption;
}
public String toString(){
return String.format(super.toString()+", %s, %s","PhotoPost",this.filename,this.caption);
}
}
import java.util.Date;
public abstract class Post implements Likeable {
protected int likes = 0;
protected String username;
protected Date timestamp;
public Post(String username) {
this.username = username;
this.timestamp = java.util.Calendar.getInstance().getTime();
}
public void like() {
++this.likes;
}
public int getLikes(){
return this.likes;
}
public String toString(){
return String.format("%%s :\t%s, %s, likes = %d",timestamp.toString(),username,likes);
}
}
public class Test {
public static void main(String[] args){
NewsFeed messages;
Post msg1, msg2;
messages = new NewsFeed(100,10);
msg1 = new PhotoPost("David","funny.png", "Birthday party");
msg1.like();
messages.add(msg1);
msg2 = new TextPost("David", "Dinner at your place with Alexe");
msg2.like();
msg2.like();
messages.add(msg2);
messages.add(new TextPost("Nancy","Okay"));
for (int i=0; i < messages.size(); ++i){
System.out.println(messages.get(i));
}
}
}
public class TextPost extends Post {
protected String message;
public TextPost(String username, String message){
super(username);
this.message=message;
}
public String toString(){
return String.format(super.toString()+", %s","TextPost",this.message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment