Please follow all the instructions. e Spherocylinder.java (new items for this cl
ID: 3742159 • Letter: P
Question
Please follow all the instructions. e Spherocylinder.java (new items for this class in Part 2 are underlined) Requirements: Create a Spherocylinder class that stores the label, radius, and cylinder height where both radius and cylinder height are non-negative (0). The Spherocylinder class also includes methods to set and get each of these fields, as well as methods to calculate the circumference, surface area, and volume of a Spherocylinder object, and a method to provide a String value that describes a Spherocylinder object. The Spherocylinder class includes a one static field (or class variable) to track the number of Spherocylinder 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 Spherocylinder objects for equality as well as a method required by Checkstyle. In addition, Spherocylinder must implement the Comparable interface for objects of type SpherocylinderExplanation / Answer
import java.text.DecimalFormat;
public class Spherocylinder implements Comparable<Spherocylinder> {
private String label = "";
private double radius = 0, height = 0;
//class variable
private static int count = 0;
public Spherocylinder(String labelIn, double radiusIn,
double heightIn) {
setLabel(labelIn);
setRadius(radiusIn);
setCylinderHeight(heightIn);
count++;
}
//getCount gets total number of objects created.
public static int getCount() {
return count;
}
//resetCount set's count to zero.
public static void resetCount() {
count = 0;
}
//getLabel gets the instance variable label.
public String getLabel() {
return label;
}
//setLabel sets the instance variable label.
public boolean setLabel(String labelIn) {
if (labelIn != null) {
label = labelIn.trim();
return true;
}
else {
return false;
}
}
//getRadius gets the instance variable radius.
public double getRadius() {
return radius;
}
//setRadius set the instance variable radius.
public boolean setRadius(double radiusIn) {
if (radiusIn >= 0) {
radius = radiusIn;
return true;
}
else {
return false;
}
}
//getCylinderHeight gets the instance variable height.
public double getCylinderHeight() {
return height;
}
//setCylinderHeight sets the instance variable height.
public boolean setCylinderHeight(double heightIn) {
if (heightIn >= 0) {
height = heightIn;
return true;
}
else {
return false;
}
}
//method to return the circumference.
public double circumference() {
return (2 * Math.PI * radius);
}
//method to return the surfaceArea.
public double surfaceArea() {
return (circumference() * (2 * radius + height));
}
//method to return the volume.
public double volume() {
//correct
double value1 = (Math.PI * Math.pow(radius, 2));
double value2 = 4.0 / 3.0;
return value1 * (((value2 * radius) + height));
}
//method to verify if two objects are equal.
public boolean equals(Object obj) {
if (!(obj instanceof Spherocylinder)) {
return false;
}
else {
Spherocylinder d = (Spherocylinder) obj;
return (label.equalsIgnoreCase(d.getLabel())
&& Math.abs(radius - d.getRadius()) < .000001
&& Math.abs(height - d.getCylinderHeight()) < .000001);
}
}
//method for checkstyle.
public int hashCode() {
return 0;
}
//campareTo implementation for the Comparable interface.
public int compareTo(Spherocylinder obj) {
if (Math.abs(this.volume() - obj.volume()) < 0.000001) {
return 0;
}
else if (this.volume() < obj.volume()) {
return -1;
}
else {
return 1;
}
}
//method to diplay object desciption.
public String toString() {
DecimalFormat decFormat = new DecimalFormat("#,##0.0##");
String output = "Spherocylinder "" + label + "" with radius "
+ decFormat.format(radius) + " and cylinder height "
+ decFormat.format(height) + " has:";
output += " circumference = " + decFormat.format(circumference())
+ " units";
output += " surface area = " + decFormat.format(surfaceArea())
+ " square units";
output += " volume = " + decFormat.format(volume()) + " cubic units";
return output;
}
}
---------------------------------------------------------------------------------------------------
import java.text.DecimalFormat;
public class SpherocylinderList {
private String listName = "";
private Spherocylinder[] sList;
private int elements = 0;
public SpherocylinderList(String nameIn,
Spherocylinder[] arrayIn, int elementsIn) {
listName = nameIn;
sList = arrayIn;
elements = elementsIn;
}
public String getName() {
return listName;
}
public int numberOfSpherocylinders() {
return elements;
}
public double totalSurfaceArea() {
double sum = 0.0;
for (int i = 0; i < elements; i++) {
sum += sList[i].surfaceArea();
}
return sum;
}
public double totalVolume() {
double sum = 0.0;
for (int i = 0; i < elements; i++) {
sum += sList[i].volume();
}
return sum;
}
public double averageSurfaceArea() {
double sum = totalSurfaceArea() / elements;
return sum;
}
public double averageVolume() {
double sum = totalVolume() / elements;
return sum;
}
public String toString() {
DecimalFormat decFormat = new DecimalFormat("#,##0.0##");
String output = "----- Summary for Spherocylinder Test List -----"
+ " " + "Number of Spherocylinders: "
+ elements + " " + "Total Surface Area: "
+ decFormat.format(totalSurfaceArea()) + " ";
output += "Total Volume: " + decFormat.format(totalVolume()) + " ";
output += "Average Surface Area: "
+ decFormat.format(averageSurfaceArea()) + " ";
output += "Average Volume: " + decFormat.format(averageVolume()) + " ";
return output;
}
public Spherocylinder[] getList() {
return sList;
}
public void addSpherocylinder(String labelIn, int radiusIn, int heightIn) {
if (labelIn != null) {
Spherocylinder tempObj = new Spherocylinder(labelIn,
radiusIn, heightIn);
int size = elements + 1;
Spherocylinder [] tempArray = new Spherocylinder[size];
for (int i = 0; i < elements; i++) {
tempArray[i] = sList[i];
}
tempArray[size - 1] = tempObj;
elements++;
sList = tempArray;
}
}
public Spherocylinder findSpherocylinder(String labelIn) {
Spherocylinder temp = null;
boolean found = false;
for (int i = 0; i < elements; i++) {
//for (Spherocylinder tmp : sList) {
if (labelIn != null) {
if (sList[i].getLabel().toUpperCase()
.equals(labelIn.toUpperCase())) {
temp = sList[i];
found = true;
}
}
}
if (found) {
return temp;
}
else {
return null;
}
}
public Spherocylinder deleteSpherocylinder(String labelIn) {
Spherocylinder tmpObj = null;
Spherocylinder[] newArr = new Spherocylinder[sList.length - 1];
boolean found = false;
int index = 0;
if (labelIn != null) {
for (int i = 0; i < elements; i++) {
if (sList[i].getLabel().toUpperCase()
.equals(labelIn.toUpperCase())) {
found = true;
tmpObj = sList[i];
index = i;
sList[i] = null;
}
}
for (int j = 0; j < index; j++) {
if (sList[j] != null) {
newArr[j] = sList[j];
}
}
for (int k = index; k < elements - 1; k++) {
if (k < elements) {
newArr[k] = sList[k + 1];
}
}
}
if (found) {
elements--;
sList = newArr;
return tmpObj; }
else {
return null; }
}
public boolean editSpherocylinder(String labelIn, int radiusIn,
int heightIn) {
boolean edited = false;
if (labelIn != null) {
for (int i = 0; i < elements; i++) {
//for (Spherocylinder tmp : sList) {
if (sList[i].getLabel() == labelIn) {
sList[i].setRadius(radiusIn);
sList[i].setCylinderHeight(heightIn);
edited = true;
}
}
}
return edited;
}
public Spherocylinder findSpherocylinderWithLargestVolume() {
Spherocylinder temp = null;
double num = 0.0;
for (int i = 0; i < elements; i++) {
if (sList[i].volume() > num) {
num = sList[i].volume();
temp = sList[i];
}
}
if (num != 0.0) {
return temp;
}
else {
return null;
}
}
}
-----------------------------------------------------------------------------------------
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class SpherocylinderListTest {
private Spherocylinder example1 = new Spherocylinder("Small Example",
0.5, 0.25);
private Spherocylinder example2 = new Spherocylinder("Medium Example",
10.8, 10.1);
private Spherocylinder example3 = new Spherocylinder("Large Example",
98.32, 99.0);
private Spherocylinder[] tempArr = {example1, example2, example3};
private static final double DELTA = 1e-15;
@Before public void setUp() {
}
@Test public void getNameTest() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
Assert.assertEquals("test", sArray.getName());
}
@Test public void numberOfSpherocylindersTest() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
Assert.assertEquals(3, sArray.numberOfSpherocylinders());
}
@Test public void totalSurfaceAreaTest() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
Assert.assertEquals(184790.42641495977, sArray.totalSurfaceArea(), DELTA);
}
@Test public void totalVolumeTest() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
Assert.assertEquals(6996733.04095263, sArray.totalVolume(), DELTA);
}
@Test public void averageSurfaceAreaTest() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
Assert.assertEquals(61596.80880498659,
sArray.averageSurfaceArea(), DELTA);
}
@Test public void averageVolumeTest() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
Assert.assertEquals(2332244.34698421, sArray.averageVolume(), DELTA);
}
@Test public void toStringTest() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
SpherocylinderList sArray2 = new SpherocylinderList("test", tempArr, 3);
String compare = sArray.toString();
String result = sArray2.toString();
Assert.assertEquals(result, compare);
}
@Test public void getListTest() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
Spherocylinder[] tmp = sArray.getList();
Assert.assertArrayEquals(tmp, tempArr);
}
@Test public void addSpherocylinderTest() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
sArray.addSpherocylinder("List Test", 1, 2);
Assert.assertEquals(4, sArray.numberOfSpherocylinders());
}
@Test public void findSpherocylinderTest() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
Spherocylinder sc = sArray.findSpherocylinder("Small Example");
Assert.assertEquals(sc, sArray.findSpherocylinder("Small Example"));
}
@Test public void findSpherocylinderTest2() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
Assert.assertEquals(null, sArray.findSpherocylinder("blah"));
}
@Test public void deleteSpherocylinderTest() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
sArray.deleteSpherocylinder("Small Example");
Assert.assertEquals(2, sArray.numberOfSpherocylinders());
}
@Test public void deleteSpherocylinderTest2() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
Assert.assertEquals(null, sArray.deleteSpherocylinder("salad"));
}
@Test public void editSpherocylinderTest() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
boolean test = sArray.editSpherocylinder("Small Example", 5, 25);
Assert.assertEquals(true, test);
}
@Test public void editSpherocylinderTest2() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
boolean test = sArray.editSpherocylinder("snap", 5, 25);
Assert.assertEquals(false, test);
}
@Test public void findSpherocylinderWithLargestVolume() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
Spherocylinder sc = sArray.findSpherocylinderWithLargestVolume();
System.out.println(sc);
Assert.assertEquals(sc, sArray.findSpherocylinder("Large Example"));
}
@Test public void findSpherocylinderWithLargestVolume2() {
SpherocylinderList sArray = new SpherocylinderList("test", tempArr, 3);
sArray.editSpherocylinder("Small Example", 0, 0);
sArray.editSpherocylinder("Medium Example", 0, 0);
sArray.editSpherocylinder("Large Example", 0, 0);
Assert.assertEquals(null, sArray.findSpherocylinderWithLargestVolume());
}
}
------------------------------------------------------------------------------------------------------
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class SpherocylinderTest {
private static final double DELTA = .001;
@Before public void setUp() {
}
@Test public void getLabelTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
Assert.assertEquals("Small Example", example1.getLabel());
}
@Test public void setLabelTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
example1.setLabel("Test");
// expected, actual
Assert.assertEquals("Test", example1.getLabel());
}
@Test public void setLabelTest2() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
example1.setLabel("Test");
// expected, actual
Assert.assertEquals(false, example1.setLabel(null));
}
@Test public void getRadiusTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
// expected, actual
Assert.assertEquals(0.5, example1.getRadius(), DELTA);
}
@Test public void setRadiusTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
example1.setRadius(0.2);
// expected, actual
Assert.assertEquals(0.2, example1.getRadius(), DELTA);
}
@Test public void setRadiusTest2() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
example1.setRadius(0.2);
// expected, actual
Assert.assertEquals(false, example1.setRadius(-0.1));
}
@Test public void getCylinderHeightTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
example1.getCylinderHeight();
// expected, actual
Assert.assertEquals(0.25, example1.getCylinderHeight(), DELTA);
}
@Test public void setCylinderHeightTestEquals() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
example1.setCylinderHeight(0.1);
// expected, actual
Assert.assertEquals(0.1, example1.getCylinderHeight(), DELTA);
}
@Test public void setCylinderHeightTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
example1.setCylinderHeight(0.1);
// expected, actual
Assert.assertEquals(false, example1.setCylinderHeight(-0.1));
}
@Test public void circumferenceTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
// expected, actual
Assert.assertEquals(3.142, example1.circumference(), DELTA);
}
@Test public void surfaceAreaTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
//example1.circumference();
// expected, actual
Assert.assertEquals(3.927, example1.surfaceArea(), DELTA);
}
@Test public void volumeTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
// expected, actual
Assert.assertEquals(0.72, example1.volume(), DELTA);
}
@Test public void countTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
Spherocylinder.resetCount();
Spherocylinder example2 = new Spherocylinder("Small Example", 0.5, 0.25);
// expected, actual
Assert.assertEquals(1, example2.getCount());
}
@Test public void equalsTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
Spherocylinder example2 = new Spherocylinder("Small Example", 0.5, 0.25);
Assert.assertEquals(true, example1.equals(example2));
}
@Test public void equalsTest2() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
Spherocylinder example3 = new Spherocylinder("Large Example",
98.32, 99.0);
Assert.assertEquals(false, example1.equals(example3));
}
@Test public void equalsTest3() {
String test = "";
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
Assert.assertEquals(false, example1.equals(test));
}
@Test public void equalsTest4() {
String test = "";
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
Spherocylinder example2 = new Spherocylinder("Small Example", 0.75, 0.25);
Assert.assertEquals(false, example1.equals(example2));
}
@Test public void equalsTest5() {
String test = "";
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
Spherocylinder example2 = new Spherocylinder("Small Example", 0.5, 0.35);
Assert.assertEquals(false, example1.equals(example2));
}
@Test public void toStringTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
String toString = example1.toString();
Assert.assertTrue(toString.contains("Small Example"));
}
@Test public void hashCodeTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
Assert.assertEquals(0, example1.hashCode());
}
@Test public void compareToTest1() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
Spherocylinder example2 = new Spherocylinder("Small Example", 0.5, 0.25);
Assert.assertTrue(example1.compareTo(example2) == 0);
}
@Test public void compareToTest2() {
Spherocylinder example1 = new Spherocylinder("Small Example",
0.5, 0.25);
Spherocylinder example2 = new Spherocylinder("Medium Example",
10.8, 10.1);
Assert.assertTrue(example1.compareTo(example2) < 0);
}
@Test public void compareToTest3() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
Spherocylinder example3 = new Spherocylinder("Large Example",
98.32, 99.0);
Assert.assertTrue(example3.compareTo(example1) > 0);
}
@Test public void resetCountTest() {
Spherocylinder example1 = new Spherocylinder("Small Example", 0.5, 0.25);
example1.resetCount();
Assert.assertEquals(0, example1.getCount());
}
}