Objectives

Social Network app · Inheritance · Polymorphism · Overriding

Network-V3

In this step, you will create version 3 of the Network system. This version will have a menu that will control the NewsFeed class.

Creating Network V3 Project

In IntelliJ, create a new project called Network-V3.

Copy the following classes into this new project (note they have the System.out.print statements removed from them and instead have returned the Strings).

Create a new class, called Driver. This class will contain the main() method. In this class:

  • write a private method, called mainMenu() that displays the following menu and returns the user's input.

Figure 1: Menu System for NetworkV3

  • write a private method, called run(). This method should call the mainMenu() method and use a switch statment to process the user's input. Hint: write a private method for each case i.e. one that creates a MessagePost, one that creates a PhotoPost and then one that adds to the posts collection.

  • The run() method should be called from the main method.

Testing V3

Test the program by doing the following:

  • Add 1 messagePost.
  • Add 1 photoPost.
  • Add another 1 messagePost.
  • Add another 1 photoPost.
  • List all posts.

Are the posts printed in the order we entered them?

Are all the details that we entered, displayed when we "List all posts"? Why do you think details are missing? Hint: Investigate the code in Post, PhotoPost and MessagePost that displays the post. Note: we will fix this is the next step.

Solution

The solution to network-v3 is here.

Network-V4

In this step, you will build on v3 that you wrote in the previous step.

Creating the Network-V4 project

Create a new project called Network-V4 and copy the src files from either your solution to network-v3 or our solution of Network-V3 into it.

In v3, details that we entered via the menu were NOT displayed back to us when we listed all posts.

The data is missing because the display method was written in the superclass, Post. This superclass has no knowledge of its subclasses. Therefore, it has no knowledge of the message field in MessagePost, or the filename and caption fields in the PhotoPost class.

To fix this issue, you need to also write a display method in the subclasses, MessagePost and PhotoPost. This display method should:

  • override the super class method, display.
  • contain a call to the super class method, display.
  • append the sub class specific fields to the String returned from the super class call to the display method.

Test your code again. Are all details printed to the console?

Solution

The solution is available here

Exercises

These exercises are based on the exercises in Chapter 8 of Objects First with Java - A Practical Introduction Using Bluej, by David Barnes & Michael Kolling).

Exercise 1

Order the following items into an inheritance hierarchy :

  • Apple

  • IceCream

  • Bread

  • Fruit

  • FoodItem

  • Cereal

  • Dessert

  • ChocolateMousse

  • Baguette

Exercise 2

Assume that we have four classes :

  • Person

  • Teacher

  • Student

  • PhDStudent

Teacher and Student are both subclasses of Person. PhDStudent is a subclass of Student.

  • Which of the following assignments are legal and why or why not:
Person p1 = new Student(); 
Person p2 = new PhDStudent (); 
PhDStudent phd1 = new Student(); 
Teacher t1 = new Person(); 
Student s1 = new PhDStudent ();

Exercise 3

Given the Person hierarchy from Exercise 2, suppose that we have the following legal declarations and assignments:

Person p1 = new Person(); 
Person p2 = new Person (); 
PhDStudent phd1 = new PhDStudent(); 
Teacher t1 = new Teacher(); 
Student s1 = new Student ();
  • Based on those just mentioned, which of the following assignments are legal and why or why not?
s1 = p1; 
s1 = p2; 
p1 = s1; 
t1 = s1; 
s1 = phd1; 
phd1 = s1;

Solutions

Exercise 1 Solution

The question was...order the following items into an inheritance hierarchy :

  • apple

  • ice-cream

  • bread

  • fruit

  • food-item

  • cereal

  • dessert

  • chocolate mousse

  • baguette

The answer is:

FoodItem Hierarchy

Exercise 2

In the Person hierarchy, Teacher and Student are both subclasses of Person. PhDStudent is a subclass of Student.

Person Hierarchy

Person p1 = new Student();  // This is legal because a Student "is a" Person.  
                            // We are following the direction of the arrows in the 
                            // inheritance diagram.
Person p2 = new PhDStudent (); // This is legal because a PhDStudent "is a" Person.
                                 // We are following the direction of the arrows in the 
                                 // inheritance diagram.
PhDStudent phd1 = new Student(); // This is NOT legal because a Student "may not be" 
                                 // a PhDStudent.  We are going against the direction  
                                 // of the arrows in the inheritance diagram.
Teacher t1 = new Person();    // This is NOT legal because a Person "may not be" 
                              // a Teacher.  For example, the Person could be a Student.  
                              // We are going against the arrows in the 
                              // inheritance diagram.
Student s1 = new PhDStudent();  //This is legal because a PhDStudent "is a" Student.
                                // We are following the arrows in the inheritance diagram.

Exercise 3

Given the Person hierarchy from Exercise 2:

Person Hierarchy

...suppose that we have the following legal declarations and assignments:

Person p1 = new Person(); 
Person p2 = new Person (); 
PhDStudent phd1 = new PhDStudent(); 
Teacher t1 = new Teacher(); 
Student s1 = new Student ();

Based on these legal variable declarations, which of the following assignments are legal and why or why not?

s1 = p1;    // This is NOT legal...a Person "may not be" a Student. 
            // We are going against the arrows in the inheritance diagram.
s1 = p2;    // Again, this is NOT legal...a Person "may not be" a Student. 
            // We are going against the arrows in the inheritance diagram.
p1 = s1;   // This is legal...a Student "is a" Person. 
             // We are following the direction of the arrows in the inheritance diagram.
t1 = s1;   // This is NOT legal...there is no relationship between a Teacher and a 
           // Student in our inheritance diagram.
s1 = phd1; // This is legal...a PhDStudent "is a" Student. 
             // We are following the direction of the arrows in the inheritance diagram.
phd1 = s1;   // However, this is NOT legal...a Student "may not be" a PhDStudent. 
               // We are going against the direction of the arrows in the 
               // inheritance diagram.