The objectives of this lab are to implement both inheritance and polymorphism in the Network system.
In Eclipse, create a new project called Network-V1
Copy the following classes into the project:
Copy the following class into the project:
This class tests the three classes by performing the following actions:
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.
Before moving onto v2, ensure that you:
understand the code in v1.
notice the code duplication and the problems that it could cause.
In this step, you will familiarise yourself with the second version of the Network example (with inheritance).
In Eclipse, create a new project called Network-V2
Copy the following classes into the project:
Copy the following class into the project:
This test class will repeat these steps that you did with v1:
Note the difference in the order (the details are now printed in the order they were entered).
In this step, you will create version 3 of the Network system. This version will have a menu that will control the NewsFeed class.
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.
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.
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.
In this step, you will build on v3 that you wrote in the previous step.
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:
Test your code again. Are all details printed to the console?
Order the following items into an inheritance hierarchy : - apple - ice-cream - bread - fruit - food-item - cereal - dessert - chocolate mousse - baguette
Assume that we have four classes : - Person - Teacher - Student - PhDStudent
Teacher and Student are both subclasses of Person. PhDStudent is a subclass of Student.
Person p1 = new Student();
Person p2 = new PhDStudent ();
PhDStudent phd1 = new Student();
Teacher t1 = new Person();
Student s1 = new PhDStudent ();
Person p1 = new Person();
Person p2 = new Person ();
PhDStudent phd1 = new PhDStudent();
Teacher t1 = new Teacher();
Student s1 = new Student ();
s1 = p1;
s1 = p2;
p1 = s1;
t1 = s1;
s1 - phd1;
phd1 = s1;