⌛ 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 IShapeMetrics that has:

    • Member variable double PI that defines pi using 5 decimals of precision.

    • Abstract memebr functions String name(), double area() and double circumference().

  • Class Circle that implements the interface IShapeMetrics and stores the radius of a circle as a double. Public members:

    • Constructor Circle(double radius) that initializes the radius.

    • Member function String toString() that returns a String of form “Circle with radius: x”, where x is the radius with 2 decimals of precision.

    • Member function String name() that returns the Stringcircle”.

    • Member function double area() that returns the area of the circle, computed using PI as the value of pi.

    • Member function double circumference() that returns the circumference of the rectangle, computed using PI as the value of pi.

  • Class Rectangle that implements the interface IShapeMetrics and stores the height and width of a rectangle as double. Public members:

    • Constructor Rectangle(double height, double width) that initilizes height and width.

    • Member function String toString() that returns a String of form “Rectangle with height x and width y”, where x is the height and y the width of the rectangle, both with 2 decimals of precision.

    • Member function String name() that returns the Stringrectangle”.

    • 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.