Objectives
The goal of this lab is to learn the following concepts:
- How to create and use blueprint actions
- How to create and use a class hierarchical system
- Apply blueprint actions across multiple classes
Overview
In this lab, you will learn how to use blueprints in classes. Blueprints are descriptions of what kind of functionality a class will provide. The idea behind blueprints is to specify the structure of a class without providing an implementation. Using inheritance, you can then use the blueprints to make similar classes use the same actions with the same names to do similar things. In this lab, you are going to create a SuperHero class to create superhero characters. Then, you will use a blueprint action to build each character's inventory and powers. Since the inventory and powers of superheros are different from other superheros, a blueprint action makes good sense to use.
Task 1: Getting Started
Start Sodbeans. Create a new “Quorum Application” project, and name it Lab6_2. In the Main.quorum file, it should contain a Main class and Main action.
Create six additional classes in this project. Name the classes SuperHero, Equipment, SuperPower, Spiderman, Superman, and CaptainAmerica.
Task 2: Building Characters
You are going implement two helper classes that will help set and get the names of equipment and superpowers for the heroes. Both classes should have the following:
- A text type variable to hold the name of the equipment or power
- A setter and a getter to set the name of the equipment or power and to get the name back.
In class SuperHero, you can use the helper classes above to create arrays that will hold the names of equipment and powers for our heroes. The arrays can be created like so:
Array inventory
Array powers
The arrays above are of type Equipment and SuperPower. That is, they will be made of they types that are present in those classes, instead of just type integer or type number. In this case, the arrays will be holding text values. Remember that you must use Libraries.Containers.Array to access the array class. Next, create the following actions:
- action AddSuperPower(SuperPower power): This action takes as an argument an object of class SuperPower. It should add to the powers array the object passed in. Recall that adding to an array can be done likes this:
myArray:Add(21) //adds the integer 21 into the array called myArray
myArray:Add(21) //adds the integer 21 into the array called myArray
- action SaySuperPowers This action should iterate through the powers array until it reaches the end and say to the user each item held in it. Recall that you can get the size of an array like this:
myArray:Add(21)
integer size = myArray:GetSize() //size would be assigned the integer value 1
myArray:Add(21) integer size = myArray:GetSize() //size would be assigned the integer value 1
- action AddEquipment(Equipment item) This action should do the same as action AddSuperPower, but instead should add items to the inventory array.
- action SayInventory This action should do the same as action SaySuperPowers, but instead should iterate through the inventory array and say to the user each item held in it.
- blueprint action BuildCharacter This is a blueprint action that each class(Spiderman, Superman, and CaptainAmerica) will inherit from class SuperHero and implement in a different way. By using blueprints and inheritance in this program, you are able to create a general action that can be used across multiple classes in different ways. This saves you the time of creating this specific action for every class. Next, in Main, try to instantiate an object of type SuperHero. Notice that this resulted in a compiler error. This is because Sodbeans doesn't know what the blueprint actions do.
Task 3: Inheriting blueprint actions from another class
In this task you will be implementing the inherited blueprint action BuildCharacter for classes Superman, Spiderman, and CaptainAmerica. The BuildCharacter action should do the same thing for each class: add equipment and powers to the hero using the setters from classes Equipment and SuperPower, and add the values to the proper array using the AddEquipment action from class SuperHero. For each superhero (Superman, Spiderman, and Captain America), add powers and pieces of equipment that describe that particular hero. i.e:
//Superman
action BuildCharacter
Equipment cape
cape:SetEquipment("has a cape")
parent:SuperHero:AddEquipment(cape)
SuperPower vision
vision:SetPower("can use laser vision")
parent:SuperHero:AddSuperPower(vision)
end
In the above example, I described that Superman has a cape and can use laser vision. For each superhero, create a combination of at least 5 powers and/or equipment that that superhero has.
Task 4: Using inherited actions
After you have action BuildCharacter implemented for each superhero, go into Main and instantiate an object of each superhero and call BuildCharacter. Then, using the SaySuperPowers and SayInventory actions, list the powers and inventory of each superhero for the user. Notice the BuildCharacter action that is called is unique to each superhero.
Sample Output
When run, the user should be told which superhero is being described, and then told that superhero's powers and equipment. Here is a sample output for Superman:
Superman
has a cape
has a blue costume
can use laser vision
can use super strength
can fly
When finished, debug and fix any errors, then show your instructor you code.
Next Tutorial
In the next tutorial, we will discuss Lab 6.3, which describes another practice session with inheritance.