Assignment #7 - Chapter 8: Inheritance and Interfaces
Due: Thursday, July 10
From chapter 8, do programming exercises
8.1 and 8.6 (page 334) -
Also note the extra instructions below.
Filenames should be
- Triangle.java
- Prob8_6.java
Note that these filenames all start with a capital letter. Please make
your filenames match mine exactly.
Extra Instructions
- For exercise 8.1, write the source code in a file named
Triangle.java. To compile this, you will need to use the
GeometricObject class from the chapter 8 examples. Do not
hand in the GeometricObject file -- you only need to hand in the
Triangle.java file.
- For exercise 8.6:
- Put all the source code into the file Prob8_6.java.
Note that this file will contain multiple classes.
- Instead of writing the test program described in the exercise, just
use the test program shown below - class Prob8_6 and the
main() method that are given here. You need to write the
interface and appropriate classes. You do not need to write
main(), since I have provided it
- 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.
The Prob8_6 class
public class Prob8_6
{
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)
{
((Eatable)object).howToEat();
}
System.out.println();
}
}
Sample run of Exercise 8.6
java.lang.Object@f4a24a
Animal
Tiger
Chicken (pluck then cook)
Elephant
Fruit (eat raw)
Apple (polish)
Orange (peel)
Compiling
Remember that the compile command is "javac", at the unix command prompt.
Compile your code on program.cs.fsu.edu, and then run your programs with
the "java" command.
Submitting:
Submit programs through the submission web page.