CSE1030Z Lab 07

Inheritance

Wed Mar 06 2:30-4:00 PM

Introduction

This lab uses inheritance to implement a simple hierarchy of shapes that are drawn to a window. In this lab, you will:

This is intended to be a relatively short lab; you should submit your work sometime today, and preferable before the end of the lab.

The Shape Hierarchy

Implementing a hierarchy of drawable shapes is a common example of inheritance in object oriented programming. Consider the shape hierarchy shown in the UML diagram below. Each shape has:

Subclasses of Shape are allowed to add their own new attributes and new methods; subclasses may also override the public (and protected) methods of Shape to customize their behavior.

We see that Circle adds its own attribute that defines the radius of the circle (so that we can draw smaller and larger circles). It also adds its own methods to get and set the radius. Circle overrides the toString method so that it can return string that also contains information about the radius. Circle implements the draw method because it has enough information to actually draw a circle using the position, radius, and color of the circle.

Rectangle adds its own attributes to define the width and height of the rectangle. It provides its own methods to get and set the width and height. Rectangle overrides the toString method so that it can return a string that also contains information about the width and height. Rectangle implements the draw method because it has enough information to actually draw a rectangle using the position, width, height, and color of the rectangle.

Question 1: Implement the Shape Hierarchy

Implement the Shape hierarchy. The APIs for the various classes can be found here:

A jar file containing Point2D is here:

Starter code for each class and a small tester is listed here:

TestShape should produce the following output:

submit 1030Z L7W Circle.java Rectangle.java