From 8dfcd15c263109d7ad7bbb197699dc5a44d202c8 Mon Sep 17 00:00:00 2001 From: tongkahpauu <110804459+tongkahpauu@users.noreply.github.com> Date: Mon, 8 Aug 2022 13:09:42 +0800 Subject: [PATCH 1/2] Added algorithms for surface area calculation --- src/main/scala/Mathematics/SurfaceArea.scala | 90 ++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/main/scala/Mathematics/SurfaceArea.scala diff --git a/src/main/scala/Mathematics/SurfaceArea.scala b/src/main/scala/Mathematics/SurfaceArea.scala new file mode 100644 index 0000000..3b24fec --- /dev/null +++ b/src/main/scala/Mathematics/SurfaceArea.scala @@ -0,0 +1,90 @@ +package Mathematics + +import scala.math.{Pi, pow} + +object SurfaceArea { + + /** Calculates the total surface area of a sphere + * @param radius + * - a double to retrieve the radius of object + * @return + * - a float that returns the area of object or an exception for invalid input + */ + def surfaceAreaCircle(radius: Double): Float = { + if (radius < 0) throw new IllegalArgumentException("surfaceAreaCircle() only accepts non-negative values") + return 4 * Pi.toFloat * pow(radius, 2).toFloat + } + + /** Calculates the total surface area of cuboid + * @param length + * - a double to retrieve length of object + * @param height + * - a double to retrieve height of object + * @param breadth + * - a double to retrieve breadth of object + * @return + * - a float that returns the surface area of object or an exception for invalid input + */ + def surfaceAreaCuboid(length: Double, height: Double,breadth:Double): Float = { + if (length< 0 || height < 0||breadth <0) + throw new IllegalArgumentException("surfaceAreaCuboid() only accepts non-negative values") + return (2 *((length*breadth)+(breadth*height)+(length*height))).toFloat + } + + /** Calculates the total surface area of cube + * @param side + * - a double to retrieve side of object + * @return + * - a float that returns the surface area of object or an exception for invalid input + */ + def surfaceAreaCube(side: Double): Float = { + if (side<0) + throw new IllegalArgumentException("surfaceAreaCube() only accepts non-negative values") + return (6 * pow(side, 2)).toFloat + } + + + + /** Calculates the total surface area of a right circular cylinder + * @param radius + * - a double to retrieve the radius of object + * @param height + * - a double to retrieve the height of object + * @return + * - a float that returns the surface area of object or an exception for invalid input + */ + def surfaceAreaCylinder(radius: Double, height: Double): Float = { + if (radius< 0 || height < 0) + throw new IllegalArgumentException("surfaceAreaCyclinder() only accepts non-negative values") + return (2*Pi*radius*(radius+height)).toFloat + } + + /** Calculates the surface area of a right pyramid + * @param side + * - a double to retrieve the side of object + * @param height + * - a double to retrieve the height of object + * @return + * - a float that returns the surface area of object or an exception for invalid input + */ + def surfaceAreaPyramid(side: Double, height:Double): Float = { + if (side < 0 || height < 0) throw new IllegalArgumentException("surfaceAreaPyramid() only accepts non-negative values") + return (0.5*((side*4)*height)).toFloat + pow(side, 2).toFloat + } + + /** Calculates the surface area of right circular cone + * @param length + * - a double to retrieve the length of object + * @param radius + * - a double to retrieve the radius of object + * @return + * - a float that returns the surface area of object or an exception for invalid input + */ + def surfaceAreaCone(length: Double, radius: Double): Float = { + if (length < 0 || radius < 0) + throw new IllegalArgumentException("surfaceAreaCone() only accepts non-negative values") + return Pi.toFloat*radius.toFloat * (length+radius).toFloat + } + + +} \ No newline at end of file From 8132afd19ed2be6aebfc7993ea771a04d8fcef93 Mon Sep 17 00:00:00 2001 From: tongkahpauu <110804459+tongkahpauu@users.noreply.github.com> Date: Mon, 8 Aug 2022 13:10:21 +0800 Subject: [PATCH 2/2] Added test class for surface area calculation --- .../scala/Mathematics/SurfaceAreaSpec.scala | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/test/scala/Mathematics/SurfaceAreaSpec.scala diff --git a/src/test/scala/Mathematics/SurfaceAreaSpec.scala b/src/test/scala/Mathematics/SurfaceAreaSpec.scala new file mode 100644 index 0000000..fd85c3f --- /dev/null +++ b/src/test/scala/Mathematics/SurfaceAreaSpec.scala @@ -0,0 +1,77 @@ +package Mathematics + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers._ + +class SurfaceAreaSpec extends AnyFlatSpec { + + "A sphere with radius 4.679" should "output the correct Float result" in { + assert(SurfaceArea.surfaceAreaSphere(4.679) === 275.11607f) + } + + "A sphere with radius -1" should "output the correct exception result" in { + val caught = intercept[IllegalArgumentException] { + SurfaceArea.surfaceAreaSphere(-1) + } + assert(caught.getMessage == "surfaceAreaSphere() only accepts non-negative values") + } + + "A cuboid with length 8.375 , height 7.354 and breadth 7.354 " should "output the correct Float result" in { + assert(SurfaceArea.surfaceAreaCuboid(8.375, 7.354,7.354) === 354.52163f) + } + + "A cuboid with length -2, height -5, breadth -10" should "output the correct exception result" in { + val caught = intercept[IllegalArgumentException] { + SurfaceArea.surfaceAreaCuboid(-2, -5,-10) + } + assert(caught.getMessage == "surfaceAreaCuboid() only accepts non-negative values") + } + + "A cube with side 5" should "output the correct Float result" in { + assert(SurfaceArea.surfaceAreaCube(5) === 150.0000f) + } + + "A cube with side -2" should "output the correct exception result" in { + val caught = intercept[IllegalArgumentException] { + SurfaceArea.surfaceAreaCube(-2) + } + assert(caught.getMessage == "surfaceAreaCube() only accepts non-negative values") + } + + + + "A right cylinder with radius 7.857 and height 15.365" should "output the correct Float result" in { + assert(SurfaceArea.surfaceAreaCylinder(7.857, 15.365) === 1146.40017f) + } + + "A right cylinder with radius -2 and height -5" should "output the correct exception result" in { + val caught = intercept[IllegalArgumentException] { + SurfaceArea.surfaceAreaCylinder(-2, -5) + } + assert(caught.getMessage == "surfaceAreaCylinder() only accepts non-negative values") + } + + "A right pyramid with side 5.352 and height 7.872" should "output the correct Float result" in { + assert(SurfaceArea.surfaceAreaPyramid(5.352,7.872) === 117.64131f) + } + + "A right pyramid with side -3 and height -4" should "output the correct exception result" in { + val caught = intercept[IllegalArgumentException] { + SurfaceArea.surfaceAreaPyramid(-3,-4) + } + assert(caught.getMessage == "surfaceAreaPyramid() only accepts non-negative values") + } + + "A right circular cone with length of 10.395 and radius of 5.944" should "output the correct Float result" in { + assert(SurfaceArea.surfaceAreaCone(10.395, 5.944) === 334.60221f) + } + + "A right circular cone with length of -4 and radius of -5" should "output the correct exception result" in { + val caught = intercept[IllegalArgumentException] { + SurfaceArea.surfaceAreaCone(-4, -5) + } + assert(caught.getMessage == "surfaceAreaCone() only accepts non-negative values") + } + + +} \ No newline at end of file