The starter code for lab 02.
Go to the https://dartpad.dartlang.org/ website, which allows you to run stand-alone Dart programs. The code for all parts of this lab should go into the same file.
For this lab, you will explore some of the features around functions and classes in the Dart programming language.
Solve the following problems using Dart:
- Given a list of (
double
) grades (between0
and100
), use themap
method with a lambda function to generate a list of grades scaled to within the range of0
-30
, followed by an increase of2
(grades
will then be between2
and32
). - Write a class, called
Student
, which contains two (String
) fields (sid
andname
), a constructor that takes these two values as named arguments, and atoString()
method to allow it to be printed. - Given a list of (
int
) numbers, use the map method with an anonymous function to generate a list ofStudent
instances containing a generated student'sname
('Student #' followed by the number) and a generatedsid
(equal to100000000
+ the number, but as aString
). - Write some code to iterate over these students and print them (using any method you prefer)
- A few Dart classes have been defined, below. The 2nd and 3rd classes use two mix-ins called
Magic
andMelee
. TheMagic
mix-in will define two (public
,int
) instance variables (magicDamage
andmana
), as well as a function (castSpell
). ThecastSpell
function will take a secondCharacter
instance as an argument and will subtract 10 from the character'smana
, and calculate damage (magicDamage
- the other character's defense stat), subtracting this damage from theirhp
and returning the damage. TheMelee
mix-in will be similar. It will have two (public
,int
) instance variables (attackPower
andstamina
), and will also define a method (attack
). Theattack
method will subtract10
from the character'sstamina
, and will calculate damage (attackPower
- the other character's defense stat). It will subtract this damage from the other character'shp
and return the damage. Your job is to create these two mix-ins.
class Character {
String name;
int hp;
int defense;
Character({this.name, this.hp, this.defense});
}
class Player extends Character with Magic {
Player({name, hp, magicDamage, mana, defense})
: super(name: name, hp: hp, defense: defense) {
this.mana = mana;
this.magicDamage = magicDamage;
}
}
class Enemy extends Character with Melee {
Enemy({name, hp, attackPower, stamina, defense})
: super(name: name, hp: hp, defense: defense) {
this.stamina = stamina;
this.attackPower = attackPower;
}
}
- Write some basic code to test our mix-ins, by instantiating one
Enemy
(enemy
) and onePlayer
(player
). First, theenemy
will melee attack theplayer
, then theplayer
will cast a spell against theenemy
. The output should look like the following (though, with different stats the numbers may differ):
Boss hits Player for 5 points of damage!
Player hits Boss for 10 points of damage!
If you run into difficulty, you may wish to check out some of the following resources:
- https://dart.dev/tutorials - A series of tutorials for the Dart programming language, focusing entirely on the features of Dart.
- http://stackoverflow.com/ - A forum for asking questions about programming. I bet you know this one already!
Of course, you can always ask the TA for help! However, learning how to find the answers out for yourself is not only more satisfying, but results in greater learning as well.
Save your .dart file in this folder, commit, and then push your code to this repository to submit your lab assignment.