CSE1030Z Lab 07

Inheritance

Thu 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 Square adds its own attribute that defines the length of the sides of the square (so that we can draw smaller and larger squares). It also adds its own methods to get and set the length. Square overrides the toString method so that it can return string that also contains information about the length. Square implements the draw method because it has enough information to actually draw a square using the position, length, and color of the square.

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

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:

TestShape2 should produce the following output:

submit 1030Z L7R Square.java Ellipse.java