Labs

We will explore abstract classes, and create a class hierarchy where common fields are encapsulated in an abstract parent class.

  1. AbstractClasses/com.example.abstractclasses.labs.vehicles
  2. Make both the Vehicle and FlyingVehicle classes abstract. Do you have to change any other application code?

(Solution: VehicleApp.java)

  1. Create an abstract class named Shape. Provide private int fields for x and y coordinates of the shape. Add getters and setters as well as appropriate constructors (including no-arg constructor). Add an abstract method called getArea.

(Solution: Shape.java)

  1. Develop two child classes of Shape: Rectangle and Circle. A Rectangle has fields of type double for width and height; a Circle has a radius. Provide getters and setters for each field and appropriate constructors.

Implement the getArea method in each subclass. (Hint: Find java.lang.Math in the Java API documentation for usage information on Math.PI and the Math.pow() method for calculating the circle's area from its radius.)

(Solutions: Rectangle.java, Circle.java)

  1. Write a program that contains a main method that creates an array of three Shapes. Store two Rectangles and a Circle in the array. Pass this Shape array to a method that loops through the array, printing out the area of each shape.

(Solution: ShapeApp.java)


Prev -- Up