Objectives

The objectives of this lab are to implement both inheritance and polymorphism in the Network system.

Network-V1

  • In this step, you will familiarise yourself with the original version of the Network example (without inheritance).

Creating Network V1 Project

Testing the project

  • Copy the following class into the project:

  • This class tests the three classes by performing the following actions:

    • Create 2 MessagePost objects.
    • Create 2 PhotoPosts objects.
    • Create 1 NewsFeed object.
    • Add 1 PhotoPost object to the NewsFeed object.
    • Add 1 MessagePost object to the NewsFeed object.
    • Add another PhotoPost object to the NewsFeed object.
    • Add another MessagePost object to the NewsFeed object.
    • List all messagePost and photoPosts from the NewsFeed object.
  • Run this test class and interrogate the output.

  • Note the order of the posts...figure out why the order is different than the order of input.

Output from Test.java

Moving onto the next version

  • Before moving onto v2, ensure that you:

    • understand the code in v1.

    • notice the code duplication and the problems that it could cause.

Network-V2

In this step, you will familiarise yourself with the second version of the Network example (with inheritance).

Creating Network V2 Project

Testing the project

  • Copy the following class into the project:

  • This test class will repeat these steps that you did with v1:

    • Create 2 MessagePost objects.
    • Create 2 PhotoPosts objects.
    • Create 1 NewsFeed object.
    • Add 1 PhotoPost object to the NewsFeed object.
    • Add 1 MessagePost object to the NewsFeed object.
    • Add another PhotoPost object to the NewsFeed object.
    • Add another MessagePost object to the NewsFeed object.
    • List all messagePost and photoPosts from the NewsFeed object.
  • Note the difference in the order (the details are now printed in the order they were entered).

Output from Test.java

  • Note that all the details associated with each post are not displayed when we "List all posts"? Why do you think details are missing? Note: we will fix this later in the lab.

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.

Exercise

  • In Eclipse, 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: You will write a method in each case that creates a MessagePost or PhotoPost object and then adds to the posts collection.

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

  • 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? Note: we will fix this is the next step.

Network-V4

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

Exercise

  • Create a new project called Network-V4 and copy the solution of Network-V3 solution 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 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?

  • NetworkV4 solution

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 - ice-cream - bread - fruit - food-item - cereal - dessert - chocolate mousse - 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 ();
  • 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;