COMP1210 Project: Square Antiprism with JUnit Tests – Part 1

$25.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (5 votes)

Overview: This project consists of two classes: (1) SquareAntiprism is a class representing a
SquareAntiprism object and (2) SquareAntiprismTest class is a JUnit test class which contains one or
more test methods for each method in the SquareAntiprism class. Note that there is no requirement
for a class with a main method in this project.
You should create a new folder to hold the files for this project. After you have created your
SquareAntiprism.java file, you should create a jGRASP project and add your SquareAntiprism.java
file; you should see it in the Source Files category of the Project section of the Browse tab. With this
project is open, your test file, SquareAntiprismTest.java, will be automatically added to the project
when it is created; you should see it in the Test Files category. If SquareAntiprismTest.java appears
in source File category, you should right-click on the file and select “Mark As Test” from the rightclick menu. You will then be able to run the test file by clicking the JUnit run button on the Open
Projects toolbar.
A uniform Square Antiprism is a Square Antiprism composed of a sequence of eight equilateral
triangle sides closed by two square caps with edge length a.
The variables in
the formulas are:
�: edge length
�: height
A: surface area
V: volume
� =
%
1 −
( 1
cos , �
16/
0
!
4 ∗ �
� = 4 ∗ ((cos (�/8) /sin (�/8)) + √3) ∗ �!
� = 8 ∗ ?4 ∗ ,cos ,
π
16//
!
− 1 ∗ sin A
3�
16B ∗ �”/(12 ∗ Dsin ,
π
8
/E
!
)
Project: Square Antiprism with JUnit Tests – Part 1 Page 2 of 5
Page 2 of 5
• SquareAntiprism.java
Requirements: Create a SquareAntiprism class that stores the label and edge (edge must be nonnegative, >= 0). The SquareAntiprism class also includes methods to set and get each of these
fields, as well as methods to calculate the height, surface area, and volume of a SquareAntiprism
object, and a method to provide a String value that describes a SquareAntiprism object. The
SquareAntiprism class includes a one static field (or class variable) to track the number of
SquareAntiprism objects that have been created, as well appropriate static methods to access and
reset this field. And finally, this class provides a method that JUnit will use to test
SquareAntiprism objects for equality as well as a method required by Checkstyle.
Design: The SquareAntiprism class has fields, a constructor, and methods as outlined below.
(1) Fields: Instance Variables – label of type String and edge of type double. Initialize the
String to “” and the double variable to 0 in their respective declarations. These instance
variables should be private so that they are not directly accessible from outside of the
SquareAntiprism class, and these should be the only instance variables (fields) in the class.
Class Variable – count of type int should be private and static, and it should be initialized to
zero.
(2) Constructor: Your SquareAntiprism class must contain a public constructor that accepts two
parameters (see types of above) representing the label and edge. Instead of assigning the
parameters directly to the fields, the respective set method for each field (described below)
should be called since they are checking the validity of the parameter. For example, instead of
using the statement label = labelIn; use the statement setLabel(labelIn);
The constructor should increment the class variable count each time a SquareAntiprism is
constructed.
Below are examples of how the constructor could be used to create SquareAntiprism objects.
Note that although String and numeric literals are used for the actual parameters (or
arguments) in these examples, variables of the required type could have been used instead of
the literals.
SquareAntiprism ex1 = new SquareAntiprism(“Small Example”, 1.25);
SquareAntiprism ex2 = new SquareAntiprism(” Medium Example “, 10.4);
SquareAntiprism ex3 = new SquareAntiprism(“Large Example”, 32.46);
(3) Methods: Usually a class provides methods to access and modify each of its instance
variables (known as get and set methods) along with any other required methods. The
methods for SquareAntiprism, which should each be public, are described below. See the
formulas in the figure above and the Code and Test section below for information on
constructing these methods.
o getLabel: Accepts no parameters and returns a String representing the label field.
Project: Square Antiprism with JUnit Tests – Part 1 Page 3 of 5
Page 3 of 5
o setLabel: Takes a String parameter and returns a boolean. If the String
parameter is not null, then the “trimmed” String is set to the label field and the
method returns true. Otherwise, the method returns false and the label is not set.
o getEdge: Accepts no parameters and returns a double representing the edge field.
o setEdge: Takes a double parameter and returns a boolean. If the double
parameter is non-negative, then the parameter is set to the edge field and the method
returns true. Otherwise, the method returns false and the edge field is not set.
o height: Accepts no parameters and returns a double representing the height of the
SquareAntiprism.
o surfaceArea: Accepts no parameters and returns the double value for the surface
area of the SquareAntiprism.
o volume: Accepts no parameters and returns the double value for the volume of the
SquareAntiprism.
o toString: Returns a String containing the information about the SquareAntiprism
object formatted as shown below, including decimal formatting (“#,##0.0##”) for the
double values. Newline and tab escape sequences should be used to achieve the proper
layout within the String but it should not begin or end with a newline. In addition to the
field values (or corresponding “get” methods), the following methods should be used to
compute appropriate values in the toString method: height(),
surfaceArea(), and volume(). Each line should have no trailing spaces (e.g.,
there should be no spaces before a newline (\n) character). The toString value for
ex1, ex2, and ex3 respectively are shown below (the blank lines are not part of the
toString values).
SquareAntiprism “Small Example” with edge of 1.25 units has:
height = 1.075 units
surface area = 25.914 square units
volume = 8.336 cubic units
SquareAntiprism “Medium Example” with edge of 10.4 units has:
height = 8.947 units
surface area = 1,793.84 square units
volume = 4,800.871 cubic units
SquareAntiprism “Large Example” with edge of 32.46 units has:
height = 27.925 units
surface area = 17,474.872 square units
volume = 145,970.655 cubic units
o getCount: A static method that accepts no parameters and returns an int representing
the static count field.
o resetCount: A static method that returns nothing, accepts no parameters, and sets the
static count field to zero.
o equals: An instance method that accepts a parameter of type Object and returns false if
the Object is a not a SquareAntiprism; otherwise, when cast to a SquareAntiprism, if it
has the same field values (ignoring case in the label field) as the SquareAntiprism upon
which the method was called, it returns true. Otherwise, it returns false. Note that this
equals method with parameter type Object will be called by the JUnit Assert.assertEquals
Project: Square Antiprism with JUnit Tests – Part 1 Page 4 of 5
Page 4 of 5
method when two SquareAntiprism objects are checked for equality.
Below is a version you are free to use.
public boolean equals(Object obj) {

if (!(obj instanceof SquareAntiprism)) {
return false;
}
else {
SquareAntiprism d = (SquareAntiprism) obj;
return (label.equalsIgnoreCase(d.getLabel())
&& (Math.abs(edge – d.getEdge()) < .000001));
}
}
o hashCode(): Accepts no parameters and returns zero of type int. This method is
required by Checkstyle if the equals method above is implemented.
Code and Test: As you implement the methods in your SquareAntiprism class, you should
compile it and then create test methods as described below for the SquareAntiprismTest class.
• SquareAntiprismTest.java
Requirements: Create a SquareAntiprismTest class that contains a set of test methods to test
each of the methods in SquareAntiprism.
Design: Typically, in each test method, you will need to create an instance of SquareAntiprism,
call the method you are testing, and then make an assertion about the expected result and the
actual result (note that the actual result is commonly the result of invoking the method unless it
has a void return type). You can think of a test method as simply formalizing or codifying what
you could be doing in jGRASP interactions to make sure a method is working correctly. That is,
the sequence of statements that you would enter in interactions to test a method should be entered
into a single test method. You should have at least one test method for each method in
SquareAntiprism, except for associated getters and setters which can be tested in the same
method. Collectively, these test methods are a set of test cases that can be invoked with a single
click to test all the methods in your SquareAntiprism class.
Code and Test: Since this is the first project requiring you to write JUnit test methods, a good
strategy would be to begin by writing test methods for those methods in SquareAntiprism that
you “know” are correct. By doing this, you will be able to concentrate on the getting the test
methods correct. That is, if the test method fails, it is most likely due to a defect in the test
method itself rather the SquareAntiprism method being testing. As you become more familiar
with the process of writing test methods, you will be better prepared to write the test methods as
new methods are developed. Be sure to call the SquareAntiprism toString method in one of
your test methods and assert something about the return value. If you do not want to use
assertEquals, which would require the return value match the expected value exactly, you could
Project: Square Antiprism with JUnit Tests – Part 1 Page 5 of 5
Page 5 of 5
use assertTrue and check that the return value contains the expected value. For example, for
SquareAntiprism example3:
Assert.assertTrue(example3.toString().contains(“\”Large Example\””));
Also, remember that you can set a breakpoint in a JUnit test method and run the test file in Debug
mode. Then, when you have an instance in the Debug tab, you can unfold it to see its values or
you can open a canvas window and drag items from the Debug tab onto the canvas.
The Grading System
When you submit SquareAntiprism.java and SquareAntiprismTest.java, the grading system will use the
results of your test methods and their level of coverage of your source files as well as the results of our
reference correctness tests to determine your grade. In this project, your test file should provide at least
method coverage. That is, each method must be called at least once in a test method.