Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised January 9, 2026)

Previous: Scenes

Coding Exercises: Inheritance and Interfaces

Vehicle

Suppose you are given a Vehicle interface:

public interface Vehicle
{
    public String name();
    public int wheelCount();
    public boolean isHumanPowered();
}

Write the following concrete classes implementing the Vehicle interface: Car, Motorcycle, Bicycle, Unicycle

Write a VehicleTest class to show that your classes are behaving properly. A simple test is sufficient: create an ArrayList of various Vehicle objects and iterate through it, printing info about each object.

Write a VehicleStats class that calculates various things about a list of Vehicle objects. Include these functions:

Write tests for your VehicleStats functions to demonstrate that they are returning the expected things.

Animal

Write a class Animal. You should include:

Write four subclasses of Animal (for example: Human, Cat, Sponge, Centipede). Note that your subclass constructors will need to call the Animal constructor using super().

Write some test code to show your subclasses all behave properly.

Write a class AnimalCalculator that calculates various things about a list of Animal objects. Include these functions:

Write unit tests for all AnimalCalculator functions that return a value, to demonstrate that the functions are working correctly. These tests can be written in the AnimalCalculator class, or in an external AnimalCalculatorTest class.


Next: