This course has already ended.

⌛ 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() and double circumference().

  • Class Circle that implements the interface IShapeMetrics and stores the radius of a circle as a value of the double 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 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 values of the double 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 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, 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.

Posting submission...