Using Interfaces Labs
Classes for these labs are in com.example.interfaces.labs.shapes. We will create and use an interface to give classes a common type and behavior.
- Create an interface named
Drawablewith the void methoddrawdeclared inside of it.
(Solution: Drawable.java)
- Retrofit
Shapeto implementDrawable, putting the actual implementation code inRectangleandCircle. Don't worry about doing any graphics for drawing, just print out a simple message indicating the type and area of each shape you are "drawing."
(Solutions: Shape.java, Circle.java, Rectangle.java)
- Create a tester program to store an array of
Drawableobjects, adding someCircleandRectangleobjects to it. Call thedrawmethod on each.
(Solution: DrawableTester.java)
- Create a class named
Textthat implementsDrawableand has aStringfield calledvalue. Add getters and setters, as well as an appropriate constructor. HaveText'sdrawmethod print the value field. Modify your tester program so that aTextobject is added into the array in addition to theShapeobjects.
(Solutions: Text.java, DrawableTester2.java)