Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added algorithms for surface area calculation #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 90 additions & 0 deletions src/main/scala/Mathematics/SurfaceArea.scala
Original file line number Diff line number Diff line change
@@ -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
}


}
77 changes: 77 additions & 0 deletions src/test/scala/Mathematics/SurfaceAreaSpec.scala
Original file line number Diff line number Diff line change
@@ -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")
}


}