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 area calculation #69

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions src/main/scala/Mathematics/Area.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package Mathematics

import scala.math.{Pi, pow}

object Area {

/** Calculates the area of a circle
* @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 areaCircle(radius: Double): Float = {
if (radius < 0) throw new IllegalArgumentException("areaCircle() only accepts non-negative values")
return Pi.toFloat * pow(radius, 2).toFloat
}

/** Calculates the area of an ellipse
* @param radiusX
* - a double to retrieve x-axis radius of object
* @param radiusY
* - a double to retrieve y-axis radius of object
* @return
* - a float that returns the area of object or an exception for invalid input
*/
def areaEllipse(radiusX: Double, radiusY: Double): Float = {
if (radiusX < 0 || radiusY < 0)
throw new IllegalArgumentException("areaEllipse() only accepts non-negative values")
return (Pi * radiusX * radiusY).toFloat
}

/** Calculates the area of a parallelogram
* @param height
* - a double to retrieve the height of object
* @param base
* - a double to retrieve the base of object
* @return
* - a float that returns the area of object or an exception for invalid input
*/
def areaParallelogram(height: Double, base: Double): Float = {
if (height < 0 || base < 0)
throw new IllegalArgumentException("areaParallelogram() only accepts non-negative values")
return (height * base).toFloat
}

/** Calculates the area of a rhombus
* @param diagonal1
* - a double to retrieve the first diagonal (vertical/horizontal) of object
* @param diagonal2
* - a double to retrieve the second diagonal (vertical/horizontal) of object
* @return
* - a float that returns the area of object or an exception for invalid input
*/
def areaRhombus(diagonal1: Double, diagonal2: Double): Float = {
if (diagonal1 < 0 || diagonal2 < 0)
throw new IllegalArgumentException("areaRhombus() only accepts non-negative values")
return (diagonal1 * diagonal2 / 2).toFloat
}

/** Calculates the area of a rectangle
* @param height
* - a double to retrieve the height of object
* @param width
* - a double to retrieve the width of object
* @return
* - a float that returns the area of object or an exception for invalid input
*/
def areaRectangle(height: Double, width: Double): Float = {
if (height < 0 || width < 0)
throw new IllegalArgumentException("areaRectangle() only accepts non-negative values")
return (height * width).toFloat
}

/** Calculates the area of a square
* @param length
* - a double to retrieve the length of object
* @return
* - a float that returns the area of object or an exception for invalid input
*/
def areaSquare(length: Double): Float = {
if (length < 0) throw new IllegalArgumentException("areaSquare() only accepts non-negative values")
return pow(length, 2).toFloat
}

/** Calculates the area of a triangle
* @param height
* - a double to retrieve the height of object
* @param base
* - a double to retrieve the base of object
* @return
* - a float that returns the area of object or an exception for invalid input
*/
def areaTriangle(height: Double, base: Double): Float = {
if (height < 0 || base < 0)
throw new IllegalArgumentException("areaTriangle() only accepts non-negative values")
return ((height * base) / 2).toFloat
}

/** Calculates the area of a trapezium
* @param base1
* - a double to retrieve the first base length of object
* @param base2
* - a double to retrieve the second base length of object
* @param height
* - a double to retrieve the height of object
* @return
* - a float that returns the area of object or an exception for invalid input
*/
def areaTrapezium(base1: Double, base2: Double, height: Double): Float = {
if (height < 0 || base1 < 0 || base2 < 0)
throw new IllegalArgumentException("areaTrapezium() only accepts non-negative values")
return ((base1 + base2) * height / 2).toFloat
}
}
95 changes: 95 additions & 0 deletions src/test/scala/Mathematics/AreaSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package Mathematics

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers._

class AreaSpec extends AnyFlatSpec {

"A circle with radius 4.679" should "output the correct Float result" in {
assert(Area.areaCircle(4.679) === 68.77902f)
}

"A circle with radius -1" should "output the correct exception result" in {
val caught = intercept[IllegalArgumentException] {
Area.areaCircle(-1)
}
assert(caught.getMessage == "areaCircle() only accepts non-negative values")
}

"An ellipse with radiusX 9.375 and radiusY 7.354" should "output the correct Float result" in {
assert(Area.areaEllipse(9.375, 7.354) === 216.59318f)
}

"An ellipse with radiusX -2 and radiusY -5" should "output the correct exception result" in {
val caught = intercept[IllegalArgumentException] {
Area.areaEllipse(-2, -5)
}
assert(caught.getMessage == "areaEllipse() only accepts non-negative values")
}

"A parallelogram with height 17.497 and base 15.368" should "output the correct Float result" in {
assert(Area.areaParallelogram(17.497, 15.368) === 268.8939f)
}

"A parallelogram with height -2 and base -5" should "output the correct exception result" in {
val caught = intercept[IllegalArgumentException] {
Area.areaParallelogram(-2, -5)
}
assert(caught.getMessage == "areaParallelogram() only accepts non-negative values")
}

"A rhombus with diagonal1 26.758 and diagonal2 75.739" should "output the correct Float result" in {
assert(Area.areaRhombus(26.758, 75.739) === 1013.31208f)
}

"A rhombus with diagonal1 -2 and diagonal2 -5" should "output the correct exception result" in {
val caught = intercept[IllegalArgumentException] {
Area.areaRhombus(-2, -5)
}
assert(caught.getMessage == "areaRhombus() only accepts non-negative values")
}

"A rectangle with height 47.857 and width 95.365" should "output the correct Float result" in {
assert(Area.areaRectangle(47.857, 95.365) === 4563.8828f)
}

"A rectangle with height -2 and width -5" should "output the correct exception result" in {
val caught = intercept[IllegalArgumentException] {
Area.areaRectangle(-2, -5)
}
assert(caught.getMessage == "areaRectangle() only accepts non-negative values")
}

"A square with length 85.352" should "output the correct Float result" in {
assert(Area.areaSquare(85.352) === 7284.9639f)
}

"A square with length -2" should "output the correct exception result" in {
val caught = intercept[IllegalArgumentException] {
Area.areaSquare(-2)
}
assert(caught.getMessage == "areaSquare() only accepts non-negative values")
}

"A triangle with height of 10.395 and length of 5.944" should "output the correct Float result" in {
assert(Area.areaTriangle(10.395, 5.944) === 30.89394f)
}

"A triangle with height of -4 and length of -5" should "output the correct exception result" in {
val caught = intercept[IllegalArgumentException] {
Area.areaTriangle(-4, -5)
}
assert(caught.getMessage == "areaTriangle() only accepts non-negative values")
}

"A trapezium with base1 85.352, base2 75.879 and height 62.869" should "output the correct Float result" in {
assert(Area.areaTrapezium(85.352, 75.879, 62.869) === 5068.21587f)
}

"A trapezium with base1 -1, base2 -2 and height -3" should "output the correct exception result" in {
val caught = intercept[IllegalArgumentException] {
Area.areaTrapezium(-1, -2, -3)
}
assert(caught.getMessage == "areaTrapezium() only accepts non-negative values")
}
}