Assignment #7 - Inheritance and Interfaces

Due: Saturday, July 17

Do the following programming exercises:


Exercise 1

Write a class called Triangle that extends GeometricObject (Recall that this is one of the examples linked from the Inheritance notes set).

Exercise 2

Create an interface named Eatable as follows:
  public interface Eatable
  {
    public String howToEat();
  }
Every class of an eatable object must implement the Eatable interface. Create the following sets of classes: Override the toString method in each class to return the name of the class.

For each class that implements Eatable, the howToEat method needs to be defined to return the eating instructions (as a String).

You can use the following main program to test:

The EatingTest class

EatingTest.java
public class EatingTest
{
    public static void main(String[] args)
    {
        Object[] objects = 
            {  new Object(), new Animal(), new Tiger(), new Chicken(), 
               new Elephant(), new Fruit(), new Apple(), 
               new Orange()
            };

        for (int i = 0; i < objects.length; i++)
        {
            showObject(objects[i]);
        }
    }

    public static void showObject(Object object)
    {
        System.out.print(object);
        if (object instanceof Eatable)
        {
            System.out.print(((Eatable)object).howToEat());
        }
        System.out.println();
    }
}

Sample run of main program

 java.lang.Object@f4a24a
 Animal
 Tiger
 Chicken (pluck then cook)
 Elephant
 Fruit (eat raw)
 Apple (polish)
 Orange (peel)

Note: The toString() method for class Object (the base class version) returns a string consisting of the name of the class, the '@' symbol, and the hash code of the object (in hexadecimal format). Your output for the toString() method of the Object instance may not look exactly like mine.


Submitting

Pack up all your source code files with jar. This does not need to be a "runnable" jar file (so no manifest addition needed). (Jarfile is hw7.jar).

E-mail this jar file to me (myers@cs.fsu.edu) by the due date. Your e-mail subject should be HW7-SUBMIT. Include your name and section in the e-mail body. Please only submit ONCE, unless you make a mistake and need to correct it (before the due date) -- and minimize this. Only the last submission will be graded.