⌛ Shapes¶
Place your code into files named Circle.java, Rectangle.java and IShapeMetrics.java1
in the directory Round6/shapes. Remember to pull student_template_project for material.
1 Here we use 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 implement the following simple interface and two classes:
Interface
IShapeMetricsthat has:Member variable
double PIthat defines pi using 5 decimals of precision.Abstract memebr functions
String name(),double area()anddouble circumference().
Class
Circlethat implements the interfaceIShapeMetricsand stores the radius of a circle as adouble. Public members:Constructor
Circle(double radius)that initializes the radius.Member function
String toString()that returns aStringof form “Circle with radius: x”, wherexis 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 usingPIas the value of pi.Member function
double circumference()that returns the circumference of the rectangle, computed usingPIas the value of pi.
Class
Rectanglethat implements the interfaceIShapeMetricsand stores the height and width of a rectangle asdouble. Public members:Constructor
Rectangle(double height, double width)that initilizes height and width.Member function
String toString()that returns aStringof form “Rectangle with height x and width y”, wherexis the height andythe 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
e.g. 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.