Last active
October 7, 2022 15:58
-
-
Save isaacadariku/dd96b1ed4c3519e3d87f58348d2aa353 to your computer and use it in GitHub Desktop.
This gist is a solution to the comment Image below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math' as math; | |
void main() { | |
final Circle c1 = Circle.all(color: 'blue', radius: 2.0); | |
c1.getRadius(); | |
c1.getColor(); | |
c1.getArea(); | |
final Circle c2 = Circle.radius(radius: 2.0); | |
c2.getRadius(); | |
c2.getColor(); | |
c2.getArea(); | |
final Circle c3 = Circle(); | |
c3.getRadius(); | |
c3.getColor(); | |
c3.getArea(); | |
} | |
class Circle { | |
// Multiple constructor can't have same name | |
/// No radius and color is required | |
Circle([this.radius = 1.0, this.color = 'red']); | |
/// Radius is required | |
Circle.radius({required this.radius, this.color = 'red'}); | |
/// Both radius and color is required | |
Circle.all({required this.color, required this.radius}); | |
final double radius; | |
final String color; | |
/// Method to get the radius | |
double getRadius() { | |
print('The radius of circle is: $radius'); | |
return radius; | |
} | |
/// Method to get the color | |
String getColor() { | |
print('The color of circle is: $color'); | |
return color; | |
} | |
/// Method to get the area of the circle | |
/// Since area of a circle is πr2 | |
/// We will define our pie with a dart maths const of π = 3.142 | |
double getArea() { | |
/// making the object [this.radius] a square | |
final radiusSquare = math.pow(radius, 2); | |
final area = math.pi * radiusSquare; | |
print('The area of the circle is: ${area.toStringAsFixed(2)}'); | |
return area; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The implementation of this image can be see above.
Click HERE to run solution on Dartpad online