⌛ Shapes

The remote material repository (the round6/shapes directory) has supplementary files for this question.

The exercise is returned as a Maven project. Place the pom.xml file in the round6/shapes directory of your local repository and create to this directory the src/main/java subdirectory. Create class files Circle.java, Rectangle.java and IShapeMetrics.java and attach them to the fi.tuni.prog3.shapes package. Your files should be in the round6/shapes/src/main/java/fi/tuni/prog3/shapes directory. We use here the convention of naming interfaces with an 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. Please, note that point is used as the decimal separator. The format member function of the String class is convenient helper in this task.

    • 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 circle, 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. Please, note that point is used as the decimal separator. The format member function of the String class is convenient helper in this task.

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

The automatic tests, and the ones given below, assume that you make the following definitions in your pom.xml project file:

  • The value of artifactId is shapes.

  • The value of version is 1.0.

  • The values of the maven.compiler.source and maven.compiler.target elements are 17 or lower. The grader uses Java 17, so any newer versions won’t work.

  • A Onejar plugin definition where the value of mainClass is InterfaceTest which is the name of the given test class (see below).

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.

Set InterfaceTest.java into the root of the src/main/java subdirectory of your Maven project, and the other files into the root directory of your Maven project, that is, where the pom.xml is. Note that InterfaceTest.java does not include a package definition and therefore is not placed into a deeper subdirectory.

After this you can compile the program with mvn package and run the first test as java -jar target/shapes-1.0.one-jar.jar "4" "5" "6.0" "7.0" and the second test as java -jar target/shapes-1.0.one-jar.jar "4 4" "5 2" "6.0 12" "70.0 4" in the root directory of the project. 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...