Dart mode

x
 
1
import 'dart:math' show Random;
2
3
void main() {
4
  print(new Die(n: 12).roll());
5
}
6
7
// Define a class.
8
class Die {
9
  // Define a class variable.
10
  static Random shaker = new Random();
11
12
  // Define instance variables.
13
  int sides, value;
14
15
  // Define a method using shorthand syntax.
16
  String toString() => '$value';
17
18
  // Define a constructor.
19
  Die({int n: 6}) {
20
    if (4 <= n && n <= 20) {
21
      sides = n;
22
    } else {
23
      // Support for errors and exceptions.
24
      throw new ArgumentError(/* */);
25
    }
26
  }
27
28
  // Define an instance method.
29
  int roll() {
30
    return value = shaker.nextInt(sides) + 1;
31
  }
32
}
33