CSE1030 Lab 05

Inheritance

Week of Feb 24
Due: Mon Mar 3 before 11:59PM

Introduction

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

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, Vector2D, and StdDraw is here:

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

TestShape should produce the following output:

submit 1030 L5 Circle.java Rectangle.java