Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
1,490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.86 KB | None | 0 0
  1. // Import the dart: math module to access the "sqrt" function.
  2. import 'dart:math' as math;
  3.  
  4. // Definition of the Point class.
  5. class Point {
  6.  
  7.     // The keyword "final" means that variables can only be assigned once.
  8.     final num x, y;
  9.    
  10.     // A constructor: the syntactic sugar allows to initialize the variables.
  11.     Point(this.x, this.y);
  12.    
  13.     // A named constructor, with an initialization list.
  14.     Point.origin() : x = 0, y = 0;
  15.    
  16.     // Definition of a method.
  17.     num distanceTo(Point other) {
  18.         var dx = x - other.x;
  19.         var dy = y - other.y;
  20.         return math.sqrt(dx * dx + dy * dy);
  21.     }
  22. }
  23.  
  24. // main() is the entry point for Dart programs.
  25. main() {
  26.     // Instantiation of Point objects.
  27.     var p1 = new Point(10, 10);
  28.     var p2 = new Point.origin();
  29.     var distance = p1.distanceTo(p2);
  30.     print(distance);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement