0% found this document useful (0 votes)
8 views

Lab 1.2 – Object Oriented Programming Using Dart

The document outlines a lab exercise focused on Object Oriented Programming using Dart, specifically creating a game involving spaceships that avoid obstacles. It details the creation of a base class 'Spaceship' and its subclasses 'Fighter' and 'ShieldedShip', including methods for movement, health management, and firing weapons. The exercise encourages the implementation of class inheritance and method overriding while providing code examples for each step.

Uploaded by

joe97wy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab 1.2 – Object Oriented Programming Using Dart

The document outlines a lab exercise focused on Object Oriented Programming using Dart, specifically creating a game involving spaceships that avoid obstacles. It details the creation of a base class 'Spaceship' and its subclasses 'Fighter' and 'ShieldedShip', including methods for movement, health management, and firing weapons. The exercise encourages the implementation of class inheritance and method overriding while providing code examples for each step.

Uploaded by

joe97wy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab Exercise – 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?");
}
}
}

class Fighter extends Spaceship {


// Spaceship is a base class and Fighter is a Sub Class
var weapon = "";
var remainingFirePower = 5;
void fire() {
if (remainingFirePower > 0) {
remainingFirePower -= 1;
} else {
print("You have no more fire power.");
}
}
}

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);

final destroyer = Fighter();


destroyer.weapon = "Laser";
destroyer.remainingFirePower = 10;
destroyer.name = "Destroyer";
print(destroyer.position);
destroyer.moveRight();
print(destroyer.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?");
}
}
}

class Fighter extends Spaceship {


// Spaceship is a base class and Fighter is a Sub Class
var weapon = "";
var remainingFirePower = 5;
void fire() {
if (remainingFirePower > 0) {
remainingFirePower -= 1;
} else {
print("You have no more fire power.");
}
}
}

class ShieldedShip extends Fighter {


var shieldStrength = 25;
//Function Overriding means implementing same method differently from the base
class
@override
void wasHit() {
if (shieldStrength > 0) {
shieldStrength -= 5;
} else {
super.wasHit();
}
}
}

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);

final destroyer = Fighter();


destroyer.weapon = "Laser";
destroyer.remainingFirePower = 10;
destroyer.name = "Destroyer";
print(destroyer.position);
destroyer.moveRight();
print(destroyer.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);

final defender = ShieldedShip();


defender.name = "Defender";
defender.weapon = "Cannon";
defender.moveRight();
print(defender.position);
defender.fire();
print(defender.remainingFirePower);

defender.wasHit();
print(defender.shieldStrength);
print(defender.health);
}

You might also like