/*
Calculate the volume of a cube in Scala
---------------------------------------
A cube has equal side lengths. If each side has length "side",
the volume is:
volume = side * side * side
Scala provides math utilities, but for a simple cube,
multiplication is clear and efficient.
*/
object CubeVolume {
// Function that computes the volume of a cube.
// side: length of one side (must be non‑negative)
def cubeVolume(side: Double): Double = {
if (side < 0.0)
throw new IllegalArgumentException("Side length must be non‑negative")
// Compute side³ using multiplication
side * side * side
}
def main(args: Array[String]): Unit = {
val side: Double = 4.0
println(s"Side length: $side")
println(s"Volume of cube: ${cubeVolume(side)}")
}
}
/*
run:
Side length: 4.0
Volume of cube: 64.0
*/