Table of Contents
Inheritance and Polymorphism
The previous assignment solved a single specific problem: drawing points with automatic scaling. What if we wanted to draw something else besides a set of points? Rather than starting from scratch all over again, it would be nice to be able to build on what we’ve already done, to provide even more useful functionality.
We’ll use an Object-Oriented approach called Inheritance and Polymorphism, and use these techniques to construct a set of classes which make it easy to draw any shape we’d like, and in the process reuse a lot of our code.
The assignment is divided into two parts:
Note: I want to see separate submissions for each of the parts. That is, I want you to consider them separate from the point of view of describing what you did, providing listings and output, etc. So expect to submit two separate Word documents, one for each part.
Part 1: Drawing Shapes: Preparing the Infrastructure
What might we need to accomplish this?
First, we’ll need a convenient place to draw our shapes. What can we draw shapes on? A Swing JPanel seems appropriate. So what if we derive our class from class JPanel — how about a ShapesPanel?
Second, we should have some way of translating between the coordinate system of our ShapesPanel and the coordinate system required to display the data — a kind of adapter class. Remember our CoordinateSystem class? That’s its job.
Finally, we’ll need some shapes to draw. Unfortunately, if we use the standard Java classes Point, Rectangle, etc., they’re not really shapes, and they don’t know how to draw themselves.
Note: I find it curious that the same folks who gave us this incredible Object-Oriented programming language didn’t, in its original version, provide even a basic set of object-oriented shape classes
In the JSDK version 1.2 and beyond, they provide a set of classes for doing two dimensional drawing — the 2D API — but that’s a topic for another course.
Since shapes have a set of common attributes and behavior, let’s first create a class that captures that common stuff — class Shape.
Then we’ll at least have to create a class that represents a point — class Point, derived from class Shape. Note that our class Point is not the same as java.awt.Point. Under some circumstances, you may need to explicitly qualify one or the other of these classes to ensure they don’t conflict with each other.
Note: Do not change the name of your Point class!
Hint: Do not use wildcard importation, such as the following:
import java.awt.*;
or:
import javax.swing.*;
Here’s What To Do:
Write a set of classes that satisfies the Shapes Specification.
Note that this specification is for the entire assignment, but in this part, we’ll just focus on the following classes:
screen.ScreenCoordinateshapes.Shapeshapes.Pointshapes.ShapesPanelshapes.ShapesTestworld.Boundsworld.Coordinateworld.CoordinateSystem
Note: This assignment uses a
CoordinateSystemclass that has been changed and enhanced from the version in the previous assignment.
In this part, you should ignore the following classes:
shapes.FilledRectangleshapes.Lineshapes.Rectangle
Here’s the order in which you probably should implement the classes you need for this part of the assignment:
Coordinate and ScreenCoordinateBoundsCoordinateSystemShapePointShapesPanelShapesTest
(You can implement them in any order you like, but since the classes have interdependencies, you’ll probably have trouble if you deviate from the above order too much.)
I encourage you to progressively implement small pieces of each class, get something that compiles and (to some extent) runs, and then iterate a number of times over them to produce the final result. It’s always a good idea to implement a small piece, get it working, and then add to it progressively. Don’t try to add everything at once, or you’ll likely get stuck and become quite frustrated!
Note: I have intentionally organized these classes into three packages:
shapes,world, andscreen. It is important to keep these classes in these specified packages.
As before, you may not change the specification, except to addmain()methods for testing purposes.
Use These Classes to Produce Equivalent Results to the Previous Assignment
My solution produced the following results for the set of points specified in assignment 4:

Finally, To Ensure That You Did Things Correctly:
Change the set of points to be displayed by the ShapesTest class to:
private static Shape[] m_shapes =
{
new Point(0.0, 0.0),
new Point(4.903926402016152, 0.9754516100806413),
new Point(9.238795325112868, 3.8268343236508975),
new Point(12.472044184538179, 8.333553495294034),
new Point(14.142135623730951, 14.14213562373095),
new Point(13.889255825490057, 20.78674030756363),
new Point(11.480502970952696, 27.716385975338603),
new Point(6.828161270564491, 34.32748481411306),
new Point(2.4492935982947065E-15, 40.0),
new Point(-8.77906449072577, 44.13533761814537),
new Point(-19.134171618254488, 46.19397662556434),
new Point(-30.556362816078106, 45.730828676639995),
new Point(-42.426406871192846, 42.42640687119285),
new Point(-54.04552479966545, 36.11206514627414),
new Point(-64.67156727579008, 26.787840265556262),
new Point(-73.5588960302423, 14.631774151209578),
new Point(-80.0, -6.12570991828312E-14),
new Point(-83.36674883427457, -16.582677371370984),
new Point(-83.14915792601576, -34.44150891285818),
new Point(-78.98961316874173, -52.77917213686232),
new Point(-70.71067811865464, -70.71067811865487),
new Point(-58.33487446705808, -87.30430929176735),
new Point(-42.09517756015967, -101.62674857624162),
new Point(-22.435387031854496, -112.79030724637155),
new Point(2.977005887073927E-13, -120.0),
new Point(24.386290252016366, -122.59816005040373),
new Point(49.748846207462016, -120.10433922646713),
new Point(75.00198145764665, -112.24839766084337),
new Point(98.99494936611698, -98.99494936611632),
new Point(120.56309378386935, -80.5576837878419),
new Point(138.58192987669324, -57.40251485476294),
new Point(152.02171846250084, -30.238999912499278),
new Point(160.0, 6.713540381873849E-13),
new Point(161.82957126653287, 32.18990313266188),
new Point(157.05952052691845, 65.05618350206599),
new Point(145.50718215294495, 97.2247907784311),
new Point(127.2792206135779, 127.2792206135792),
new Point(102.7804931086256, 153.82187827597141),
new Point(72.7098521493661, 175.53711117714488),
new Point(38.042612793143924, 191.25312967863016),
new Point(-1.1822174476228077E-12, 200.0),
new Point(-39.99351601330753, 201.06098248266198),
new Point(-80.36352079666992, 194.01470182736978),
new Point(-119.44760009921532, 178.76596664504666),
new Point(-155.56349186104111, 155.5634918610398),
new Point(-187.0806627680731, 125.00330242940987),
new Point(-212.4922924775962, 88.01718944397007),
new Point(-230.48454089475925, 45.84622567378966),
new Point(-240.0, -3.3815107191745065E-13),
new Point(-240.29239369879141, -47.7971288939516),
new Point(-230.96988312782167, -95.67085809127246),
new Point(-212.0247511371491, -141.67040941999844),
new Point(-183.8477631085026, -183.8477631085021),
new Point(-147.22611175019503, -220.33944726017418),
new Point(-103.3245267385749, -249.44747377804714),
new Point(-53.64983855443617, -269.71595211088817),
new Point(-1.114775216380581E-12, -280.0),
new Point(55.60074177459526, -279.5238049149209),
new Point(110.9781953858746, -267.92506442827374),
new Point(163.8932187407812, -245.28353562925182),
new Point(212.13203435596284, -212.13203435596566),
new Point(253.59823175227504, -169.44892107098056),
new Point(286.4026550784979, -118.63186403318012),
new Point(308.94736332701706, -61.45345143508309),
new Point(320.0, -2.9989257333312617E-12),
new Point(318.75521613105053, 63.404354655238485),
new Point(304.880245728726, 126.28553268047635),
new Point(278.54232012135475, 186.11602806156355),
new Point(240.4163056034291, 240.41630560342327),
new Point(191.67173039176643, 286.85701624437564),
new Point(133.93920132778575, 323.35783637894855),
new Point(69.25706431573045, 348.17877454314583),
new Point(5.314300478934592E-12, 360.0),
new Point(-71.2079675358813, 357.9866273471802),
new Point(-141.59286997507772, 341.8354270291784),
new Point(-208.33883738234562, 311.80110461345794),
new Point(-268.70057685088335, 268.7005768508928),
new Point(-320.11580073647605, 213.89453971255264),
new Point(-360.313017679399, 149.2465386223918),
new Point(-387.41018575927455, 77.06067719637821),
new Point(-400.0, 8.060899453190573E-12),
new Point(-397.21803856330996, -79.01158041652369),
new Point(-378.7906083296307, -156.90020726967936),
new Point(-345.05988910556096, -230.5616467031279),
new Point(-296.9848480983562, -296.98484809834366),
new Point(-236.11734903333863, -353.37458522857656),
new Point(-164.5538759169975, -397.2681989798496),
new Point(-84.86429007702566, -426.64159697540333),
new Point(-1.0457125646763092E-11, -440.0),
new Point(86.81519329716642, -436.44944977943965),
new Point(172.20754456427997, -415.7457896300834),
new Point(252.78445602390923, -378.3186735976646),
new Point(325.26911934580323, -325.26911934582046),
new Point(386.6333697206765, -258.34015835412555),
new Point(434.22338028029975, -179.86121321160428),
new Point(465.8730081915318, -92.6679029576742),
new Point(480.0, -1.399511880474836E-11),
new Point(475.6808609955696, 94.61880617780803),
new Point(452.7009709305362, 187.5148818588802),
new Point(411.57745808976847, 275.00726534469027),
new Point(353.553390593285, 353.5533905932625),
new Point(280.5629676749128, 419.8921542127762),
new Point(195.1685505062114, 471.1785615807498),
new Point(100.47151583832317, 505.1044194076603),
new Point(1.7964336191386278E-11, 520.0),
new Point(-102.42241905844921, 514.9122722116996),
new Point(-202.82221915347998, 489.6561522309893),
new Point(-297.23007466547097, 444.83624258187257),
new Point(-381.8376618407214, 381.8376618407499),
new Point(-453.15093870487567, 302.78577699570036),
new Point(-508.13374288119957, 210.47588780081898),
new Point(-544.3358306237886, 108.27512871897255),
new Point(-560.0, 2.2364777806676846E-11),
new Point(-554.1436834278297, -110.22603193908998),
new Point(-526.6113335314425, -218.12955644807943),
new Point(-478.0950270739769, -319.4528839862512),
new Point(-410.12193308821503, -410.12193308818007),
new Point(-325.0085863164884, -486.4097231969749),
new Point(-225.78322509542696, -545.0889241816492),
new Point(-116.07874159962239, -583.567241839917),
new Point(-2.7196443650620066E-11, -600.0),
new Point(118.02964481973031, -593.3750946439599),
new Point(233.43689374267845, -563.5665148318958),
new Point(341.67569330703117, -511.3538115660815),
new Point(438.40620433563845, -438.40620433568057),
new Point(519.6685076890739, -347.2313956372767),
new Point(582.0441054820988, -241.09056239003527),
new Point(622.7986530560452, -123.88235448027261),
new Point(640.0, -3.245933372321593E-11),
new Point(632.6065058600901, 125.83325770037023),
new Point(600.5216961323493, 248.74423103727707),
new Point(544.6125960581863, 363.89850262781073),
new Point(466.69047558314634, 466.69047558309643),
new Point(369.4542049580654, 552.9272921811726),
new Point(256.397899684644, 618.9992867825481),
new Point(131.68596736092329, 662.0300642721733),
new Point(3.815344802446445E-11, 680.0),
new Point(-133.6368705810097, 671.8379170762205),
new Point(-264.05156833187533, 637.476877432803),
new Point(-386.12131194858995, 577.8713805502914),
new Point(-494.9747468305542, 494.9747468306124),
new Point(-586.186076673271, 391.6770142788545),
new Point(-655.9544680829973, 271.7052369792532),
new Point(-701.2614754883012, 139.48958024157437),
new Point(-720.0, 4.4278786554365614E-11),
new Point(-711.0693282923509, -141.4404834616488),
new Point(-674.4320587332569, -279.35890562647313),
new Point(-611.1301650423967, -408.3441212693688),
new Point(-523.2590180780787, -523.2590180780116),
new Point(-413.8998235996438, -619.4448611653693),
new Point(-287.01257427386275, -692.9096493834462),
new Point(-147.2931931222259, -740.4928867044292),
new Point(-5.083534931291943E-11, -760.0),
new Point(149.24409634228743, -750.3007395084813),
new Point(294.6662429210706, -711.3872400337109),
new Point(430.5669305901473, -644.3889495345022),
new Point(551.5432893254687, -551.5432893255455),
new Point(652.7036456574673, -436.12263292043366),
new Point(729.8648306838951, -302.31991156847266),
new Point(779.7242979205571, -155.0968060028778),
new Point(800.0, -5.78231363001259E-11),
new Point(789.5321507246119, 157.04770922292565),
new Point(748.3424213341651, 309.9735802156676),
new Point(677.6477340266081, 452.78973991092545),
new Point(579.8275605730124, 579.8275605729254),
new Point(458.34544224122374, 685.9624301495651),
new Point(317.627248863083, 766.8200119843438),
new Point(162.90041888353014, 818.9557091366848),
new Point(6.524214751598501E-11, 840.0),
new Point(-164.85132210356346, 828.7635619407426),
new Point(-325.28091751026426, 785.2976026346194),
new Point(-475.0125492317033, 710.906518518714),
new Point(-608.111831820382, 608.1118318204797),
new Point(-719.2212146416626, 480.56825156201415),
new Point(-803.7751932847923, 332.9345861576938),
new Point(-858.1871203528125, 170.70403176418293),
new Point(-880.0, 7.309238296049678E-11),
new Point(-867.9949731568734, -172.65493498420085),
new Point(-822.2527839350739, -340.58825480486047),
new Point(-744.1653030108204, -497.23535855248065),
new Point(-636.3961030679474, -636.3961030678381),
new Point(-502.7910608828051, -752.4799991337601),
new Point(-348.2419234523049, -840.7303745852406),
new Point(-178.50764464483612, -897.4185315689402),
new Point(-8.137384263366119E-11, -920.0),
new Point(180.45854786483778, -907.2263843730043),
new Point(355.89559209945634, -859.2079652355287),
new Point(519.4581678732577, -777.4240875029268),
new Point(664.6803743152941, -664.6803743154153),
new Point(785.7387836258571, -525.0138702035962),
new Point(877.6855558856887, -363.54926074691645),
new Point(936.6499427850677, -186.31125752548976),
new Point(960.0, -9.008652653547826E-11),
new Point(946.4577955891351, 188.26216074547432),
new Point(896.1631465359835, 371.2029293940518),
new Point(810.6828719950336, 541.6809771940344),
new Point(692.9646455628834, 692.9646455627498),
new Point(547.2366795243878, 818.9975681179538),
new Point(378.8565980415284, 914.6407371861368),
new Point(194.1148704061438, 975.8813540011952),
};
To obtain this source code, you can copy and paste the above into your Java IDE.
After you’ve made the changes, see what your program displays!
You shouldn’t have to make any other changes to the classes you wrote for this part of the assignment other than changing the
m_shapesarray, above. If you do have to make additional changes to make this last part work correctly, you need to go back and ensure that the earlier parts of this assignment still work.
If your program doesn’t show anything reasonable, or if it doesn’t display all the points in the ShapesPanel, or if it doesn’t scale properly, then go back and fix the problems, and make sure that the program still works with the original set of points.
Part 2: Drawing Shapes: Generalizing the Problem
In this section, you’ll write a program that displays the data in a histogram form, like this:

with the same resizing behavior as before:

In other words:
- display the original points (in black)
- display green lines connecting the points
- display the data in the form of a histogram by drawing red rectangles
Because we’re displaying rectangles based at the x axis (where y = 0), we’re including a larger region of world coordinate space, so the points appear to move upward in the window, compared with the results of the last part of this assignment.
We’ll be using inheritance and polymorphism to simplify the job. We’ve done all the hard work in Part A — this should be quite a bit simpler, if you’ve done things properly in Part A.
To implement this, you will need refer to the Specification again; we’ll be implementing more of the classes specified therein.
Here’s What To Do:
- Write the
shapes.Lineandshapes.Rectangleclasses.
Bear in mind:
- Both
LineandRectanglewill extend classShape. - Ignore the
FilledRectangleclass; that will be used in the next section. - Augment your
ShapesTestclass to use the above classes to produce the above results.
Once you’ve implemented these classes, and augmented the ShapesTest class appropriately, you should see results like the above images.
How will you draw the histogram?
Hints:
- Note that the points are all spaced evenly in the x direction, so you can figure out the width of each rectangle quite easily from the x coordinates of the points. Each point should be centered in its corresponding rectangle. The height of the rectangle should be determined by recognizing that the base of the rectangle is on the x axis (where y = 0), and the top of the rectangle should intersect with the corresponding point.
- Think about the order in which you need to draw the three sets of graphical objects so that they display correctly. This will be reflected in how you specify the shapes in your
m_shapesarray in theShapesTestclass. - You should not have to change any of the classes implemented in Part 1 in order to get this to work. If you think you must make a change to them, you’re probably doing something wrong! Try to figure out what you did wrong, and correct it, highlighting what you changed. I want to see what you did to make this work!
- Write the
shapes.FilledRectangleclass
Here, we will modify your program to display the histogram slightly differently:

Notice that our rectangles are now filled with a different color. This is where the FilledRectangle class comes in:
- Implement the
FilledRectangleclass, per the specification. - Modify your
ShapesTestclass to use theFilledRectangleclass, in place of theRectangleclass, to produce the above results.
Notes:
- There should be no need to change any other classes to make this work.
- The implementation of
FilledRectangleshould be relatively simple. (A lot of the work is already done in theRectangleclass.) - The changes to
ShapesTestshould be simple — just change theRectangles referred to in them_shapesarray toFilledRectangles.
- See Whether You’ve Implemented Things Correctly:
Change the contents of the m_shapes array to the following:
public static Shape[] m_shapes =
{
new FilledRectangle(-4.999999999999994, 95.0, 5.000000000000006, 105.0, Color.cyan, Color.blue),
new FilledRectangle(-100.10565162951535, 25.901699437494752, -90.10565162951535, 35.901699437494756, Color.cyan, Color.green),
new FilledRectangle(-63.77852522924732, -85.90169943749473, -53.77852522924732, -75.90169943749473, Color.cyan, Color.red),
new FilledRectangle(53.77852522924729, -85.90169943749476, 63.77852522924729, -75.90169943749476, Color.cyan, Color.magenta),
new FilledRectangle(90.10565162951536, 25.901699437494717, 100.10565162951536, 35.90169943749471, Color.cyan, Color.orange),
new Line(6.123233995736766E-15, 100.0, -58.77852522924732, -80.90169943749473, Color.blue),
new Line(6.123233995736766E-15, 100.0, 58.77852522924729, -80.90169943749476, Color.blue),
new Line(-95.10565162951535, 30.901699437494752, 58.77852522924729, -80.90169943749476, Color.green),
new Line(-95.10565162951535, 30.901699437494752, 95.10565162951536, 30.901699437494717, Color.green),
new Line(-58.77852522924732, -80.90169943749473, 95.10565162951536, 30.901699437494717, Color.red),
new Line(-58.77852522924732, -80.90169943749473, 6.123233995736766E-15, 100.0, Color.red),
new Line(58.77852522924729, -80.90169943749476, 6.123233995736766E-15, 100.0, Color.magenta),
new Line(58.77852522924729, -80.90169943749476, -95.10565162951535, 30.901699437494752, Color.magenta),
new Line(95.10565162951536, 30.901699437494717, -95.10565162951535, 30.901699437494752, Color.orange),
new Line(95.10565162951536, 30.901699437494717, -58.77852522924732, -80.90169943749473, Color.orange),
new Line(-150.0, 0.0, +150.0, 0.0, Color.black),
new Line(0.0, -150.0, 0.0, +150.0, Color.black),
new Point(0.0, 0.0, Color.red),
};
and see what your application displays. (You can copy out of the above and paste it into your Java IDE.)
Hint: To get the best view of this, resize your display so that it’s square, rather than rectangular.
It should display something interesting, and it should automatically display all of the above shapes in the window (no shapes, or parts thereof, should be outside of the display. The entire display should scale appropriately as you resize the window. If all of these don’t happen, you need to figure out what the problem is.
