Skip to content

Instantly share code, notes, and snippets.

@otoo-peacemaker
Created October 27, 2022 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otoo-peacemaker/86990078b225481ce4dc486ed1d4f411 to your computer and use it in GitHub Desktop.
Save otoo-peacemaker/86990078b225481ce4dc486ed1d4f411 to your computer and use it in GitHub Desktop.
Stage 1 Task: Mobile
import kotlin.time.times
/**
* Write a program that creates a [Circle] with the first constructor,
* a circle with the second constructor and a circle with the third constructor.
* Then print the area, circumference, description and color of each circle.
* [Notice] : Please kindly note that steps 2-4 is covered ones in the class constructor where:
* 1. user can invoke the class with no parameters
* 2. user can invoke the class with only radius as a parameter and the color is already set to "red"
* 3. user can invoke the class with two parameters
* */
const val pi = 3.142
class Circle(
private var radius: Double = 1.0,
private var color: String = "red"
){
/**
* A method that returns the [getArea] of a circle*/
fun getArea()= pi.times(radius.times(radius))
/**
* A method that returns the [getCircumference] of a circle*/
fun getCircumference() = 2*pi.times(radius)
/**
* A method that returns the [getDescription] of a circle*/
fun getDescription() = run { "Radius: $radius Color: $color"}
/**
* A method that returns the [getColor] of a circle*/
fun getColor() = color
}
/**
* You can edit, run, and share this code.
* play.kotlinlang.org
*/
fun main() {
val circle1 = Circle()
println("Circle 1:")
println("Area:${circle1.getArea()}")
println("Circumference:${circle1.getCircumference()}")
println("Description:${circle1.getDescription()}")
println("Color:${circle1.getColor()}\n")
val circle2 = Circle(radius = 2.0)
println("Circle 2:")
println("Area:${circle2.getArea()}")
println("Circumference:${circle2.getCircumference()}")
println("Description:${circle2.getDescription()}")
println("Color:${circle2.getColor()}\n")
val circle3 = Circle(radius = 2.0, color = "blue")
println("Circle 3:")
println("Area:${circle3.getArea()}")
println("Circumference:${circle3.getCircumference()}")
println("Description:${circle3.getDescription()}")
println("Color:${circle3.getColor()}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment