Practical Exercise 8 using BlueJ

Note that this exercise is based on Barnes and Koelling, Objects First, Chapter 8.

Before starting these exercises copy (right click then save as) projects.zip to your home directory (e.g.Z:\My Documents) and unzip it.

Dome1

  1. Open dome-v1. Create a database object. Create some videos and CDs. Enter them into tthe database. List the database contents.
  2. Create a CD object. Notice that it has no comment. Add a comment. List the database. Is the comment displayed? Explain the behaviour.

Dome2

  1. Open dome-v2. Note the class diagram. Remove the "extends Item" phrase. Not the changes in the class diagram.
  2. Create a CD object. Call some of its methods. Can you call the inherited methods (e.g. setCOMMENT)? What do you observe about the inherited methods?

Inheritance

  1. Order the following items into an inheritance hierarchy: apple, ice-cream, bread, fruit, food, cereal, orange, dessert, baguette, banana, croissant.
  2. What is the inheritance relationship between rectangle and square?
  3. Assume we have four classes: Person, Teacher, Student and PhDstudent. Teacher and Student are both subclasses of Person. PhDstudent is a subclass of Student

    Which of the following assignments are legal and why?

    Person p1 = new Student();
    Person p2 = new PhDStudent();
    Teacher t1 = new Person();
    Student s1 = new PhDstudent();
    
    s1 = p1;
    s1 = p2;
    p1 = s1;
    t1 = s1;
    s1 = phd1;
    phd1 = s1;
    
  4. Test your answers to the previous question by creating the classes mentioned and trying them out in BlueJ.