Leave a rating/review
Now that you have an idea on how to create a class, it’s time to put your new skills to the test. I want your to define an RPG Character class. The class name should be RPGCharacter. It should have a character name as string. Finally, create three instance variables for strength, dexterity and constitution. These should be of type int.
Now comes the fun part. Create a method called rollStats. It should assign a random number between 3 to 18 for each instance variable. To do this, you need to import the math package. Then you call the nextInt method on the random object. I’ll provide the code in the challenge slide.
Finally, create another method called printStats that will print out the character stats. Now that’s a lot of things to do, but you can do this! I know you can. So pause the video and try it out. If you get stuck, just wait a few seconds, I’ll walk you through the solution.
How’d that go? Granted, that was a lot of things I put on your plate, but this is just building on everything you have learned so far. We’ll get started by defining our RPGCharacter class.
class RPGCharacter {
}
Now lets define our instance variables.
String name = '';
int strength = 0;
int dexterity = 0;
int constitution = 0;
We’re setting some default values. If we don’t we’ll get an error. You’ll learn about that soon enough. For now, we’ll just work with these. Now to generate the random number. We’ll need to import the math library.
import 'dart:math';
Importing code allows to access all the various functions and classes created by other developers. Now let’s write our rollStats method.
void rollStats() {
strength = Random().nextInt(16) + 3;
dexterity = Random().nextInt(16) + 3;
constitution = Random().nextInt(16) + 3;
}
Because we aren’t returning a value, we set the return type to be void. Then we call the nextInt method. We pass in sixteen, meaning the method will return from zero to fifteen. After which, we add three producing a random number from three to eighteen.
Now let’s write our printStats method.
void printStats() {
print('$name has $strength strength, $dexterity dexterity, and $constitution constitution');
}
All this does is print out all the field. Now lets create an instance of our class. First, we’ll create Fizboz.
var fizBoz = RPGCharacter();
Next, we’ll assign him a name.
fizBoz.name = 'FizBoz';
Now we’ll roll stats and then call print stats.
fizBoz.rollStats();
fizBoz.printStats();
Now run the program and we to see our character. Now what if we confused the order of rollStats and printStats. Let’s do this now.
fizBoz.printStats();
fizBoz.rollStats();
Now we have no stats whatsoever. This means the caller of the code will need to know the proper order of calling methods. That’s really not a good design. Rather, it’d be better for the stats to be automatically rolled when the character is created. We can actually do this like so:
int strength = Random().nextInt(16) + 3;
int dexterity = Random().nextInt(16) + 3;
int constitution = Random().nextInt(16) + 3;
Now we can delete the roll stats call and just print out the results.
void main() {
var fizBoz = RPGCharacter();
fizBoz.name = 'FizBoz';
fizBoz.printStats();
}
Now run the program. Everything works just like before. Except, we have a bit of code duplication which isn’t a good thing at all. A better solution is for the object to call the roll stats method when the object is created and good place for that is the constructor. What’s a constructor? Well, see me in the next episode and you’ll find out.