Lab Exercise 2
Lab Exercise 2
In this exercise you will create a hierarchy of animals that is rooted in an abstract
class Animal. Several of the animal classes will implement an interface called Pet. You
will experiment with variations of these animals, their methods, and polymorphism.
Write the interface called GeometricObject, which declares two abstract methods:
getParimeter() and getArea().
Write the implementation class Circle, with a variable radius, which implements the
interface GeometricObject.
Write a test program called TestCircle to test the methods defined in Circle.
Note:
Formula for Perimeter of Circle = 2pr
Formula for Area of Circle = pr2
The class ResizableCircle is defined as a subclass of the class Circle, which also
implements an interface called Resizable.
The interface Resizable declares an abstract method resize(), which modifies the
dimension (such as radius) by the given percentage.
Write the interface Resizable and the class ResizableCircle.
Write a test program called TestResizableCircle to test the methods defined in
ResizableCircle.
Q-3)
Define another interface called SportInterface. This interface containt the following
abstract methods.
String getMyFavoriteSport();
void setMyFavoriteSport(String sportName);
int howMuchItCostToPlayThisSport();
Define the third interface called HobbyInterface. The interface contains the following
abstract methods.
String whatIsMyHobby();
void setMyHobby(String hobby);
Write a class called SportAndHobbyImpl that implements the all three interfaces
defined above. It sets other fields - MyFavoriteSport and MyHobby - via
setMyFavoriteSport(String sportName) and setMyHobby(String hobby) methods.
It computes theCostToPlayThisSport as following:
age * 10