- COMP.CS.140
- 6. Class Hierarchies
- 6.6 ⌛ Shapes
⌛ Shapes¶
The remote material repository (the round6/shapes
directory) has supplementary files
for this question.
Place your code into files named Circle.java, Rectangle.java and IShapeMetrics.java
in the round6/shapes
directory of your local repository. We use here the convention of naming
interfaces with I-prefix. This is a common practice especially in the C# programming language,
but some use it in Java too.
In this exercise, you should write the following interface and two classes:
Interface
IShapeMetrics
that has:Member variable
double PI
that defines pi using 5 decimals of precision.Abstract member functions
String name()
,double area()
anddouble circumference()
.
Class
Circle
that implements the interfaceIShapeMetrics
and stores the radius of a circle as a value of thedouble
type. Public members:Constructor
Circle(double radius)
that initializes the radius.Member function
String toString()
that overrides the default version inherited from Object. The function returns aString
of form “Circle with radius: x
”, wherex
is the radius with 2 decimals of precision.Member function
String name()
that returns theString
“circle
”.Member function
double area()
that returns the area of the circle, computed usingPI
as the value of pi.Member function
double circumference()
that returns the circumference of the rectangle, computed usingPI
as the value of pi.
Class
Rectangle
that implements the interfaceIShapeMetrics
and stores the height and width of a rectangle as values of thedouble
type. Public members:Constructor
Rectangle(double height, double width)
that initilizes height and width.Member function
String toString()
that overrides the default version inherited from Object. Function returns aString
of form “Rectangle with height x and width y
”, wherex
is the height andy
the width of the rectangle, both with 2 decimals of precision.Member function
String name()
that returns theString
“rectangle
”.Member function
double area()
that returns the area of the rectangle.Member function
double circumference()
that returns the circumference of the rectangle.
Testing¶
You may test your implementation by using the test program given in the file
InterfaceTest.java
and the example output given in the files output1.txt
and
output2.txt
. Place these files to the same directory with your code, compile the test program,
for example, as javac *.java
, and run the first test as java InterfaceTest "4" "5" "6.0" "7.0"
and the second test as java InterfaceTest "4 4" "5 2" "6.0 12" "70.0 4"
. The expected outputs of
these two tests are given in the files output1.txt
and output2.txt
.
A+ presents the exercise submission form here.