# Kotlin Class: Circle class with circumference calculation

## Kotlin Class: Exercise-7 with Solution

Write a Kotlin program that creates a class 'Circle' with properties for radius and center coordinates. Include a function to calculate the circle circumference.

Sample Solution:

Kotlin Code:

``````import kotlin.math.PI

class Circle(
val centerX: Double,
val centerY: Double
) {
fun calculateCircumference(): Double {
return 2 * PI * radius
}
}

fun main() {
val circle = Circle(7.0, 0.0, 0.0)
val circumference = circle.calculateCircumference()
println("Circle Circumference: \$circumference")
}
```
```

Sample Output:

```Circle Circumference: 43.982297150257104
```

Explanation:

In the above exercise -

• First we define a class "Circle" with three properties: radius, centerX, and centerY. The class includes a function calculateCircumference that calculates and returns the circle circumference using the formula 2 *π *radius. This is the mathematical constant PI.
• In the "main()" function, we create an instance of the Circle class named circle with a radius of 5.0 and center coordinates at (0.0, 0.0). We then call the "calculateCircumference()" function on the circle object and store the result in the circumference variable. Finally, we print the calculated circumference.

Kotlin Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿