Shape
Interface-and-Implementing-Classes HierarchyShape / \ Circle TriangleYou will also be creating demo/app clases to 'demo' the facilities of the classes.
Shape
Shape
interface with the following:
numSides
method
getPerimeter
method
getArea
method
ShapeTest.java
). I am using to test your interface (all supplied code can be accessed via the link at the bottom of the page).
You might find it informative to look at the testing and other supplementary code; it may also give you ideas for the code you have to write.
ShapeDemo
ShapeDemo
class that contains a static method named demo
accepting a parameter that is a reference variable of type Shape
.
This method should 'demo' those Shape
methods implemented by the implementing class whose object was passed to the Shape
parameter. (This is like the other 'Demo'
classes with demo
methods I presented in class.)
I have supplied you with the class file of a Shape-implementing class named Rectangle
whose objects are thus eligible to be passed to ShapeDemo.demo
, as
well as the source and class files of a corresponding RactangleApp
class. This class creates an object of type Rectangle
. It then passes it
to ShapeDemo.demo
which invokes the various methods of the Shape
interface on the passed object. When control returns from demo
to RectangleApp
's
main
method, the methods specific to Rectangle
; i.e., those methods in Rectangle
that are not in the Shape
interface
are invoked showing their operation. Here are the files and here is is the output of RectangleApp
:
stdout === Rectangle App --- Shape demo for a Rectangle --- A rectangle with sides 3, 4 Perimeter: 14.00 Area : 12.00 ... Rectangle-specific demo Square: false --- Shape demo for a Rectangle --- A rectangle with sides 1, 1 Perimeter: 4.00 Area : 1.00 ... Rectangle-specific demo Square: true --- Shape demo for a Rectangle --- A rectangle with sides 4, 7 Perimeter: 22.00 Area : 28.00 ... Rectangle-specific demo Square: falsethe portion of the output generated by
ShapeDemo
(applied to a Rectangle
) is in green. You can use RectangleApp
as a guide to writing your own xxxApp
classes (below).
Circle
& CircleApp
Shape
-implementing clas and its xxxApp
class.
Circle
Circle
class that implements Shape and also contains the following additional (i.e., Circle-specific) methods:
getRadius
method
CircleApp
CircleApp
class that reads in Circle
objects from the file circles.data
and illustrates the facilities of the Circle
class. It
does this by first invoking ShapeDemo.demo
and then 'demo'ing' the Circle
-specific methods.
circles.data 5 0 2
stdout === Circle App --- Shape demo for a Circle --- A circle with diameter 5.0 Perimeter: 15.71 Area : 19.63 --- Circle-specific demo radius: 2.5 --- Shape demo for a Circle --- A circle with diameter 0.0 Perimeter: 0.00 Area : 0.00 --- Circle-specific demo radius: 0.0 --- Shape demo for a Circle --- A circle with diameter 2.0 Perimeter: 6.28 Area : 3.14 --- Circle-specific demo radius: 1.0As mentioned above,
CircleApp
is the RectangleApp
analog.
Triangle
& TriangleApp
Triangle
Lab 4.3
but for a Triangle
class.
Define a Triangle
class that implements Shape and also contains the following additional (i.e., Triangle-specific) methods:
getRadius
method
TriangleApp
TriangleApp
class that reads in Triangle
objects from the file triangles.data
and illustrates the facilities of the Triangle
class. It
does this by first invoking ShapeDemo.demo
and then 'demo'ing' the Triangle
-specific methods.
3 4 5 1 1 2 3 3 3
stdout === Triangle App --- Shape demo for a Triangle --- A triangle with sides 3, 4, 5 Perimeter: 12.00 Area : 6.00 --- Triangle-specific demo Equilateral: false Isoceles: false --- Shape demo for a Triangle --- A triangle with sides 6, 6, 3 Perimeter: 15.00 Area : 8.71 --- Triangle-specific demo Equilateral: false Isoceles: true --- Shape demo for a Triangle --- A triangle with sides 3, 3, 3 Perimeter: 9.00 Area : 3.90 --- Triangle-specific demo Equilateral: true Isoceles: true
ShapesApp
ShapesApp
class that:
Shape
objects (i.e., Circle
s and Triangle
s) from the file shapes.data
into a (partially-populated) array of Shape
.
The format of the file is:
type indicator data for particular type
where type indicator is either C
(for Circle), or T
(for Triangle). The indicator is followed by the appropriate
values for reading in the specified type, i.e., a diamter for circles, and the three sides for triangles.
ShapeDemo
's demo
method on each object as it is read in and placed into the array.
shapes.data C 12 T 3 4 5 T 10 15 4 T 1 2 3 C 2 C 9 T 3 4 5
stdout --- Shape demo for a Circle --- A circle with diameter 12.0 Perimeter: 37.70 Area : 113.10 --- Shape demo for a Triangle --- A triangle with sides 3, 4, 5 Perimeter: 12.00 Area : 6.00 --- Shape demo for a Triangle --- A triangle with sides 10, 15, 4 Perimeter: 29.00 Area : NaN --- Shape demo for a Triangle --- A triangle with sides 1, 2, 3 Perimeter: 6.00 Area : 2.45 --- Shape demo for a Circle --- A circle with diameter 2.0 Perimeter: 6.28 Area : 3.14 --- Shape demo for a Circle --- A circle with diameter 9.0 Perimeter: 28.27 Area : 63.62 --- Shape demo for a Triangle --- A triangle with sides 3, 4, 5 Perimeter: 12.00 Area : 6.00 A circle with diameter 12.0 (Perimeter: 37.70) A triangle with sides 3, 4, 5 (Perimeter: 12.00) A triangle with sides 10, 15, 4 (Perimeter: 29.00) A triangle with sides 1, 2, 3 (Perimeter: 6.00) A circle with diameter 2.0 (Perimeter: 6.28) A circle with diameter 9.0 (Perimeter: 28.27) A triangle with sides 3, 4, 5 (Perimeter: 12.00) A triangle with sides 1, 2, 3 (Perimeter: 6.00) A circle with diameter 2.0 (Perimeter: 6.28) A triangle with sides 3, 4, 5 (Perimeter: 12.00) A triangle with sides 3, 4, 5 (Perimeter: 12.00) A circle with diameter 9.0 (Perimeter: 28.27) A triangle with sides 10, 15, 4 (Perimeter: 29.00) A circle with diameter 12.0 (Perimeter: 37.70)