Lab 1.2 – Object Oriented Programming Using Dart
Lab 1.2 – Object Oriented Programming Using Dart
The exercises below are based on a game where a spaceship avoids obstacles in space. The
ship is positioned at the bottom of a coordinate system and can only move left and right while
obstacles "fall" from top to bottom. Throughout the exercises, you'll create classes to represent
different types of spaceships that can be used in the game.
Create a Spaceship class with three variable properties: name, health, and position. The default
value of name should be an empty string and health should be 0. position will be represented
by an Int where negative numbers place the ship further to the left and positive numbers place
the ship further to the right. The default value of position should be 0.
class Spaceship {
// Data Members
var name = "";
var health = 100;
var position = 0;
//Functional Members
void moveLeft() {
position -= 1;
}
void moveRight() {
position += 1;
}
void wasHit() {
health -= 5;
if (health <= 0) {
print(
"Sorry, your ship was hit one too many times. Do you want to play
again?");
}
}
}
Create a let constant called falcon and assign it to an instance of Spaceship. After initialization,
set name to "Falcon". Add a method called moveLeft() to the definition of Spaceship. This
method should adjust the position of the spaceship to the left by one. Add a similar method
called moveRight() that moves the spaceship to the right. Once these methods exist, use them
to move falcon to the left twice and to the right once. Print the new position of falcon after each
change in position.
void main() {
final falcon = Spaceship();
falcon.name = "Falcon";
print(falcon.position);
falcon.moveLeft();
print(falcon.position);
falcon.moveLeft();
print(falcon.position);
falcon.moveRight();
print(falcon.position);
}
The last thing Spaceship needs for this example is a method to handle what happens if the ship
gets hit. Go back and add a method wasHit() to Spaceship that will decrement the ship's health
by 5, then if health is less than or equal to 0 will print "Sorry, your ship was hit one too many
times. Do you want to play again?" Once this method exists, call it on falcon and print out the
value of health.
falcon.wasHit();
print(falcon.health);
Define a new class Fighter that inherits from Spaceship. Add a variable property weapon that
defaults to an empty string and a variable property remainingFirePower that defaults to 5.
class Spaceship {
// Data Members
var name = "";
var health = 100;
var position = 0;
//Functional Members
void moveLeft() {
position -= 1;
}
void moveRight() {
position += 1;
}
void wasHit() {
health -= 5;
if (health <= 0) {
print(
"Sorry, your ship was hit one too many times. Do you want to play
again?");
}
}
}
Create a new instance of Fighter called destroyer. A Fighter will be able to shoot incoming
objects to avoid colliding with them. After initialization, set weapon to "Laser" and
remainingFirePower to 10. Note that since Fighter inherits from Spaceship, it also has
properties for name, health, and position, and has methods for moveLeft(), moveRight(), and
wasHit() even though you did not specifically add them to the declaration of Fighter. Knowing
that, set name to "Destroyer," print position, then call moveRight() and print position again.
void main() {
final falcon = Spaceship();
falcon.name = "Falcon";
print(falcon.position);
falcon.moveLeft();
print(falcon.position);
falcon.moveLeft();
print(falcon.position);
falcon.moveRight();
print(falcon.position);
Try to print weapon on falcon. Why doesn't this work? Provide your answer in a comment or a
print statement below and remove any code you added that doesn't compile.
//print(falcon.weapon)
// `falcon` does not have a property `weapon` because `weapon` is a property of
`Fighter`, not `Spaceship`. While `Fighter` has
// everything that `Spaceship` has, the reverse is not true.
Add a method to fighter called fire(). This should check to see if remainingFirePower is greater
than 0, and if so, should decrement remainingFirePower by one. If remainingFirePower is not
greater than 0, print "You have no more fire power." Call fire() on destroyer a few times and
print remainingFirePower after each method call.
destroyer.fire();
print(destroyer.remainingFirePower);
destroyer.fire();
print(destroyer.remainingFirePower);
destroyer.fire();
print(destroyer.remainingFirePower);
Define a new class ShieldedShip that inherits from Fighter. Add a variable property
shieldStrength that defaults to 25. Create a new instance of ShieldedShip called defender. Set
name to "Defender" and weapon to "Cannon." Call moveRight() and print position, then call
fire() and print remainingFirePower. Go to your declaration of ShieldedShip and override
wasHit(). In the body of the method, check to see if shieldStrength is greater than 0. If it is,
decrement shieldStrength by 5. Otherwise, decrement health by 5. Call wasHit() on defender
and print shieldStrength and health.
class Spaceship {
// Data Members
var name = "";
var health = 100;
var position = 0;
//Functional Members
void moveLeft() {
position -= 1;
}
void moveRight() {
position += 1;
}
void wasHit() {
health -= 5;
if (health <= 0) {
print(
"Sorry, your ship was hit one too many times. Do you want to play
again?");
}
}
}
void main() {
final falcon = Spaceship();
falcon.name = "Falcon";
print(falcon.position);
falcon.moveLeft();
print(falcon.position);
falcon.moveLeft();
print(falcon.position);
falcon.moveRight();
print(falcon.position);
//print(falcon.weapon)
// `falcon` does not have a property `weapon` because `weapon` is a property of
`Fighter`, not `Spaceship`. While `Fighter` has
// everything that `Spaceship` has, the reverse is not true.
destroyer.fire();
print(destroyer.remainingFirePower);
destroyer.fire();
print(destroyer.remainingFirePower);
destroyer.fire();
print(destroyer.remainingFirePower);
defender.wasHit();
print(defender.shieldStrength);
print(defender.health);
}