- COMP.CS.140
- 6. Class Hierarchies
- 6.6 ⌛ Shapes
⌛ 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
IShapeMetricsthat has:Member variable
double PIthat defines pi using 5 decimals of precision.Abstract member functions
String name(),double area()anddouble circumference().
Class
Circlethat implements the interfaceIShapeMetricsand stores the radius of a circle as a value of thedoubletype. 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 aStringof form “Circle with radius: x”, wherexis the radius with 2 decimals of precision. Please, note that point is used as the decimal separator. Theformatmember function of theStringclass is convenient helper in this task.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 circle, computed usingPIas the value of pi.
Class
Rectanglethat implements the interfaceIShapeMetricsand stores the height and width of a rectangle as values of thedoubletype. 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 aStringof form “Rectangle with height x and width y”, wherexis the height andythe width of the rectangle, both with 2 decimals of precision. Please, note that point is used as the decimal separator. Theformatmember function of theStringclass is convenient helper in this task.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.
The automatic tests, and the ones given below, assume that you make the following definitions
in your pom.xml project file:
The value of
artifactIdisshapes.The value of
versionis1.0.The values of the
maven.compiler.sourceandmaven.compiler.targetelements are17or lower. The grader uses Java 17, so any newer versions won’t work.A Onejar plugin definition where the value of
mainClassisInterfaceTestwhich 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.