Repository navigation
Expand file tree
/
Copy pathSurfaceArea.scala
More file actions
90 lines (78 loc) · 3.36 KB
/
Copy pathSurfaceArea.scala
File metadata and controls
90 lines (78 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
}
}